~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/intl/uconv/tools/umaptable.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Netscape Public License
 
4
 * Version 1.0 (the "NPL"); you may not use this file except in
 
5
 * compliance with the NPL.  You may obtain a copy of the NPL at
 
6
 * http://www.mozilla.org/NPL/
 
7
 *
 
8
 * Software distributed under the NPL is distributed on an "AS IS" basis,
 
9
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
 
10
 * for the specific language governing rights and limitations under the
 
11
 * NPL.
 
12
 *
 
13
 * The Initial Developer of this code under the NPL is Netscape
 
14
 * Communications Corporation.  Portions created by Netscape are
 
15
 * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
 
16
 * Reserved.
 
17
 */
 
18
#include <stdio.h>
 
19
typedef short int16;
 
20
typedef unsigned short uint16;
 
21
 
 
22
#define NOMAPPING 0xfffd
 
23
 
 
24
typedef struct {
 
25
                uint16 srcBegin;                /* 2 byte       */
 
26
                uint16 srcEnd;                  /* 2 byte       */
 
27
                uint16 destBegin;               /* 2 byte       */
 
28
} uFormat0;
 
29
 
 
30
typedef struct {
 
31
                uint16 srcBegin;                /* 2 byte       */
 
32
                uint16 srcEnd;                  /* 2 byte       */
 
33
                uint16  mappingOffset;  /* 2 byte       */
 
34
} uFormat1;
 
35
 
 
36
typedef struct {
 
37
                uint16 srcBegin;                /* 2 byte       */
 
38
                uint16 srcEnd;                  /* 2 byte       -waste  */
 
39
                uint16 destBegin;               /* 2 byte       */
 
40
} uFormat2;
 
41
 
 
42
typedef struct  {
 
43
        union {
 
44
                uFormat0        format0;
 
45
                uFormat1        format1;
 
46
                uFormat2        format2;
 
47
        } fmt;
 
48
} uMapCell;
 
49
 
 
50
/* =================================================
 
51
                                        uTable 
 
52
================================================= */
 
53
typedef struct  {
 
54
        uint16          itemOfList;
 
55
        uint16          offsetToFormatArray;
 
56
        uint16          offsetToMapCellArray;
 
57
        uint16          offsetToMappingTable;
 
58
        uint16          data[1];
 
59
} uTable;
 
60
 
 
61
uint16 umap[256][256];
 
62
int bInitFromOrTo = 0;
 
63
int bGenerateFromUnicodeTable = 0;
 
64
 
 
65
#define MAXCELLNUM 1000
 
66
 
 
67
static int numOfItem = 0;
 
68
uMapCell cell[MAXCELLNUM];
 
69
uint16    format[MAXCELLNUM / 4];
 
70
uint16   mapping[256*256];
 
71
static int mappinglen  = 0;
 
72
static int formatcount[4] = {0,0,0,0}; 
 
73
 
 
74
#define SetFormat(n,f)          { format[(n >> 2)] |= ((f) << ((n & 0x0003)     << 2)); formatcount[f]++; }
 
75
#define GetFormat(n)            ( format[(n >> 2)] >> ((n & 0x0003)     << 2)) &0x00FF)
 
76
#define MAPVALUE(i)     (umap[(i >> 8) & 0xFF][(i) & 0xFF])
 
77
 
 
78
int  FORMAT1CNST = 10 ;
 
79
int  FORMAT0CNST = 5 ;
 
80
void initmaps()
 
81
{
 
82
        int i,j;
 
83
        for(i=0;i<256;i++)
 
84
                for(j=0;j<256;j++) 
 
85
                {
 
86
                        umap[i][j]=   NOMAPPING;
 
87
                }
 
88
        for(i=0;i<MAXCELLNUM / 4;i++)
 
89
                format[i]=0;
 
90
}
 
91
void SetMapValue(short u,short c)
 
92
{
 
93
        if(NOMAPPING == MAPVALUE(u))
 
94
           MAPVALUE(u) = c & 0x0000FFFF;
 
95
        else {
 
96
           fprintf(stderr, "warning- duplicate mapping %x map to both %x and %x\n", u, MAPVALUE(u), c);
 
97
        }
 
98
}
 
