home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libi18n / unicode / tbltool / utblutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.0 KB  |  361 lines

  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. #include "umap.h"
  22. uint16 umap[256][256];
  23. extern void getinput();
  24.  
  25. #define MAXCELLNUM 1000
  26.  
  27. static int numOfItem = 0;
  28. uMapCell cell[MAXCELLNUM];
  29. uint16    format[MAXCELLNUM / 4];
  30. uint16   mapping[256*256];
  31. static int mappinglen  = 0;
  32. static int formatcount[4] = {0,0,0,0}; 
  33.  
  34. #define SetFormat(n,f)        { format[(n >> 2)] |= ((f) << ((n & 0x0003)    << 2)); formatcount[f]++; }
  35. #define GetFormat(n)        ( format[(n >> 2)] >> ((n & 0x0003)    << 2)) &0x00FF)
  36. #define MAPVALUE(i)    (umap[(i >> 8) & 0xFF][(i) & 0xFF])
  37.  
  38. int  FORMAT1CNST = 10 ;
  39. int  FORMAT0CNST = 5 ;
  40. void initmaps()
  41. {
  42.     int i,j;
  43.     for(i=0;i<256;i++)
  44.         for(j=0;j<256;j++) 
  45.         {
  46.             umap[i][j]=   NOMAPPING;
  47.         }
  48.     for(i=0;i<MAXCELLNUM / 4;i++)
  49.         format[i]=0;
  50. }
  51. void SetMapValue(short u,short c)
  52. {
  53.     MAPVALUE(u) = c & 0x0000FFFF;
  54. }
  55. void AddFormat2(uint16 srcBegin)
  56. {
  57.     uint16 destBegin = MAPVALUE(srcBegin);
  58.     printf("Begin of Item %04X\n",numOfItem);
  59.     printf(" Format 2\n");
  60.     printf("  srcBegin = %04X\n", srcBegin);
  61.     printf("  destBegin = %04X\n", destBegin );
  62.     SetFormat(numOfItem,2);
  63.     cell[numOfItem].fmt.format2.srcBegin = srcBegin;
  64.     cell[numOfItem].fmt.format2.srcEnd = 0;
  65.     cell[numOfItem].fmt.format2.destBegin = destBegin;
  66.     printf("End of Item %04X \n\n",numOfItem);
  67.     numOfItem++;
  68.     /*    Unmark the umap */
  69.     MAPVALUE(srcBegin) = NOMAPPING;
  70. }
  71. void AddFormat1(uint16 srcBegin, uint16 srcEnd)
  72. {
  73.     uint16 i;
  74.     printf("Begin of Item %04X\n",numOfItem);
  75.     printf(" Format 1\n");
  76.     printf("  srcBegin = %04X\n", srcBegin);
  77.     printf("  srcEnd = %04X\n", srcEnd );
  78.     printf("  mappingOffset = %04X\n", mappinglen);
  79.     printf(" Mapping  = " );  
  80.     SetFormat(numOfItem,1);
  81.     cell[numOfItem].fmt.format1.srcBegin = srcBegin;
  82.     cell[numOfItem].fmt.format1.srcEnd = srcEnd;
  83.     cell[numOfItem].fmt.format1.mappingOffset = mappinglen;
  84.     for(i=srcBegin ; i <= srcEnd ; i++,mappinglen++)
  85.     {
  86.         if( ((i-srcBegin) % 8) == 0)
  87.             printf("\n  ");
  88.         mapping[mappinglen]= MAPVALUE(i);
  89.         printf("%04X ",(mapping[mappinglen]  ));
  90.         /*    Unmark the umap */
  91.         MAPVALUE(i) = NOMAPPING;
  92.     }
  93.     printf("\n");
  94.     printf("End of Item %04X \n\n",numOfItem);
  95.     numOfItem++;
  96. }
  97. void AddFormat0(uint16 srcBegin, uint16 srcEnd)
  98. {
  99.     uint16 i;
  100.     uint16 destBegin = MAPVALUE(srcBegin);
  101.     printf("Begin of Item %04X\n",numOfItem);
  102.     printf(" Format 0\n");
  103.     printf("  srcBegin = %04X\n", srcBegin);
  104.     printf("  srcEnd = %04X\n", srcEnd );
  105.     printf("  destBegin = %04X\n", destBegin );
  106.     SetFormat(numOfItem,0);
  107.     cell[numOfItem].fmt.format0.srcBegin = srcBegin;
  108.     cell[numOfItem].fmt.format0.srcEnd = srcEnd;
  109.     cell[numOfItem].fmt.format0.destBegin = destBegin;
  110.     for(i=srcBegin ; i <= srcEnd ; i++)
  111.     {
  112.         /*    Unmark the umap */
  113.         MAPVALUE(i) = NOMAPPING;
  114.     }
  115.     printf("End of Item %04X \n\n",numOfItem);
  116.     numOfItem++;
  117. }
  118. void gentable()
  119. {
  120.     /*    OK! For now, we just use format 1 for each row */
  121.     /*    We need to chage this to use other format to save the space */
  122.     uint16 i,j,k;
  123.     uint16 begin,end;
  124.     uint16 ss,gs,gp,state,gc;    
  125.     uint16 diff, lastdiff;
  126.  
  127.     printf("/*========================================================\n");
  128.     printf("  This is a Generated file. Please don't edit it.\n");
  129.     printf("\n");
  130.     printf("  The tool which used to generate this file is called fromu.\n");
  131.     printf("  If you have any problem of this file. Please contact \n"); 
  132.     printf("  Netscape Client International Team or \n");
  133.     printf("  ftang@netscape <Frank Tang> \n");
  134.     printf("\n");
  135.     printf("              Table in Debug form \n");
  136.  
  137.     for(begin = 0; MAPVALUE(begin) ==NOMAPPING; begin++)
  138.         ;
  139.     for(end = 0xFFFF; MAPVALUE(end) ==NOMAPPING; end--)
  140.         ;
  141.     if(end != begin)
  142.     {
  143.        lastdiff = MAPVALUE(begin) - begin; 
  144.         for(gp=begin+1,state = 0 ; gp<=end; gp++)
  145.         {
  146.             int input ;
  147.            diff = MAPVALUE(gp) - gp; 
  148.            input = (diff == lastdiff);
  149.             switch(state)
  150.             {
  151.                  case 0:    
  152.                     if(input)
  153.                     {
  154.                         state = 1;
  155.                        ss =  gp -1;
  156.                        gc = 2;
  157.                     }
  158.                     break;
  159.                case 1:
  160.                     if(input)
  161.                     {
  162.                         if(gc++ >= FORMAT0CNST)
  163.                         {
  164.                             state = 2;
  165.                         }
  166.                     }
  167.                     else
  168.                     {
  169.                         state = 0;
  170.                     }
  171.                     break;
  172.                case 2:
  173.                     if(input)
  174.                     {
  175.                     }
  176.                     else
  177.                     {
  178.                        AddFormat0(ss,gp-1);
  179.                         state = 0;
  180.                     }
  181.                     break;
  182.             }
  183.             
  184.            lastdiff = diff;
  185.         }    
  186.     }
  187.     if(state == 2)
  188.         AddFormat0(ss,end);
  189.  
  190.     for(;(MAPVALUE(begin) ==NOMAPPING) && (begin <= end); begin++)
  191.         ;
  192.  if(begin <= end)
  193.  {
  194.         for(;(MAPVALUE(end)==NOMAPPING) && (end >= begin); end--)
  195.             ;
  196.         for(ss=gp=begin,state = 0 ; gp<=end; gp++)
  197.         {
  198.             int input = (MAPVALUE(gp) == NOMAPPING);
  199.             switch(state)
  200.             {
  201.             case 0:
  202.                 if(input)
  203.                 {
  204.                     gc = 1;
  205.                     gs = gp;
  206.                     state = 1;
  207.             }
  208.                 break;
  209.             case 1:
  210.                 if(input)
  211.                 {
  212.                     if(gc++ >= FORMAT1CNST)
  213.                         state = 2;
  214.                 }
  215.                 else        
  216.                     state = 0;
  217.                 break;
  218.             case 2:
  219.                 if(input)
  220.                 {        
  221.                 }
  222.                 else
  223.                 {
  224.                    if(gs == (ss+1))
  225.                         AddFormat2(ss);    
  226.                     else
  227.                         AddFormat1(ss ,gs-1);    
  228.                     state = 0;
  229.                     ss = gp;
  230.                 }
  231.                         break;
  232.                     }
  233.                 }
  234.                 if(end == ss)
  235.                     AddFormat2(ss );    
  236.                 else
  237.                     AddFormat1(ss ,end );    
  238.     }
  239.     printf("========================================================*/\n");
  240. }
  241. void writetable()
  242. {
  243.     uint16 i;
  244.     uint16 off1,off2,off3;
  245.     uint16 cur = 0; 
  246.     uint16 formatitem = (((numOfItem)>>2) + 1);
  247.     off1 = 4;
  248.     off2 = off1 + formatitem ;
  249.     off3 = off2 + numOfItem * sizeof(uMapCell) / sizeof(uint16);
  250.     /*    write itemOfList        */
  251.     printf("/* Offset=0x%04X  ItemOfList */\n  0x%04X,\n", cur++, numOfItem);
  252.  
  253.     /*    write offsetToFormatArray    */
  254.     printf("/*-------------------------------------------------------*/\n");
  255.     printf("/* Offset=0x%04X  offsetToFormatArray */\n  0x%04X,\n",  cur++,off1);
  256.  
  257.     /*    write offsetToMapCellArray    */
  258.     printf("/*-------------------------------------------------------*/\n");
  259.     printf("/* Offset=0x%04X  offsetToMapCellArray */ \n  0x%04X,\n",  cur++,off2);
  260.  
  261.     /*    write offsetToMappingTable    */
  262.     printf("/*-------------------------------------------------------*/\n");
  263.     printf("/* Offset=0x%04X  offsetToMappingTable */ \n  0x%04X,\n", cur++,off3);
  264.  
  265.     /*    write FormatArray        */
  266.     printf("/*-------------------------------------------------------*/\n");
  267.     printf("/*       Offset=0x%04X   Start of Format Array */ \n",cur);
  268.     printf("/*    Total of Format 0 : 0x%04X             */\n"
  269.             , formatcount[0]);    
  270.     printf("/*    Total of Format 1 : 0x%04X             */\n"
  271.             , formatcount[1]);    
  272.     printf("/*    Total of Format 2 : 0x%04X             */\n"
  273.             , formatcount[2]);    
  274.     printf("/*    Total of Format 3 : 0x%04X             */\n"
  275.             , formatcount[3]);    
  276.     for(i=0;i<formatitem;i++,cur++)
  277.     {
  278.         if((i%8) == 0)    
  279.             printf("\n");
  280.         printf("0x%04X, ",format[i]);
  281.     }
  282.     printf("\n");
  283.  
  284.     /*    write MapCellArray        */
  285.     printf("/*-------------------------------------------------------*/\n");
  286.     printf("/*       Offset=0x%04X   Start of MapCell Array */ \n",cur);
  287.     for(i=0;i<numOfItem;i++,cur+=3)
  288.     {
  289.         printf("/* %04X */    0x%04X, 0x%04X, 0x%04X, \n", 
  290.             i,
  291.             cell[i].fmt.format0.srcBegin,
  292.             cell[i].fmt.format0.srcEnd,
  293.             cell[i].fmt.format0.destBegin
  294.             );
  295.     }
  296.  
  297.     /*    write MappingTable        */
  298.     printf("/*-------------------------------------------------------*/\n");
  299.     printf("/*       Offset=0x%04X   Start of MappingTable */ \n",cur);
  300.     for(i=0;i<mappinglen;i++,cur++)
  301.     {
  302.         if((i%8) == 0)    
  303.             printf("\n/* %04X */    ",i);
  304.         printf("0x%04X, ",mapping[i] );
  305.     }
  306.     printf("\n");
  307.     printf("/*    End of table Total Length = 0x%04X * 2 */\n",cur);
  308. }
  309.  
  310. parsearg(int argc, char* argv[])
  311. {
  312.     int i;
  313.     for(i=0;i<argc;i++)
  314.     {
  315.         if((strncmp("-0", argv[i],2) == 0) && ((i+1) < argc))
  316.         {
  317.             int cnst0;
  318.             if(sscanf(argv[i+1], "%d", &cnst0) == 1)
  319.             {
  320.                 if(cnst0 > 0)
  321.                 {
  322.                     FORMAT0CNST = cnst0;
  323.                 }
  324.             }
  325.             else
  326.             {
  327.                 fprintf(stderr, "argc error !!!!\n");
  328.                 exit(-1);
  329.             }
  330.             i++;
  331.         }
  332.         if((strncmp("-1", argv[i],2) == 0) && ((i+1) < argc))
  333.         {
  334.             int cnst1;
  335.             if(sscanf(argv[i+1], "%d", &cnst1) == 1)
  336.             {
  337.                 if(cnst1 > 0)
  338.                 {
  339.                     FORMAT1CNST = cnst1;
  340.                 }
  341.             }
  342.             else
  343.             {
  344.                 fprintf(stderr, "argc error !!!!\n");
  345.                 exit(-1);
  346.             }
  347.             i++;
  348.         }
  349.     }
  350.     fprintf(stderr, "format 0 cnst = %d\n", FORMAT0CNST);
  351.     fprintf(stderr, "format 1 cnst = %d\n", FORMAT1CNST);
  352. }
  353. main(int argc, char* argv[])
  354. {
  355.   parsearg(argc, argv);
  356.   initmaps();
  357.   getinput();
  358.   gentable();
  359.   writetable();    
  360. }
  361.