Debian Codegraph @

unikey/mactab.cpp

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