Debian Codegraph @

unikey/mactab.cpp

raw
1 // -*- coding:unix; mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 /* Unikey Vietnamese Input Method
3 * Copyright (C) 2000-2005 Pham Kim Long
4 * Contact:
5 * [email protected]
6 * UniKey project: http://unikey.org
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <iostream>
28 #include "mactab.h"
29 #include "vnconv.h"
30
31 using namespace std;
32 #define UKMACRO_VERSION_UTF8 1
33
34 //---------------------------------------------------------------
35 void CMacroTable::init()
36 {
37 m_memSize = MACRO_MEM_SIZE;
38 m_count = 0;
39 m_occupied = 0;
40 }
41
42 //---------------------------------------------------------------
43 char *MacCompareStartMem;
44
45 #define STD_TO_LOWER(x) (((x) >= VnStdCharOffset && \
46 (x) < (VnStdCharOffset + TOTAL_ALPHA_VNCHARS) && \
47 !((x) & 1)) ? \
48 (x+1) : (x))
49
50 int macCompare(const void *p1, const void *p2)
51 {
52 StdVnChar *s1 = (StdVnChar *) ((char *)MacCompareStartMem + ((MacroDef *)p1)->keyOffset);
53 StdVnChar *s2 = (StdVnChar *) ((char *)MacCompareStartMem + ((MacroDef *)p2)->keyOffset);
54
55 int i;
56 StdVnChar ls1, ls2;
57
58 for (i=0; s1[i] != 0 && s2[i] != 0; i++) {
59 ls1 = STD_TO_LOWER(s1[i]);
60 ls2 = STD_TO_LOWER(s2[i]);
61 if (ls1 > ls2)
62 return 1;
63 if (ls1 < ls2)
64 return -1;
65 /*
66 if (s1[i] > s2[i])
67 return 1;
68 if (s1[i] < s2[i])
69 return -1;
70 */
71 }
72 if (s1[i] == 0)
73 return (s2[i] == 0)? 0 : -1;
74 return 1;
75 }
76
77 //---------------------------------------------------------------
78 int macKeyCompare(const void *key, const void *ele)
79 {
80 StdVnChar *s1 = (StdVnChar *)key;
81 StdVnChar *s2 = (StdVnChar *) ((char *)MacCompareStartMem + ((MacroDef *)ele)->keyOffset);
82
83 StdVnChar ls1, ls2;
84 int i;
85 for (i=0; s1[i] != 0 && s2[i] != 0; i++) {
86 ls1 = STD_TO_LOWER(s1[i]);
87 ls2 = STD_TO_LOWER(s2[i]);
88 if (ls1 > ls2)
89 return 1;
90 if (ls1 < ls2)
91 return -1;
92 /*
93 if (s1[i] > s2[i])
94 return 1;
95 if (s1[i] < s2[i])
96 return -1;
97 */
98 }
99 if (s1[i] == 0)
100 return (s2[i] == 0)? 0 : -1;
101 return 1;
102 }
103
104 //---------------------------------------------------------------
105 const StdVnChar *CMacroTable::lookup(StdVnChar *key)
106 {
107 MacCompareStartMem = m_macroMem;
108 MacroDef *p = (MacroDef *)bsearch(key, m_table, m_count, sizeof(MacroDef), macKeyCompare);
109 if (p)
110 return (StdVnChar *)(m_macroMem + p->textOffset);
111 return 0;
112 }
113
114 //----------------------------------------------------------------------------
115 // Read header, if it's present in the file. Get the version of the file
116 // If header is absent, go back to the beginning of file and set version to 0
117 // Return false if reading failed.
118 //
119 // Header format: ;[DO NOT DELETE THIS LINE]***version=n
120 //----------------------------------------------------------------------------
121 bool CMacroTable::readHeader(FILE *f, int & version)
122 {
123 char line[MAX_MACRO_LINE];
124 if (!fgets(line, sizeof(line), f)) {
125 if (feof(f)) {
126 fseek(f, 0, SEEK_SET);
127 version = 0;
128 return true;
129 }
130 return false;
131 }
132
133 //if BOM is available, skip it
134 char *p = line;
135 size_t len = strlen(line);
136 if (len >= 3 && (unsigned char)line[0] == 0xEF && (unsigned char)line[1] == 0xBB &&
137 (unsigned char)line[2] == 0xBF)
138 {
139 p += 3;
140 }
141
142 //read version number
143 p = strstr(p, "***");
144 if (p) {
145 p += 3;
146 //skip possible spaces
147 while (*p == ' ') p++;
148 if (sscanf(p, "version=%d", &version) == 1)
149 return true;
150 }
151
152 fseek(f, 0, SEEK_SET);
153 version = 0;
154 return true;
155 }
156
157 //----------------------------------------------------------------
158 void CMacroTable::writeHeader(FILE *f)
159 {
160 #if defined(WIN32)
161 fprintf(f, "\xEF\xBB\xBF;DO NOT DELETE THIS LINE*** version=%d ***\n", UKMACRO_VERSION_UTF8);
162 #else
163 fprintf(f, "DO NOT DELETE THIS LINE*** version=%d ***\n", UKMACRO_VERSION_UTF8);
164 #endif
165 }
166 //---------------------------------------------------------------
167 int CMacroTable::loadFromFile(const char *fname)
168 {
169 FILE *f;
170 #if defined(WIN32)
171 f = _tfopen(fname, _TEXT("rt"));
172 #else
173 f = fopen(fname, "r");
174 #endif
175
176 if (f == NULL)
177 return 0;
178 char line[MAX_MACRO_LINE];
179 size_t len;
180
181 resetContent();
182
183 //read possible header
184 int version;
185 if (!readHeader(f, version)) {
186 version = 0;
187 }
188
189 while (fgets(line, sizeof(line), f)) {
190 len = strlen(line);
191 if (len > 0 && line[len-1] == '\n')
192 line[len-1] = 0;
193 if (len > 1 && line[len-2] == '\r')
194 line[len-2] = 0;
195 if (version == UKMACRO_VERSION_UTF8)
196 addItem(line, CONV_CHARSET_UNIUTF8);
197 else
198 addItem(line, CONV_CHARSET_VIQR);
199 }
200 fclose(f);
201 MacCompareStartMem = m_macroMem;
202 qsort(m_table, m_count, sizeof(MacroDef), macCompare);
203 // Convert old version
204 if (version != UKMACRO_VERSION_UTF8) {
205 writeToFile(fname);
206 }
207 return 1;
208 }
209
210 //---------------------------------------------------------------
211 int CMacroTable::writeToFile(const char *fname)
212 {
213 FILE *f;
214 #if defined(WIN32)
215 f = _tfopen(fname, _TEXT("wt"));
216 #else
217 f = fopen(fname, "w");
218 #endif
219 return writeToFp(f);
220 }
221
222 int CMacroTable::writeToFp(FILE* f)
223 {
224 int ret;
225 int inLen, maxOutLen;
226
227 if (f == NULL)
228 return 0;
229
230 char line[MAX_MACRO_LINE*3]; //1 VnChar may need 3 chars in UTF8
231 char key[MAX_MACRO_KEY_LEN*3];
232 char text[MAX_MACRO_TEXT_LEN*3];
233
234 writeHeader(f);
235
236 UKBYTE *p;
237 for (int i=0; i < m_count; i++) {
238 p = (UKBYTE *)m_macroMem + m_table[i].keyOffset;
239 inLen = -1;
240 maxOutLen = sizeof(key);
241 ret = VnConvert(CONV_CHARSET_VNSTANDARD, CONV_CHARSET_UNIUTF8,
242 (UKBYTE *) p, (UKBYTE *)key,
243 &inLen, &maxOutLen);
244 if (ret != 0)
245 continue;
246
247 p = (UKBYTE *)m_macroMem + m_table[i].textOffset;
248 inLen = -1;
249 maxOutLen = sizeof(text);
250 ret = VnConvert(CONV_CHARSET_VNSTANDARD, CONV_CHARSET_UNIUTF8,
251 p, (UKBYTE *)text,
252 &inLen, &maxOutLen);
253 if (ret != 0)
254 continue;
255 if (i < m_count-1)
256 sprintf(line, "%s:%s\n", key, text);
257 else
258 sprintf(line, "%s:%s", key, text);
259 fputs(line, f);
260 }
261
262 fclose(f);
263 return 1;
264 }
265
266 //---------------------------------------------------------------
267 int CMacroTable::addItem(const void *key, const void *text, int charset)
268 {
269 int ret;
270 int inLen, maxOutLen;
271 int offset = m_occupied;
272 char *p = m_macroMem + offset;
273
274 if (m_count >= MAX_MACRO_ITEMS)
275 return -1;
276
277 m_table[m_count].keyOffset = offset;
278
279 // Convert macro key to VN standard
280 inLen = -1; //input is null-terminated
281 maxOutLen = MAX_MACRO_KEY_LEN * sizeof(StdVnChar);
282 if (maxOutLen + offset > m_memSize)
283 maxOutLen = m_memSize - offset;
284 ret = VnConvert(charset, CONV_CHARSET_VNSTANDARD,
285 (UKBYTE *)key, (UKBYTE *)p,
286 &inLen, &maxOutLen);
287 if (ret != 0)
288 return -1;
289
290 offset += maxOutLen;
291 p += maxOutLen;
292
293 //convert macro text to VN standard
294 m_table[m_count].textOffset = offset;
295 inLen = -1; //input is null-terminated
296 maxOutLen = MAX_MACRO_TEXT_LEN * sizeof(StdVnChar);
297 if (maxOutLen + offset > m_memSize)
298 maxOutLen = m_memSize - offset;
299 ret = VnConvert(charset, CONV_CHARSET_VNSTANDARD,
300 (UKBYTE *)text, (UKBYTE *)p,
301 &inLen, &maxOutLen);
302 if (ret != 0)
303 return -1;
304
305 m_occupied = offset + maxOutLen;
306 m_count++;
307 return (m_count-1);
308 }
309
310 //---------------------------------------------------------------
311 // add a new macro into the sorted macro table
312 // item format: key:text (key and text are separated by a colon)
313 //---------------------------------------------------------------
314 int CMacroTable::addItem(const char *item, int charset)
315 {
316 char key[MAX_MACRO_KEY_LEN];
317
318 // Parse the input item
319 char * pos = (char*)strchr(item, ':');
320 if (pos == NULL)
321 return -1;
322 int keyLen = (int)(pos - item);
323 if (keyLen > MAX_MACRO_KEY_LEN-1)
324 keyLen = MAX_MACRO_KEY_LEN-1;
325 strncpy(key, item, keyLen);
326 key[keyLen] = '\0';
327 return addItem(key, ++pos, charset);
328 }
329
330 //---------------------------------------------------------------
331 void CMacroTable::resetContent()
332 {
333 m_occupied = 0;
334 m_count = 0;
335 }
336
337 //---------------------------------------------------------------
338 const StdVnChar *CMacroTable::getKey(int idx) const
339 {
340 if (idx < 0 || idx >= m_count)
341 return 0;
342 return (StdVnChar *)(m_macroMem + m_table[idx].keyOffset);
343 }
344
345 //---------------------------------------------------------------
346 const StdVnChar *CMacroTable::getText(int idx) const
347 {
348 if (idx < 0 || idx >= m_count)
349 return 0;
350 return (StdVnChar *)(m_macroMem + m_table[idx].textOffset);
351 }