99
void AddFormat2(uint16 srcBegin)
 
100
{
 
101
        uint16 destBegin = MAPVALUE(srcBegin);
 
102
        printf("Begin of Item %04X\n",numOfItem);
 
103
        printf(" Format 2\n");
 
104
        printf("  srcBegin = %04X\n", srcBegin);
 
105
        printf("  destBegin = %04X\n", destBegin );
 
106
        SetFormat(numOfItem,2);
 
107
        cell[numOfItem].fmt.format2.srcBegin = srcBegin;
 
108
        cell[numOfItem].fmt.format2.srcEnd = 0;
 
109
        cell[numOfItem].fmt.format2.destBegin = destBegin;
 
110
        printf("End of Item %04X \n\n",numOfItem);
 
111
        numOfItem++;
 
112
        /*      Unmark the umap */
 
113
        MAPVALUE(srcBegin) = NOMAPPING;
 
114
}
 
115
void AddFormat1(uint16 srcBegin, uint16 srcEnd)
 
116
{
 
117
        uint16 i;
 
118
        printf("Begin of Item %04X\n",numOfItem);
 
119
        printf(" Format 1\n");
 
120
        printf("  srcBegin = %04X\n", srcBegin);
 
121
        printf("  srcEnd = %04X\n", srcEnd );
 
122
        printf("  mappingOffset = %04X\n", mappinglen);
 
123
        printf(" Mapping  = " );  
 
124
        SetFormat(numOfItem,1);
 
125
        cell[numOfItem].fmt.format1.srcBegin = srcBegin;
 
126
        cell[numOfItem].fmt.format1.srcEnd = srcEnd;
 
127
        cell[numOfItem].fmt.format1.mappingOffset = mappinglen;
 
128
        for(i=srcBegin ; i <= srcEnd ; i++,mappinglen++)
 
129
        {
 
130
                if( ((i-srcBegin) % 8) == 0)
 
131
                        printf("\n  ");
 
132
                mapping[mappinglen]= MAPVALUE(i);
 
133
                printf("%04X ",(mapping[mappinglen]  ));
 
134
                /*      Unmark the umap */
 
135
                MAPVALUE(i) = NOMAPPING;
 
136
        }
 
137
        printf("\n");
 
138
        printf("End of Item %04X \n\n",numOfItem);
 
139
        numOfItem++;
 
140
}
 
141
void AddFormat0(uint16 srcBegin, uint16 srcEnd)
 
142
{
 
143
        uint16 i;
 
144
        uint16 destBegin = MAPVALUE(srcBegin);
 
145
        printf("Begin of Item %04X\n",numOfItem);
 
146
        printf(" Format 0\n");
 
147
        printf("  srcBegin = %04X\n", srcBegin);
 
148
        printf("  srcEnd = %04X\n", srcEnd );
 
149
        printf("  destBegin = %04X\n", destBegin );
 
150
        SetFormat(numOfItem,0);
 
151
        cell[numOfItem].fmt.format0.srcBegin = srcBegin;
 
152
        cell[numOfItem].fmt.format0.srcEnd = srcEnd;
 
153
        cell[numOfItem].fmt.format0.destBegin = destBegin;
 
154
        for(i=srcBegin ; i <= srcEnd ; i++)
 
155
        {
 
156
                /*      Unmark the umap */
 
157
                MAPVALUE(i) = NOMAPPING;
 
158
        }
 
159
        printf("End of Item %04X \n\n",numOfItem);
 
160
        numOfItem++;
 
161
}
 
162
void printnpl()
 
163
{
 
164
printf(
 
165
"/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-\n"
 
166
"*\n"
 
167
"* The contents of this file are subject to the Netscape Public License\n"
 
168
"* Version 1.0 (the \"NPL\"); you may not use this file except in\n"
 
169
"* compliance with the NPL.  You may obtain a copy of the NPL at\n"
 
170
"* http://www.mozilla.org/NPL/\n"
 
171
"*\n"
 
172
"* Software distributed under the NPL is distributed on an \"AS IS\" basis, \n"
 
173
"* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL \n"
 
174
"* for the specific language governing rights and limitations under the \n"
 
175
"* NPL. \n"
 
176
"*\n"
 
177
"* The Initial Developer of this code under the NPL is Netscape\n"
 
178
"* Communications Corporation.  Portions created by Netscape are\n"
 
179
"* Copyright (C) 2001 Netscape Communications Corporation.  All Rights\n"
 
180
"* Reserved.\n"
 
181
"*/\n ");
 
182
}
 
