Back to index

lightning-sunbird  0.9+nobinonly
umaptable.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org Code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 #include <stdio.h>
00039 typedef short int16;
00040 typedef unsigned short uint16;
00041 
00042 #define NOMAPPING 0xfffd
00043 
00044 typedef struct {
00045               uint16 srcBegin;            /* 2 byte     */
00046               uint16 srcEnd;                     /* 2 byte     */
00047               uint16 destBegin;           /* 2 byte     */
00048 } uFormat0;
00049 
00050 typedef struct {
00051               uint16 srcBegin;            /* 2 byte     */
00052               uint16 srcEnd;                     /* 2 byte     */
00053               uint16 mappingOffset;       /* 2 byte     */
00054 } uFormat1;
00055 
00056 typedef struct {
00057               uint16 srcBegin;            /* 2 byte     */
00058               uint16 srcEnd;                     /* 2 byte     -waste */
00059               uint16 destBegin;           /* 2 byte     */
00060 } uFormat2;
00061 
00062 typedef struct  {
00063        union {
00064               uFormat0      format0;
00065               uFormat1      format1;
00066               uFormat2      format2;
00067        } fmt;
00068 } uMapCell;
00069 
00070 /* =================================================
00071                                    uTable 
00072 ================================================= */
00073 typedef struct  {
00074        uint16               itemOfList;
00075        uint16        offsetToFormatArray;
00076        uint16        offsetToMapCellArray;
00077        uint16        offsetToMappingTable;
00078        uint16        data[1];
00079 } uTable;
00080 
00081 uint16 umap[256][256];
00082 int bInitFromOrTo = 0;
00083 int bGenerateFromUnicodeTable = 0;
00084 
00085 #define MAXCELLNUM 1000
00086 
00087 static int numOfItem = 0;
00088 uMapCell cell[MAXCELLNUM];
00089 uint16    format[MAXCELLNUM / 4];
00090 uint16   mapping[256*256];
00091 static int mappinglen  = 0;
00092 static int formatcount[4] = {0,0,0,0}; 
00093 
00094 #define SetFormat(n,f)             { format[(n >> 2)] |= ((f) << ((n & 0x0003)      << 2)); formatcount[f]++; }
00095 #define GetFormat(n)        ( format[(n >> 2)] >> ((n & 0x0003)       << 2)) &0x00FF)
00096 #define MAPVALUE(i)  (umap[(i >> 8) & 0xFF][(i) & 0xFF])
00097 
00098 int  FORMAT1CNST = 10 ;
00099 int  FORMAT0CNST = 5 ;
00100 void initmaps()
00101 {
00102        int i,j;
00103        for(i=0;i<256;i++)
00104               for(j=0;j<256;j++) 
00105               {
00106                      umap[i][j]=   NOMAPPING;
00107               }
00108        for(i=0;i<MAXCELLNUM / 4;i++)
00109               format[i]=0;
00110 }
00111 void SetMapValue(short u,short c)
00112 {
00113         if(NOMAPPING == MAPVALUE(u))
00114           MAPVALUE(u) = c & 0x0000FFFF;
00115         else {
00116            fprintf(stderr, "warning- duplicate mapping %x map to both %x and %x\n", u, MAPVALUE(u), c);
00117         }
00118 }
00119 void AddFormat2(uint16 srcBegin)
00120 {
00121        uint16 destBegin = MAPVALUE(srcBegin);
00122        printf("Begin of Item %04X\n",numOfItem);
00123        printf(" Format 2\n");
00124        printf("  srcBegin = %04X\n", srcBegin);
00125        printf("  destBegin = %04X\n", destBegin );
00126        SetFormat(numOfItem,2);
00127        cell[numOfItem].fmt.format2.srcBegin = srcBegin;
00128        cell[numOfItem].fmt.format2.srcEnd = 0;
00129        cell[numOfItem].fmt.format2.destBegin = destBegin;
00130        printf("End of Item %04X \n\n",numOfItem);
00131        numOfItem++;
00132        /*     Unmark the umap */
00133        MAPVALUE(srcBegin) = NOMAPPING;
00134 }
00135 void AddFormat1(uint16 srcBegin, uint16 srcEnd)
00136 {
00137        uint16 i;
00138        printf("Begin of Item %04X\n",numOfItem);
00139        printf(" Format 1\n");
00140        printf("  srcBegin = %04X\n", srcBegin);
00141        printf("  srcEnd = %04X\n", srcEnd );
00142        printf("  mappingOffset = %04X\n", mappinglen);
00143        printf(" Mapping  = " );  
00144        SetFormat(numOfItem,1);
00145        cell[numOfItem].fmt.format1.srcBegin = srcBegin;
00146        cell[numOfItem].fmt.format1.srcEnd = srcEnd;
00147        cell[numOfItem].fmt.format1.mappingOffset = mappinglen;
00148        for(i=srcBegin ; i <= srcEnd ; i++,mappinglen++)
00149        {
00150               if( ((i-srcBegin) % 8) == 0)
00151                      printf("\n  ");
00152               mapping[mappinglen]= MAPVALUE(i);
00153               printf("%04X ",(mapping[mappinglen]  ));
00154               /*     Unmark the umap */
00155               MAPVALUE(i) = NOMAPPING;
00156        }
00157        printf("\n");
00158        printf("End of Item %04X \n\n",numOfItem);
00159        numOfItem++;
00160 }
00161 void AddFormat0(uint16 srcBegin, uint16 srcEnd)
00162 {
00163        uint16 i;
00164        uint16 destBegin = MAPVALUE(srcBegin);
00165        printf("Begin of Item %04X\n",numOfItem);
00166        printf(" Format 0\n");
00167        printf("  srcBegin = %04X\n", srcBegin);
00168        printf("  srcEnd = %04X\n", srcEnd );
00169        printf("  destBegin = %04X\n", destBegin );
00170        SetFormat(numOfItem,0);
00171        cell[numOfItem].fmt.format0.srcBegin = srcBegin;
00172        cell[numOfItem].fmt.format0.srcEnd = srcEnd;
00173        cell[numOfItem].fmt.format0.destBegin = destBegin;
00174        for(i=srcBegin ; i <= srcEnd ; i++)
00175        {
00176               /*     Unmark the umap */
00177               MAPVALUE(i) = NOMAPPING;
00178        }
00179        printf("End of Item %04X \n\n",numOfItem);
00180        numOfItem++;
00181 }
00182 void printnpl()
00183 {
00184 printf(
00185 "/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */\n"
00186 "/* ***** BEGIN LICENSE BLOCK *****\n"
00187 " * Version: MPL 1.1/GPL 2.0/LGPL 2.1\n"
00188 " *\n"
00189 " * The contents of this file are subject to the Mozilla Public License Version\n"
00190 " * 1.1 (the \"License\"); you may not use this file except in compliance with\n"
00191 " * the License. You may obtain a copy of the License at\n"
00192 " * http://www.mozilla.org/MPL/\n"
00193 " *\n"
00194 " * Software distributed under the License is distributed on an \"AS IS\" basis,\n"
00195 " * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\n"
00196 " * for the specific language governing rights and limitations under the\n"
00197 " * License.\n"
00198 " *\n"
00199 " * The Original Code is mozilla.org code.\n"
00200 " *\n"
00201 " * The Initial Developer of the Original Code is\n"
00202 " * Netscape Communications Corporation.\n"
00203 " * Portions created by the Initial Developer are Copyright (C) 2001\n"
00204 " * the Initial Developer. All Rights Reserved.\n"
00205 " *\n"
00206 " * Contributor(s):\n"
00207 " *\n"
00208 " * Alternatively, the contents of this file may be used under the terms of\n"
00209 " * either the GNU General Public License Version 2 or later (the \"GPL\"), or\n"
00210 " * the GNU Lesser General Public License Version 2.1 or later (the \"LGPL\"),\n"
00211 " * in which case the provisions of the GPL or the LGPL are applicable instead\n"
00212 " * of those above. If you wish to allow use of your version of this file only\n"
00213 " * under the terms of either the GPL or the LGPL, and not to allow others to\n"
00214 " * use your version of this file under the terms of the MPL, indicate your\n"
00215 " * decision by deleting the provisions above and replace them with the notice\n"
00216 " * and other provisions required by the GPL or the LGPL. If you do not delete\n"
00217 " * the provisions above, a recipient may use your version of this file under\n"
00218 " * the terms of any one of the MPL, the GPL or the LGPL.\n"
00219 " *\n"
00220 " * ***** END LICENSE BLOCK ***** */\n");
00221 }
00222 void gentable()
00223 {
00224        /*     OK! For now, we just use format 1 for each row */
00225        /*     We need to chage this to use other format to save the space */
00226        uint16 begin,end;
00227        uint16 ss,gs,gp,state,gc;   
00228        uint16 diff, lastdiff;
00229 
00230         printnpl();
00231        printf("/*========================================================\n");
00232        printf("  This is a Generated file. Please don't edit it.\n");
00233        printf("\n");
00234        printf("  The tool which used to generate this file is called umaptable.\n");
00235        printf("  You can find this tool under mozilla/intl/uconv/tools/umaptable.c.\n");
00236 
00237        printf("  If you have any problem of this file. Please contact \n"); 
00238        printf("  Netscape Client International Team or \n");
00239        printf("  ftang@netscape <Frank Tang> \n");
00240        printf("\n");
00241        printf("              Table in Debug form \n");
00242 
00243        for(begin = 0; MAPVALUE(begin) ==NOMAPPING; begin++)
00244               ;
00245        for(end = 0xFFFF; MAPVALUE(end) ==NOMAPPING; end--)
00246               ;
00247        if(end != begin)
00248        {
00249           lastdiff = MAPVALUE(begin) - begin; 
00250               for(gp=begin+1,state = 0 ; gp<=end; gp++)
00251               {
00252                      int input ;
00253               diff = MAPVALUE(gp) - gp; 
00254                  input = (diff == lastdiff);
00255                      switch(state)
00256                      {
00257                             case 0:       
00258                                    if(input)
00259                                    {
00260                                           state = 1;
00261                                       ss =  gp -1;
00262                                       gc = 2;
00263                                    }
00264                                    break;
00265                         case 1:
00266                                    if(input)
00267                                    {
00268                                           if(gc++ >= FORMAT0CNST)
00269                                           {
00270                                                  state = 2;
00271                                           }
00272                                    }
00273                                    else
00274                                    {
00275                                           state = 0;
00276                                    }
00277                                    break;
00278                         case 2:
00279                                    if(input)
00280                                    {
00281                                    }
00282                                    else
00283                                    {
00284                                       AddFormat0(ss,gp-1);
00285                                           state = 0;
00286                                    }
00287                                    break;
00288                      }
00289                      
00290                  lastdiff = diff;
00291               }      
00292        }
00293        if(state == 2)
00294               AddFormat0(ss,end);
00295 
00296        for(;(MAPVALUE(begin) ==NOMAPPING) && (begin <= end); begin++)
00297               ;
00298  if(begin <= end)
00299  {
00300               for(;(MAPVALUE(end)==NOMAPPING) && (end >= begin); end--)
00301                      ;
00302               for(ss=gp=begin,state = 0 ; gp<=end; gp++)
00303               {
00304                      int input = (MAPVALUE(gp) == NOMAPPING);
00305                      switch(state)
00306                      {
00307                      case 0:
00308                             if(input)
00309                             {
00310                                    gc = 1;
00311                                    gs = gp;
00312                                    state = 1;
00313                      }
00314                             break;
00315                      case 1:
00316                             if(input)
00317                             {
00318                                    if(gc++ >= FORMAT1CNST)
00319                                           state = 2;
00320                             }
00321                             else          
00322                                    state = 0;
00323                             break;
00324                      case 2:
00325                             if(input)
00326                             {             
00327                             }
00328                             else
00329                             {
00330                             if(gs == (ss+1))
00331                                           AddFormat2(ss);      
00332                                    else
00333                                           AddFormat1(ss ,gs-1);       
00334                                    state = 0;
00335                                    ss = gp;
00336                             }
00337                                           break;
00338                                    }
00339                             }
00340                             if(end == ss)
00341                                    AddFormat2(ss );     
00342                             else
00343                                    AddFormat1(ss ,end );       
00344        }
00345        printf("========================================================*/\n");
00346 }
00347 void writetable()
00348 {
00349        uint16 i;
00350        uint16 off1,off2,off3;
00351        uint16 cur = 0; 
00352        uint16 formatitem = (((numOfItem)>>2) + 1);
00353        off1 = 4;
00354        off2 = off1 + formatitem ;
00355        off3 = off2 + numOfItem * sizeof(uMapCell) / sizeof(uint16);
00356        /*     write itemOfList            */
00357        printf("/* Offset=0x%04X  ItemOfList */\n  0x%04X,\n", cur++, numOfItem);
00358 
00359        /*     write offsetToFormatArray   */
00360        printf("/*-------------------------------------------------------*/\n");
00361        printf("/* Offset=0x%04X  offsetToFormatArray */\n  0x%04X,\n",  cur++,off1);
00362 
00363        /*     write offsetToMapCellArray  */
00364        printf("/*-------------------------------------------------------*/\n");
00365        printf("/* Offset=0x%04X  offsetToMapCellArray */ \n  0x%04X,\n",  cur++,off2);
00366 
00367        /*     write offsetToMappingTable  */
00368        printf("/*-------------------------------------------------------*/\n");
00369        printf("/* Offset=0x%04X  offsetToMappingTable */ \n  0x%04X,\n", cur++,off3);
00370 
00371        /*     write FormatArray           */
00372        printf("/*-------------------------------------------------------*/\n");
00373        printf("/*       Offset=0x%04X   Start of Format Array */ \n",cur);
00374        printf("/*    Total of Format 0 : 0x%04X                 */\n"
00375                      , formatcount[0]);   
00376        printf("/*    Total of Format 1 : 0x%04X                 */\n"
00377                      , formatcount[1]);   
00378        printf("/*    Total of Format 2 : 0x%04X                 */\n"
00379                      , formatcount[2]);   
00380        printf("/*    Total of Format 3 : 0x%04X                 */\n"
00381                      , formatcount[3]);   
00382        for(i=0;i<formatitem;i++,cur++)
00383        {
00384               if((i%8) == 0)       
00385                      printf("\n");
00386               printf("0x%04X, ",format[i]);
00387        }
00388        printf("\n");
00389 
00390        /*     write MapCellArray          */
00391        printf("/*-------------------------------------------------------*/\n");
00392        printf("/*       Offset=0x%04X   Start of MapCell Array */ \n",cur);
00393        for(i=0;i<numOfItem;i++,cur+=3)
00394        {
00395               printf("/* %04X */    0x%04X, 0x%04X, 0x%04X, \n", 
00396                      i,
00397                      cell[i].fmt.format0.srcBegin,
00398                      cell[i].fmt.format0.srcEnd,
00399                      cell[i].fmt.format0.destBegin
00400                );
00401        }
00402 
00403        /*     write MappingTable          */
00404        printf("/*-------------------------------------------------------*/\n");
00405        printf("/*       Offset=0x%04X   Start of MappingTable */ \n",cur);
00406        for(i=0;i<mappinglen;i++,cur++)
00407        {
00408               if((i%8) == 0)       
00409                      printf("\n/* %04X */    ",i);
00410               printf("0x%04X, ",mapping[i] );
00411        }
00412        printf("\n");
00413        printf("/*    End of table Total Length = 0x%04X * 2 */\n",cur);
00414 }
00415 
00416 void usage()
00417 {
00418   fprintf(stderr, "please indicate what kind of mapping mapping table you want to generate:\n");
00419   fprintf(stderr, "\t-uf : generate *.uf (from unicode) table, or\n");
00420   fprintf(stderr, "\t-ut : generate *.ut (to unicode) table\n");
00421 }
00422 parsearg(int argc, char* argv[])
00423 {
00424        int i;
00425        for(i=0;i<argc;i++)
00426        {
00427               if(strncmp("-uf", argv[i],3) == 0) {
00428                         if(! bInitFromOrTo) {
00429                            bGenerateFromUnicodeTable = 1;
00430                            bInitFromOrTo = 1;
00431                         } else {
00432                            usage();
00433                            exit(-1);
00434                         }
00435                 } 
00436               if(strncmp("-ut", argv[i],3) == 0) {
00437                         if(! bInitFromOrTo) {
00438                            bGenerateFromUnicodeTable = 0;
00439                            bInitFromOrTo = 1;
00440                         } else {
00441                            usage();
00442                            exit(-1);
00443                         }
00444                 } 
00445               if((strncmp("-0", argv[i],2) == 0) && ((i+1) < argc))
00446               {
00447                      int cnst0;
00448                      if(sscanf(argv[i+1], "%d", &cnst0) == 1)
00449                      {
00450                             if(cnst0 > 0)
00451                             {
00452                                    FORMAT0CNST = cnst0;
00453                             }
00454                      }
00455                      else
00456                      {
00457                             fprintf(stderr, "argc error !!!!\n");
00458                             exit(-1);
00459                      }
00460                      i++;
00461               }
00462               if((strncmp("-1", argv[i],2) == 0) && ((i+1) < argc))
00463               {
00464                      int cnst1;
00465                      if(sscanf(argv[i+1], "%d", &cnst1) == 1)
00466                      {
00467                             if(cnst1 > 0)
00468                             {
00469                                    FORMAT1CNST = cnst1;
00470                             }
00471                      }
00472                      else
00473                      {
00474                             fprintf(stderr, "argc error !!!!\n");
00475                             exit(-1);
00476                      }
00477                      i++;
00478               }
00479        }
00480         if(! bInitFromOrTo)
00481         {
00482                 usage();
00483               exit(-1);
00484         }
00485        fprintf(stderr, "format 0 cnst = %d\n", FORMAT0CNST);
00486        fprintf(stderr, "format 1 cnst = %d\n", FORMAT1CNST);
00487         fprintf(stderr, "generate u%c table\n", 
00488                         bGenerateFromUnicodeTable ? 'f' : 't');
00489 }
00490 void getinput()
00491 {
00492   char buf[256];
00493   short c,u;
00494   for(;gets(buf)!=NULL;)
00495   {
00496      if(buf[0]=='0' && buf[1] == 'x')
00497         {
00498           sscanf(buf,"%hx %hx",&c,&u);
00499           if(bGenerateFromUnicodeTable)
00500             SetMapValue(u, c);
00501           else
00502             SetMapValue(c, u);
00503         }
00504   }
00505 }
00506 main(int argc, char* argv[])
00507 {
00508   parsearg(argc, argv);
00509   initmaps();
00510   getinput();
00511   gentable();
00512   writetable();      
00513 }