Debian Codegraph @

src/intl/utils/mapgen4.c

raw
1 /*
2 * The contents of this file are subject to the Interbase Public
3 * License Version 1.0 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy
5 * of the License at http://www.Inprise.com/IPL.html
6 *
7 * Software distributed under the License is distributed on an
8 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
9 * or implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code was created by Inprise Corporation
13 * and its predecessors. Portions created by Inprise Corporation are
14 * Copyright (C) Inprise Corporation.
15 *
16 * All Rights Reserved.
17 * Contributor(s): ______________________________________.
18 */
19
20 #include "firebird.h"
21 #include <stdio.h>
22 #include <string.h>
23
24 typedef struct {
25 unsigned int codepoint;
26 unsigned int equivilant;
27 unsigned int exists; /* Count of times in input table */
28 unsigned int not_defined; /* Defined as NOT DEFINED */
29 char name[80]; /* Name of the character */
30 } CONVERSION;
31
32 typedef struct {
33 unsigned int high_point;
34 unsigned int low_point;
35 unsigned int count;
36 CONVERSION table[0xFFFF + 1];
37 } TABLE;
38
39 TABLE codepoint_conversion;
40 TABLE unicode_conversion;
41
42 #define UNICODE_REPLACEMENT_CHARACTER 0xFFFD
43 #define CANT_MAP_CHARACTER 0
44
45 void print_condensed_indexed_table(char *name, TABLE * table,
46 unsigned short replacement);
47 void print_indexed_table(char *name, TABLE * table,
48 unsigned short replacement);
49 void print_direct_table(char *name, TABLE * table);
50 void declare(unsigned short codepoint, unsigned short unicode, char *name);
51
52 main(int argc, char *argv[])
53 {
54 FILE *f = NULL;
55 char line[100];
56 int i;
57 int linecount = 0;
58 int option_euc = 0;
59 int option_sjis = 0;
60 int option_condensed = 0;
61 int usage = 0;
62 char *filename = NULL;
63
64 for (i = 1; i < argc && !usage; i++)
65 {
66 if (argv[i][0] == '-')
67 {
68 if (strcmp(argv[i], "-euc") == 0)
69 option_euc++;
70 else if (strcmp(argv[i], "-sjis") == 0)
71 option_sjis++;
72 else if (strcmp(argv[i], "-condense") == 0)
73 option_condensed++;
74 else
75 usage++;
76 }
77 else if (filename)
78 usage++;
79 else
80 filename = argv[i];
81 }
82
83 if (usage)
84 {
85 fprintf(stderr,
86 "usage: mapgen4 [-condense | -euc | -sjis] <name of unicode map file>\n");
87 exit(1);
88 }
89
90 f = fopen(filename, "r");
91 if (!f)
92 {
93 fprintf(stderr, "Unable to open file '%s'\n", filename);
94 exit(1);
95 }
96
97 memset((void *) &codepoint_conversion, 0, sizeof(codepoint_conversion));
98 memset((void *) &unicode_conversion, 0, sizeof(unicode_conversion));
99 codepoint_conversion.low_point = 0xFFFF;
100 unicode_conversion.low_point = 0xFFFF;
101
102 printf("/* -------------------------------------------- \n");
103 printf(" THIS FILE WAS GENERATED BY intl/mapgen4.c\n");
104 printf(" ");
105 for (i = 0; i < argc; i++)
106 printf("%s ", argv[i]);
107 printf("\n\n");
108 printf(" Unicode mapping table generated from file %s \n",
109 filename);
110 printf("\n");
111
112
113 while (fgets(line, sizeof(line), f))
114 {
115 unsigned int unicode;
116 unsigned int codepoint;
117 char *p;
118
119 /* Strip trailing control codes from the line */
120 p = &line[strlen(line) - 1];
121 while (p >= line && (*p == '\r' || *p == '\n' || *p == 26))
122 *p-- = 0;
123
124 printf("%s\n", line);
125 linecount++;
126 /* Skip blank lines */
127 if (line[0] == 0)
128 continue;
129 /* Skip comment lines */
130 if (line[0] == '#')
131 continue;
132 p = line;
133
134 if (option_sjis)
135 {
136 unsigned int sjis_point;
137 if (1 != sscanf(p, "0x%04x\t", &sjis_point))
138 {
139 fprintf(stderr, "Problem in input file - line %d\n",
140 linecount);
141 exit(1);
142 }
143 p = strchr(p, '\t') + 1;
144 }
145
146 codepoint = 0;
147 if (1 != sscanf(p, "0x%04x\t", &codepoint))
148 {
149 fprintf(stderr, "Problem in input file - line %d\n",
150 linecount);
151 exit(1);
152 }
153 p = strchr(p, '\t') + 1;
154
155 if (option_euc)
156 codepoint += 0x8080;
157
158 unicode = 0;
159 if (0 == sscanf(p, "0x%04x\t", &unicode))
160 {
161 /* Conversion is not defined */
162 codepoint_conversion.table[codepoint].not_defined++;
163 declare(codepoint, unicode, "NOT DEFINED");
164 }
165 else
166 {
167 /* Find start of comment defining Unicode name */
168 p = strchr(p, '#');
169 if (p)
170 p++; /* Skip comment character */
171 else
172 p = "";
173 declare(codepoint, unicode, p);
174 }
175
176 }
177 fclose(f);
178 printf(" -------------------------------------------- */\n");
179
180 /* Declare any standard ASCII characters that didn't exist in the table */
181 for (i = 0; i <= 0x7f; i++)
182 {
183 if (!codepoint_conversion.table[i].exists)
184 declare(i, i, "ASCII");
185 }
186
187 printf("\n");
188 printf("\n");
189 printf("#ifndef UNICODE_REPLACEMENT_CHARACTER\n");
190 printf("#define UNICODE_REPLACEMENT_CHARACTER 0xFFFD\n");
191 printf("#endif\n");
192 printf("\n");
193
194 /* Small tables print with all comments */
195 if ((codepoint_conversion.high_point < 256) || (!option_condensed))
196 print_indexed_table("to_unicode", &codepoint_conversion,
197 UNICODE_REPLACEMENT_CHARACTER);
198 else
199 print_condensed_indexed_table("to_unicode", &codepoint_conversion,
200 UNICODE_REPLACEMENT_CHARACTER);
201
202
203 printf("\n");
204 printf("\n");
205 printf("#ifndef CANT_MAP_CHARACTER\n");
206 printf("#define CANT_MAP_CHARACTER 0\n");
207 printf("#endif\n");
208 printf("\n");
209
210 /* Small tables print with all comments */
211 if ((codepoint_conversion.count < 256) || (!option_condensed))
212 print_indexed_table("from_unicode", &unicode_conversion,
213 CANT_MAP_CHARACTER);
214 else
215 print_condensed_indexed_table("from_unicode", &unicode_conversion,
216 CANT_MAP_CHARACTER);
217
218 return 0;
219 }
220
221 void declare(unsigned short codepoint, unsigned short unicode, char *name)
222 {
223 if (codepoint < codepoint_conversion.low_point)
224 codepoint_conversion.low_point = codepoint;
225 if (codepoint > codepoint_conversion.high_point)
226 codepoint_conversion.high_point = codepoint;
227
228 if (codepoint_conversion.table[codepoint].exists++)
229 {
230 if (unicode != codepoint_conversion.table[codepoint].equivilant)
231 fprintf(stderr,
232 "Error: duplicate unequal mappings for 0x%04x : 0x%04x and 0x%04x\n",
233 codepoint, unicode,
234 codepoint_conversion.table[codepoint].equivilant);
235 else
236 fprintf(stderr,
237 "Warning: duplicate entries for codepoint 0x%04x to 0x%04x\n",
238 codepoint, unicode);
239 }
240 else
241 codepoint_conversion.count++;
242
243 codepoint_conversion.table[codepoint].codepoint = codepoint;
244 strcpy(codepoint_conversion.table[codepoint].name, name);
245
246 if (!codepoint_conversion.table[codepoint].not_defined)
247 {
248 codepoint_conversion.table[codepoint].equivilant = unicode;
249
250 if (unicode < unicode_conversion.low_point)
251 unicode_conversion.low_point = unicode;
252 if (unicode > unicode_conversion.high_point)
253 unicode_conversion.high_point = unicode;
254 if (!unicode_conversion.table[unicode].exists++)
255 {
256 unicode_conversion.table[unicode].codepoint = unicode;
257 if (unicode != UNICODE_REPLACEMENT_CHARACTER)
258 {
259 unicode_conversion.table[unicode].equivilant = codepoint;
260 strcpy(unicode_conversion.table[unicode].name, name);
261 }
262 }
263 else
264 {
265 if (unicode != UNICODE_REPLACEMENT_CHARACTER)
266 fprintf(stderr,
267 "Warning: Multiple mappings to Unicode 0x%04x : 0x%04x and 0x%04x\n",
268 unicode, codepoint,
269 unicode_conversion.table[unicode].equivilant);
270 }
271 }
272 }
273
274
275 void print_direct_table(char *name, TABLE* table)
276 {
277 int i;
278
279 printf("static const USHORT %s_map[256] = {\n", name);
280 for (i = 0; i < 256; i++)
281 {
282 if (table->table[i].exists != 1 || table->table[i].not_defined)
283 printf("/* %02X */ UNICODE_REPLACEMENT_CHARACTER,\n", i);
284 else
285 printf("/* %02X */ 0x%04X /* %45s */,\n",
286 i, table->table[i].equivilant, table->table[i].name);
287 }
288 printf("};\n");
289 }
290
291
292 void print_indexed_table(char *name, TABLE* table,
293 unsigned short replacement)
294 {
295 unsigned int index;
296 unsigned int codepoint;
297 unsigned short upper_byte[256];
298 int i;
299
300 printf("static const USHORT %s_mapping_array[] = {\n",
301 name);
302 for (index = 0; index < 256; index++)
303 {
304 if (replacement == UNICODE_REPLACEMENT_CHARACTER)
305 printf("/* U+XX%02X */\tUNICODE_REPLACEMENT_CHARACTER,\t/* %d */\n",
306 index % 256, index);
307 else
308 printf("/* U+XX%02X */\tCANT_MAP_CHARACTER,\t/* %d */\n",
309 index % 256, index);
310 }
311
312 memset(upper_byte, 0, sizeof(upper_byte));
313 codepoint = 0;
314 while (codepoint < 0xFFFF + 1)
315 {
316 if (!table->table[codepoint].exists)
317 {
318 codepoint++;
319 continue;
320 }
321 upper_byte[codepoint / 256] = index;
322 while ((index % 256) < (codepoint % 256))
323 {
324 if (replacement == UNICODE_REPLACEMENT_CHARACTER)
325 printf("/* U+%04X */\tUNICODE_REPLACEMENT_CHARACTER,\t/* %d */\n",
326 (codepoint & 0xFF00) + (index % 256), index);
327 else
328 printf("/* U+%04X */\tCANT_MAP_CHARACTER,\t/* %d */\n",
329 (codepoint & 0xFF00) + (index % 256), index);
330 index++;
331 }
332 do {
333 if (!table->table[codepoint].exists)
334 {
335 if (replacement == UNICODE_REPLACEMENT_CHARACTER)
336 printf("/* U+%04X */\tUNICODE_REPLACEMENT_CHARACTER,\t/* %d */\n",
337 codepoint, index);
338 else
339 printf("/* U+%04X */\tCANT_MAP_CHARACTER,\t/* %d */\n",
340 codepoint, index);
341 }
342 else
343 printf("/* U+%04X */\t0x%04X\t/* %45s */,/* %d */\n",
344 codepoint, table->table[codepoint].equivilant,
345 table->table[codepoint].name, index);
346 index++;
347 codepoint++;
348 } while (index % 256);
349 }
350 printf("\t0 /* END OF MAP TABLE */\n");
351 printf("};\n");
352
353 printf("static const USHORT %s_map[256] = {\n", name);
354 for (i = 0; i < 256; i++)
355 printf("/* U+%02X-- */\t%d,\n", i, upper_byte[i]);
356 printf("};\n");
357
358 }
359
360
361 void print_condensed_indexed_table(char *name,
362 TABLE* table, unsigned short replacement)
363 {
364 unsigned int index;
365 unsigned int codepoint;
366 unsigned short upper_byte[256];
367 int i;
368
369 printf("static const USHORT %s_mapping_array[] = {\n",
370 name);
371
372 printf("\n");
373 printf("/* %5d to %5d */\n", 0, 255);
374 for (index = 0; index < 256; index++)
375 {
376 if (replacement)
377 printf("0x%04X,", replacement);
378 else
379 printf("0, ");
380 if (index % 8 == 7)
381 printf("\n");
382 else
383 printf(" ");
384 }
385
386 memset(upper_byte, 0, sizeof(upper_byte));
387 codepoint = 0;
388 while (codepoint < 0xFFFF + 1)
389 {
390 if (!table->table[codepoint].exists)
391 {
392 codepoint++;
393 continue;
394 }
395 upper_byte[codepoint / 256] = index;
396
397 printf("\n");
398 printf("/* %5d to %5d */\n", index, index + 255);
399
400 while ((index % 256) < (codepoint % 256))
401 {
402 if (replacement)
403 printf("0x%04X,", replacement);
404 else
405 printf("0, ");
406 if (index % 8 == 7)
407 printf("\n");
408 else
409 printf(" ");
410 index++;
411 }
412 do {
413 if (!table->table[codepoint].exists)
414 {
415 if (replacement)
416 printf("0x%04X,", replacement);
417 else
418 printf("0, ");
419 }
420 else
421 printf("0x%04X,", table->table[codepoint].equivilant);
422 if (index % 8 == 7)
423 printf("\n");
424 else
425 printf(" ");
426 index++;
427 codepoint++;
428 } while (index % 256);
429 }
430 printf("\t0 /* END OF MAP TABLE */\n");
431 printf("};\n");
432
433 printf("static const USHORT %s_map[256] = {\n", name);
434 for (i = 0; i < 256; i++)
435 printf("%d,\n", upper_byte[i]);
436 printf("};\n");
437
438 }