183
void gentable()
 
184
{
 
185
        /*      OK! For now, we just use format 1 for each row */
 
186
        /*      We need to chage this to use other format to save the space */
 
187
        uint16 begin,end;
 
188
        uint16 ss,gs,gp,state,gc;       
 
189
        uint16 diff, lastdiff;
 
190
 
 
191
        printnpl();
 
192
        printf("/*========================================================\n");
 
193
        printf("  This is a Generated file. Please don't edit it.\n");
 
194
        printf("\n");
 
195
        printf("  The tool which used to generate this file is called umaptable.\n");
 
196
        printf("  You can find this tool under mozilla/intl/uconv/tools/umaptable.c.\n");
 
197
 
 
198
        printf("  If you have any problem of this file. Please contact \n"); 
 
199
        printf("  Netscape Client International Team or \n");
 
200
        printf("  ftang@netscape <Frank Tang> \n");
 
201
        printf("\n");
 
202
        printf("              Table in Debug form \n");
 
203
 
 
204
        for(begin = 0; MAPVALUE(begin) ==NOMAPPING; begin++)
 
205
                ;
 
206
        for(end = 0xFFFF; MAPVALUE(end) ==NOMAPPING; end--)
 
207
                ;
 
208
        if(end != begin)
 
209
        {
 
210
           lastdiff = MAPVALUE(begin) - begin; 
 
211
                for(gp=begin+1,state = 0 ; gp<=end; gp++)
 
212
                {
 
213
                        int input ;
 
214
                diff = MAPVALUE(gp) - gp; 
 
215
                   input = (diff == lastdiff);
 
216
                        switch(state)
 
217
                        {
 
218
                                case 0: 
 
219
                                        if(input)
 
220
                                        {
 
221
                                                state = 1;
 
222
                                           ss =  gp -1;
 
223
                                           gc = 2;
 
224
                                        }
 
225
                                        break;
 
226
                           case 1:
 
227
                                        if(input)
 
228
                                        {
 
229
                                                if(gc++ >= FORMAT0CNST)
 
230
                                                {
 
231
                                                        state = 2;
 
232
                                                }
 
233
                                        }
 
234
                                        else
 
235
                                        {
 
236
                                                state = 0;
 
237
                                        }
 
238
                                        break;
 
239
                           case 2:
 
240
                                        if(input)
 
241
                                        {
 
242
                                        }
 
243
                                        else
 
244
                                        {
 
245
                                           AddFormat0(ss,gp-1);
 
246
                                                state = 0;
 
247
                                        }
 
248
                                        break;
 
249
                        }
 
250
                        
 
251
                   lastdiff = diff;
 
252
                }       
 
253
        }
 
254
        if(state == 2)
 
255
                AddFormat0(ss,end);
 
256
 
 
257
        for(;(MAPVALUE(begin) ==NOMAPPING) && (begin <= end); begin++)
 
258
                ;
 
259
 if(begin <= end)
 
260
 {
 
261
                for(;(MAPVALUE(end)==NOMAPPING) && (end >= begin); end--)
 
262
                        ;
 
263
                for(ss=gp=begin,state = 0 ; gp<=end; gp++)
 
264
                {
 
265
                        int input = (MAPVALUE(gp) == NOMAPPING);
 
266
                        switch(state)
 
267
                        {
 
268
                        case 0:
 
269
                                if(input)
 
270
                                {
 
271
                                        gc = 1;
 
272
                                        gs = gp;
 
273
                                        state = 1;
 
274
                        }
 
275
                                break;
 
276
                        case 1:
 
277
                                if(input)
 
278
                                {
 
279
                                        if(gc++ >= FORMAT1CNST)
 
280
                                                state = 2;
 
281
                                }
 
282
                                else            
 
283
                                        state = 0;
 
284
                                break;
 
285
                        case 2:
 
286
                                if(input)
 
287
                                {               
 
288
                                }
 
289
                                else
 
290
                                {
 
291
                                if(gs == (ss+1))
 
292
                                                AddFormat2(ss); 
 
293
                                        else
 
294
                                                AddFormat1(ss ,gs-1);   
 
295
                                        state = 0;
 
296
                                        ss = gp;
 
297
                                }
 
298
                                                break;
 
299
                                        }
 
300
                                }
 
301
                                if(end == ss)
 
302
                                        AddFormat2(ss );        
 
303
                                else
 
304
                                        AddFormat1(ss ,end );   
 
305
        }
 
306
        printf("========================================================*/\n");
 
307
}
 
