Bug 468575 - Scrape some gunk off the config/ grout, r=ted
[wine-gecko.git] / intl / uconv / tools / umaptable.c
blob2963e439cf8ed0164e2fb96d4edc0c9f553eddea
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozilla.org Code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
38 #include <stdio.h>
39 typedef short int16;
40 typedef unsigned short uint16;
42 #define NOMAPPING 0xfffd
44 typedef struct {
45 uint16 srcBegin; /* 2 byte */
46 uint16 srcEnd; /* 2 byte */
47 uint16 destBegin; /* 2 byte */
48 } uFormat0;
50 typedef struct {
51 uint16 srcBegin; /* 2 byte */
52 uint16 srcEnd; /* 2 byte */
53 uint16 mappingOffset; /* 2 byte */
54 } uFormat1;
56 typedef struct {
57 uint16 srcBegin; /* 2 byte */
58 uint16 srcEnd; /* 2 byte -waste */
59 uint16 destBegin; /* 2 byte */
60 } uFormat2;
62 typedef struct {
63 union {
64 uFormat0 format0;
65 uFormat1 format1;
66 uFormat2 format2;
67 } fmt;
68 } uMapCell;
70 /* =================================================
71 uTable
72 ================================================= */
73 typedef struct {
74 uint16 itemOfList;
75 uint16 offsetToFormatArray;
76 uint16 offsetToMapCellArray;
77 uint16 offsetToMappingTable;
78 uint16 data[1];
79 } uTable;
81 uint16 umap[256][256];
82 int bInitFromOrTo = 0;
83 int bGenerateFromUnicodeTable = 0;
85 #define MAXCELLNUM 1000
87 static int numOfItem = 0;
88 uMapCell cell[MAXCELLNUM];
89 uint16 format[MAXCELLNUM / 4];
90 uint16 mapping[256*256];
91 static int mappinglen = 0;
92 static int formatcount[4] = {0,0,0,0};
94 #define SetFormat(n,f) { format[(n >> 2)] |= ((f) << ((n & 0x0003) << 2)); formatcount[f]++; }
95 #define GetFormat(n) ( format[(n >> 2)] >> ((n & 0x0003) << 2)) &0x00FF)
96 #define MAPVALUE(i) (umap[(i >> 8) & 0xFF][(i) & 0xFF])
98 int FORMAT1CNST = 10 ;
99 int FORMAT0CNST = 5 ;
100 void initmaps()
102 int i,j;
103 for(i=0;i<256;i++)
104 for(j=0;j<256;j++)
106 umap[i][j]= NOMAPPING;
108 for(i=0;i<MAXCELLNUM / 4;i++)
109 format[i]=0;
111 void SetMapValue(short u,short c)
113 if(NOMAPPING == MAPVALUE(u))
114 MAPVALUE(u) = c & 0x0000FFFF;
115 else {
116 fprintf(stderr, "warning- duplicate mapping %x map to both %x and %x\n", u, MAPVALUE(u), c);
119 void AddFormat2(uint16 srcBegin)
121 uint16 destBegin = MAPVALUE(srcBegin);
122 printf("Begin of Item %04X\n",numOfItem);
123 printf(" Format 2\n");
124 printf(" srcBegin = %04X\n", srcBegin);
125 printf(" destBegin = %04X\n", destBegin );
126 SetFormat(numOfItem,2);
127 cell[numOfItem].fmt.format2.srcBegin = srcBegin;
128 cell[numOfItem].fmt.format2.srcEnd = 0;
129 cell[numOfItem].fmt.format2.destBegin = destBegin;
130 printf("End of Item %04X \n\n",numOfItem);
131 numOfItem++;
132 /* Unmark the umap */
133 MAPVALUE(srcBegin) = NOMAPPING;
135 void AddFormat1(uint16 srcBegin, uint16 srcEnd)
137 uint16 i;
138 printf("Begin of Item %04X\n",numOfItem);
139 printf(" Format 1\n");
140 printf(" srcBegin = %04X\n", srcBegin);
141 printf(" srcEnd = %04X\n", srcEnd );
142 printf(" mappingOffset = %04X\n", mappinglen);
143 printf(" Mapping = " );
144 SetFormat(numOfItem,1);
145 cell[numOfItem].fmt.format1.srcBegin = srcBegin;
146 cell[numOfItem].fmt.format1.srcEnd = srcEnd;
147 cell[numOfItem].fmt.format1.mappingOffset = mappinglen;
148 for(i=srcBegin ; i <= srcEnd ; i++,mappinglen++)
150 if( ((i-srcBegin) % 8) == 0)
151 printf("\n ");
152 mapping[mappinglen]= MAPVALUE(i);
153 printf("%04X ",(mapping[mappinglen] ));
154 /* Unmark the umap */
155 MAPVALUE(i) = NOMAPPING;
157 printf("\n");
158 printf("End of Item %04X \n\n",numOfItem);
159 numOfItem++;
161 void AddFormat0(uint16 srcBegin, uint16 srcEnd)
163 uint16 i;
164 uint16 destBegin = MAPVALUE(srcBegin);
165 printf("Begin of Item %04X\n",numOfItem);
166 printf(" Format 0\n");
167 printf(" srcBegin = %04X\n", srcBegin);
168 printf(" srcEnd = %04X\n", srcEnd );
169 printf(" destBegin = %04X\n", destBegin );
170 SetFormat(numOfItem,0);
171 cell[numOfItem].fmt.format0.srcBegin = srcBegin;
172 cell[numOfItem].fmt.format0.srcEnd = srcEnd;
173 cell[numOfItem].fmt.format0.destBegin = destBegin;
174 for(i=srcBegin ; i <= srcEnd ; i++)
176 /* Unmark the umap */
177 MAPVALUE(i) = NOMAPPING;
179 printf("End of Item %04X \n\n",numOfItem);
180 numOfItem++;
182 void printnpl()
184 printf(
185 "/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */\n"
186 "/* ***** BEGIN LICENSE BLOCK *****\n"
187 " * Version: MPL 1.1/GPL 2.0/LGPL 2.1\n"
188 " *\n"
189 " * The contents of this file are subject to the Mozilla Public License Version\n"
190 " * 1.1 (the \"License\"); you may not use this file except in compliance with\n"
191 " * the License. You may obtain a copy of the License at\n"
192 " * http://www.mozilla.org/MPL/\n"
193 " *\n"
194 " * Software distributed under the License is distributed on an \"AS IS\" basis,\n"
195 " * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\n"
196 " * for the specific language governing rights and limitations under the\n"
197 " * License.\n"
198 " *\n"
199 " * The Original Code is mozilla.org code.\n"
200 " *\n"
201 " * The Initial Developer of the Original Code is\n"
202 " * Netscape Communications Corporation.\n"
203 " * Portions created by the Initial Developer are Copyright (C) 2001\n"
204 " * the Initial Developer. All Rights Reserved.\n"
205 " *\n"
206 " * Contributor(s):\n"
207 " *\n"
208 " * Alternatively, the contents of this file may be used under the terms of\n"
209 " * either the GNU General Public License Version 2 or later (the \"GPL\"), or\n"
210 " * the GNU Lesser General Public License Version 2.1 or later (the \"LGPL\"),\n"
211 " * in which case the provisions of the GPL or the LGPL are applicable instead\n"
212 " * of those above. If you wish to allow use of your version of this file only\n"
213 " * under the terms of either the GPL or the LGPL, and not to allow others to\n"
214 " * use your version of this file under the terms of the MPL, indicate your\n"
215 " * decision by deleting the provisions above and replace them with the notice\n"
216 " * and other provisions required by the GPL or the LGPL. If you do not delete\n"
217 " * the provisions above, a recipient may use your version of this file under\n"
218 " * the terms of any one of the MPL, the GPL or the LGPL.\n"
219 " *\n"
220 " * ***** END LICENSE BLOCK ***** */\n");
222 void gentable()
224 /* OK! For now, we just use format 1 for each row */
225 /* We need to chage this to use other format to save the space */
226 uint16 begin,end;
227 uint16 ss,gs,gp,state,gc;
228 uint16 diff, lastdiff;
230 printnpl();
231 printf("/*========================================================\n");
232 printf(" This is a Generated file. Please don't edit it.\n");
233 printf("\n");
234 printf(" The tool which used to generate this file is called umaptable.\n");
235 printf(" You can find this tool under mozilla/intl/uconv/tools/umaptable.c.\n");
237 printf(" If you have any problem of this file. Please contact \n");
238 printf(" Netscape Client International Team or \n");
239 printf(" ftang@netscape <Frank Tang> \n");
240 printf("\n");
241 printf(" Table in Debug form \n");
243 for(begin = 0; MAPVALUE(begin) ==NOMAPPING; begin++)
245 for(end = 0xFFFF; MAPVALUE(end) ==NOMAPPING; end--)
247 if(end != begin)
249 lastdiff = MAPVALUE(begin) - begin;
250 for(gp=begin+1,state = 0 ; gp<=end; gp++)
252 int input ;
253 diff = MAPVALUE(gp) - gp;
254 input = (diff == lastdiff);
255 switch(state)
257 case 0:
258 if(input)
260 state = 1;
261 ss = gp -1;
262 gc = 2;
264 break;
265 case 1:
266 if(input)
268 if(gc++ >= FORMAT0CNST)
270 state = 2;
273 else
275 state = 0;
277 break;
278 case 2:
279 if(input)
282 else
284 AddFormat0(ss,gp-1);
285 state = 0;
287 break;
290 lastdiff = diff;
293 if(state == 2)
294 AddFormat0(ss,end);
296 for(;(MAPVALUE(begin) ==NOMAPPING) && (begin <= end); begin++)
298 if(begin <= end)
300 for(;(MAPVALUE(end)==NOMAPPING) && (end >= begin); end--)
302 for(ss=gp=begin,state = 0 ; gp<=end; gp++)
304 int input = (MAPVALUE(gp) == NOMAPPING);
305 switch(state)
307 case 0:
308 if(input)
310 gc = 1;
311 gs = gp;
312 state = 1;
314 break;
315 case 1:
316 if(input)
318 if(gc++ >= FORMAT1CNST)
319 state = 2;
321 else
322 state = 0;
323 break;
324 case 2:
325 if(input)
328 else
330 if(gs == (ss+1))
331 AddFormat2(ss);
332 else
333 AddFormat1(ss ,gs-1);
334 state = 0;
335 ss = gp;
337 break;
340 if(end == ss)
341 AddFormat2(ss );
342 else
343 AddFormat1(ss ,end );
345 printf("========================================================*/\n");
347 void writetable()
349 uint16 i;
350 uint16 off1,off2,off3;
351 uint16 cur = 0;
352 uint16 formatitem = (((numOfItem)>>2) + 1);
353 off1 = 4;
354 off2 = off1 + formatitem ;
355 off3 = off2 + numOfItem * sizeof(uMapCell) / sizeof(uint16);
356 /* write itemOfList */
357 printf("/* Offset=0x%04X ItemOfList */\n 0x%04X,\n", cur++, numOfItem);
359 /* write offsetToFormatArray */
360 printf("/*-------------------------------------------------------*/\n");
361 printf("/* Offset=0x%04X offsetToFormatArray */\n 0x%04X,\n", cur++,off1);
363 /* write offsetToMapCellArray */
364 printf("/*-------------------------------------------------------*/\n");
365 printf("/* Offset=0x%04X offsetToMapCellArray */ \n 0x%04X,\n", cur++,off2);
367 /* write offsetToMappingTable */
368 printf("/*-------------------------------------------------------*/\n");
369 printf("/* Offset=0x%04X offsetToMappingTable */ \n 0x%04X,\n", cur++,off3);
371 /* write FormatArray */
372 printf("/*-------------------------------------------------------*/\n");
373 printf("/* Offset=0x%04X Start of Format Array */ \n",cur);
374 printf("/* Total of Format 0 : 0x%04X */\n"
375 , formatcount[0]);
376 printf("/* Total of Format 1 : 0x%04X */\n"
377 , formatcount[1]);
378 printf("/* Total of Format 2 : 0x%04X */\n"
379 , formatcount[2]);
380 printf("/* Total of Format 3 : 0x%04X */\n"
381 , formatcount[3]);
382 for(i=0;i<formatitem;i++,cur++)
384 if((i%8) == 0)
385 printf("\n");
386 printf("0x%04X, ",format[i]);
388 printf("\n");
390 /* write MapCellArray */
391 printf("/*-------------------------------------------------------*/\n");
392 printf("/* Offset=0x%04X Start of MapCell Array */ \n",cur);
393 for(i=0;i<numOfItem;i++,cur+=3)
395 printf("/* %04X */ 0x%04X, 0x%04X, 0x%04X, \n",
397 cell[i].fmt.format0.srcBegin,
398 cell[i].fmt.format0.srcEnd,
399 cell[i].fmt.format0.destBegin
403 /* write MappingTable */
404 printf("/*-------------------------------------------------------*/\n");
405 printf("/* Offset=0x%04X Start of MappingTable */ \n",cur);
406 for(i=0;i<mappinglen;i++,cur++)
408 if((i%8) == 0)
409 printf("\n/* %04X */ ",i);
410 printf("0x%04X, ",mapping[i] );
412 printf("\n");
413 printf("/* End of table Total Length = 0x%04X * 2 */\n",cur);
416 void usage()
418 fprintf(stderr, "please indicate what kind of mapping mapping table you want to generate:\n");
419 fprintf(stderr, "\t-uf : generate *.uf (from unicode) table, or\n");
420 fprintf(stderr, "\t-ut : generate *.ut (to unicode) table\n");
422 parsearg(int argc, char* argv[])
424 int i;
425 for(i=0;i<argc;i++)
427 if(strncmp("-uf", argv[i],3) == 0) {
428 if(! bInitFromOrTo) {
429 bGenerateFromUnicodeTable = 1;
430 bInitFromOrTo = 1;
431 } else {
432 usage();
433 exit(-1);
436 if(strncmp("-ut", argv[i],3) == 0) {
437 if(! bInitFromOrTo) {
438 bGenerateFromUnicodeTable = 0;
439 bInitFromOrTo = 1;
440 } else {
441 usage();
442 exit(-1);
445 if((strncmp("-0", argv[i],2) == 0) && ((i+1) < argc))
447 int cnst0;
448 if(sscanf(argv[i+1], "%d", &cnst0) == 1)
450 if(cnst0 > 0)
452 FORMAT0CNST = cnst0;
455 else
457 fprintf(stderr, "argc error !!!!\n");
458 exit(-1);
460 i++;
462 if((strncmp("-1", argv[i],2) == 0) && ((i+1) < argc))
464 int cnst1;
465 if(sscanf(argv[i+1], "%d", &cnst1) == 1)
467 if(cnst1 > 0)
469 FORMAT1CNST = cnst1;
472 else
474 fprintf(stderr, "argc error !!!!\n");
475 exit(-1);
477 i++;
480 if(! bInitFromOrTo)
482 usage();
483 exit(-1);
485 fprintf(stderr, "format 0 cnst = %d\n", FORMAT0CNST);
486 fprintf(stderr, "format 1 cnst = %d\n", FORMAT1CNST);
487 fprintf(stderr, "generate u%c table\n",
488 bGenerateFromUnicodeTable ? 'f' : 't');
490 void getinput()
492 char buf[256];
493 short c,u;
494 for(;gets(buf)!=NULL;)
496 if(buf[0]=='0' && buf[1] == 'x')
498 sscanf(buf,"%hx %hx",&c,&u);
499 if(bGenerateFromUnicodeTable)
500 SetMapValue(u, c);
501 else
502 SetMapValue(c, u);
506 main(int argc, char* argv[])
508 parsearg(argc, argv);
509 initmaps();
510 getinput();
511 gentable();
512 writetable();