308
void writetable()
 
309
{
 
310
        uint16 i;
 
311
        uint16 off1,off2,off3;
 
312
        uint16 cur = 0; 
 
313
        uint16 formatitem = (((numOfItem)>>2) + 1);
 
314
        off1 = 4;
 
315
        off2 = off1 + formatitem ;
 
316
        off3 = off2 + numOfItem * sizeof(uMapCell) / sizeof(uint16);
 
317
        /*      write itemOfList                */
 
318
        printf("/* Offset=0x%04X  ItemOfList */\n  0x%04X,\n", cur++, numOfItem);
 
319
 
 
320
        /*      write offsetToFormatArray       */
 
321
        printf("/*-------------------------------------------------------*/\n");
 
322
        printf("/* Offset=0x%04X  offsetToFormatArray */\n  0x%04X,\n",  cur++,off1);
 
323
 
 
324
        /*      write offsetToMapCellArray      */
 
325
        printf("/*-------------------------------------------------------*/\n");
 
326
        printf("/* Offset=0x%04X  offsetToMapCellArray */ \n  0x%04X,\n",  cur++,off2);
 
327
 
 
328
        /*      write offsetToMappingTable      */
 
329
        printf("/*-------------------------------------------------------*/\n");
 
330
        printf("/* Offset=0x%04X  offsetToMappingTable */ \n  0x%04X,\n", cur++,off3);
 
331
 
 
332
        /*      write FormatArray               */
 
333
        printf("/*-------------------------------------------------------*/\n");
 
334
        printf("/*       Offset=0x%04X   Start of Format Array */ \n",cur);
 
335
        printf("/*      Total of Format 0 : 0x%04X                       */\n"
 
336
                        , formatcount[0]);      
 
337
        printf("/*      Total of Format 1 : 0x%04X                       */\n"
 
338
                        , formatcount[1]);      
 
339
        printf("/*      Total of Format 2 : 0x%04X                       */\n"
 
340
                        , formatcount[2]);      
 
341
        printf("/*      Total of Format 3 : 0x%04X                       */\n"
 
342
                        , formatcount[3]);      
 
343
        for(i=0;i<formatitem;i++,cur++)
 
344
        {
 
345
                if((i%8) == 0)  
 
346
                        printf("\n");
 
347
                printf("0x%04X, ",format[i]);
 
348
        }
 
349
        printf("\n");
 
350
 
 
351
        /*      write MapCellArray              */
 
352
        printf("/*-------------------------------------------------------*/\n");
 
353
        printf("/*       Offset=0x%04X   Start of MapCell Array */ \n",cur);
 
354
        for(i=0;i<numOfItem;i++,cur+=3)
 
355
        {
 
356
                printf("/* %04X */    0x%04X, 0x%04X, 0x%04X, \n", 
 
357
                        i,
 
358
                        cell[i].fmt.format0.srcBegin,
 
359
                        cell[i].fmt.format0.srcEnd,
 
360
                        cell[i].fmt.format0.destBegin
 
361
                );
 
362
        }
 
363
 
 
364
        /*      write MappingTable              */
 
365
        printf("/*-------------------------------------------------------*/\n");
 
366
        printf("/*       Offset=0x%04X   Start of MappingTable */ \n",cur);
 
367
        for(i=0;i<mappinglen;i++,cur++)
 
368
        {
 
369
                if((i%8) == 0)  
 
370
                        printf("\n/* %04X */    ",i);
 
371
                printf("0x%04X, ",mapping[i] );
 
372
        }
 
373
        printf("\n");
 
374
        printf("/*      End of table Total Length = 0x%04X * 2 */\n",cur);
 
375
}
 
376
 
 
377
void usage()
 
378
{
 
379
  fprintf(stderr, "please indicate what kind of mapping mapping table you want to generate:\n");
 
380
  fprintf(stderr, "\t-uf : generate *.uf (from unicode) table, or\n");
 
381
  fprintf(stderr, "\t-ut : generate *.ut (to unicode) table\n");
 
382
}
 
383
parsearg(int argc, char* argv[])
 
384
{
 
385
        int i;
 
386
        for(i=0;i<argc;i++)
 
387
        {
 
388
                if(strncmp("-uf", argv[i],3) == 0) {
 
389
                        if(! bInitFromOrTo) {
 
390
                           bGenerateFromUnicodeTable = 1;
 
391
                           bInitFromOrTo = 1;
 
392
                        } else {
 
393
                           usage();
 
394
                           exit(-1);
 
395
                        }
 
396
                } 
 
397
                if(strncmp("-ut", argv[i],3) == 0) {
 
398
                        if(! bInitFromOrTo) {
 
399
                           bGenerateFromUnicodeTable = 0;
 
400
                           bInitFromOrTo = 1;
 
401
                        } else {
 
402
                           usage();
 
403
                           exit(-1);
 
404
                        }
 
405
                } 
 
406
                if((strncmp("-0", argv[i],2) == 0) && ((i+1) < argc))
 
407
                {
 
408
                        int cnst0;
 
409
                        if(sscanf(argv[i+1], "%d", &cnst0) == 1)
 
410
                        {
 
411
                                if(cnst0 > 0)
 
412
                                {
 
413
                                        FORMAT0CNST = cnst0;
 
414
                                }
 
415
                        }
 
416
                        else
 
417
                        {
 
418
                                fprintf(stderr, "argc error !!!!\n");
 
419
                                exit(-1);
 
420
                        }
 
421
                        i++;
 
422
                }
 
423
                if((strncmp("-1", argv[i],2) == 0) && ((i+1) < argc))
 
424
                {
 
425
                        int cnst1;
 
426
                        if(sscanf(argv[i+1], "%d", &cnst1) == 1)
 
427
                        {
 
428
                                if(cnst1 > 0)
 
429
                                {
 
430
                                        FORMAT1CNST = cnst1;
 
431
                                }
 
432
                        }
 
433
                        else
 
434
                        {
 
435
                                fprintf(stderr, "argc error !!!!\n");
 
436
                                exit(-1);
 
437
                        }
 
438
                        i++;
 
439
                }
 
440
        }
 
441
        if(! bInitFromOrTo)
 
442
        {
 
443
                usage();
 
444
                exit(-1);
 
445
        }
 
446
        fprintf(stderr, "format 0 cnst = %d\n", FORMAT0CNST);
 
447
        fprintf(stderr, "format 1 cnst = %d\n", FORMAT1CNST);
 
448
        fprintf(stderr, "generate u%c table\n", 
 
449
                        bGenerateFromUnicodeTable ? 'f' : 't');
 
450
}
 
451
void getinput()
 
452
{
 
453
  char buf[256];
 
454
  short c,u;
 
455
  for(;gets(buf)!=NULL;)
 
456
  {
 
457
     if(buf[0]=='0' && buf[1] == 'x')
 
458
        {
 
459
          sscanf(buf,"%hx %hx",&c,&u);
 
460
          if(bGenerateFromUnicodeTable)
 
461
            SetMapValue(u, c);
 
462
          else
 
463
            SetMapValue(c, u);
 
464
        }
 
465
  }
 
466
}
 
467
main(int argc, char* argv[])
 
468
{
 
469
  parsearg(argc, argv);
 
470
  initmaps();
 
471
  getinput();
 
472
  gentable();
 
473
  writetable(); 
 
474
}