Back to index

im-sdk  12.3.91
bin2txt.c
Go to the documentation of this file.
00001 /*
00002   Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004   Permission is hereby granted, free of charge, to any person obtaining a
00005   copy of this software and associated documentation files (the
00006   "Software"), to deal in the Software without restriction, including
00007   without limitation the rights to use, copy, modify, merge, publish,
00008   distribute, sublicense, and/or sell copies of the Software, and to
00009   permit persons to whom the Software is furnished to do so, subject to
00010   the following conditions: The above copyright notice and this
00011   permission notice shall be included in all copies or substantial
00012   portions of the Software.
00013 
00014 
00015   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018   IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019   FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020   CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021   THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022   ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025   Except as contained in this notice, the names of The Open Group and/or
00026   Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027   promote the sale, use or other dealings in this Software without prior
00028   written authorization from The Open Group and/or Sun Microsystems,
00029   Inc., as applicable.
00030 
00031 
00032   X Window System is a trademark of The Open Group
00033 
00034   OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035   logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036   Group. All other trademarks and registered trademarks mentioned herein
00037   are the property of their respective owners. No right, title or
00038   interest in or to any trademark, service mark, logo or trade name of
00039   Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <unistd.h>
00045 #include <string.h>
00046 #include "codetable.h"
00047 #include "ctfile.h"
00048 #include "encode.h"
00049 
00050 #define       MAX_CHAR_NUM_PER_LINE       80
00051 #define       MAX_WORD_NUM_PER_LINE       30
00052 
00053 CodeTableStruct hzCodeTable;
00054 
00055 tableNode *nodeList;        /* all Nodes will be linearized to put here */
00056 unsigned char *hzList;             /* all HZ codes will be put here */
00057 
00058 unsigned int clen = 0;
00059 unsigned char keystack[80];
00060 
00061 char comment[MAX_COMMENT_LEN];
00062 keyPrompt keyprompt[MAX_USEDCODES_NUM];
00063 functionKey functionkey[MAX_FUNCTIONKEY_NUM];
00064 
00065 typedef struct _str_int {
00066   char  *string;
00067   int   value;
00068 } string_value_pair;
00069 
00070 string_value_pair selectkeymode_str_val[] = {
00071   { NUMBER_MODE_STR, NUMBER_MODE },
00072   { LOWER_MODE_STR,  LOWER_MODE },
00073   { UPPER_MODE_STR,  UPPER_MODE },
00074   { NULL,            NUMBER_MODE }
00075 };
00076 
00077 int str2val(string_value_pair *str_val, char *str)
00078 {
00079   while (str_val->string) {
00080     /*
00081       printf("%d: %s\n", str_val->value, str_val->string);
00082     */
00083     if (!strncasecmp (str_val->string, str, strlen(str_val->string)))
00084       return (str_val->value);
00085     str_val++;
00086   }
00087   return (str_val->value);
00088 }
00089 
00090 char *val2str(string_value_pair *str_val, int value)
00091 {
00092   while (str_val->string) {
00093     /*
00094       printf("%d: %s\n", str_val->value, str_val->string);
00095     */
00096     if (str_val->value == value)
00097       return (str_val->string);
00098     str_val++;
00099   }
00100   return (str_val->string);
00101 }
00102 
00103 void PrintNodeList()
00104 {
00105   int i;
00106 
00107   for (i=0; i<hzCodeTable.sizeNodeList; i++)
00108     {
00109       printf("id :%d\n", i);
00110       printf("key: %c\n", nodeList[i].key);
00111       printf("num_NextKeys: %d\n", nodeList[i].num_NextKeys);
00112       printf("num_HZchoice: %d\n", nodeList[i].num_HZchoice);
00113       printf("pos_NextKey: %d\n", nodeList[i].pos_NextKey);
00114       printf("pos_HZidx: %d\n", nodeList[i].pos_HZidx);
00115       printf("\n");
00116     }
00117 }
00118 
00119 void PrintTableInfo()
00120 {
00121   int i;
00122 
00123   printf("Lname:%s\n", hzCodeTable.Lname);
00124   printf("Cname:%s\n", hzCodeTable.Cname);
00125   printf("WildChar:%s\n", hzCodeTable.WildChar);
00126   printf("UsedCodes:%s\n", hzCodeTable.UsedCodes);
00127   printf("MaxCodes:%d\n", hzCodeTable.MaxCodes);
00128   printf("Encode:%d\n", hzCodeTable.Encode);
00129   printf("sizeNodeList:%d\n", hzCodeTable.sizeNodeList);
00130   printf("sizeHZList:%d\n", hzCodeTable.sizeHZList);
00131 
00132   printf("key_prompt\n");
00133   for (i=0; i<MAX_USEDCODES_NUM; i++)
00134     {
00135       if (keyprompt[i].prompt[0])
00136        printf("%c:  %s\n", i, keyprompt[i].prompt);
00137     }
00138 
00139   printf("function_key\n");
00140   for (i=0; i<MAX_FUNCTIONKEY_NUM; i++)
00141     {
00142       if (functionkey[i].keylist[0])
00143        printf("%d:  %d\n", i, functionkey[i].keylist[0]);
00144     }
00145 
00146   comment[clen] = '\0';
00147   printf("comment is \n%s\n", comment);
00148 }
00149 
00150 /*
00151  * ReadInput -- read and parse input codetable binary format file
00152  */
00153 static void ReadInput(ifile)
00154      FILE *ifile;
00155 {
00156   char ctFlag[256];
00157   int  ver;
00158 
00159   /* Read CodeTable File Flag */
00160   if (fread (ctFlag, strlen(CODETABLE_FLAG), 1, ifile) != 1)
00161     {
00162       fprintf (stderr, "File read Error\n");
00163       exit(1);
00164     }
00165 
00166   if (strncmp (ctFlag, CODETABLE_FLAG, strlen(CODETABLE_FLAG)) != 0) {
00167     fprintf (stderr, "File is not in CodeTable format\n");
00168     exit(1);
00169   }
00170 
00171   /* Read CodeTable Version Flag */
00172   if (fread ((char *)(&ver), sizeof (int), 1, ifile) != 1) 
00173     {
00174       fprintf (stderr, "File read Error\n");
00175       exit(1);
00176     }
00177 
00178   if (ver != CODETABLE_VERSION)
00179     {
00180       fprintf (stderr, "File is not in correct Version Number\n");
00181       exit(1);
00182     }
00183 
00184 
00185   /* Read CodeTable Header Structure */
00186   if (fread (&hzCodeTable, sizeof(CodeTableStruct), 1, ifile) == 0) {
00187     fprintf (stderr, "File read Error\n");
00188     exit(1);
00189   }
00190 
00191   /* Read Node List and HanZi List */
00192   nodeList = (tableNode *) calloc(hzCodeTable.sizeNodeList,sizeof(tableNode));
00193   hzList = (unsigned char *) calloc(hzCodeTable.sizeHZList, sizeof(unsigned char));
00194   if ((! nodeList) || (! hzList)) {
00195     perror ("Memory allocation");
00196     exit (1);
00197   }
00198 
00199   if ((fread (nodeList, sizeof(tableNode), hzCodeTable.sizeNodeList, ifile)
00200        != hzCodeTable.sizeNodeList) ||
00201       (fread (hzList, sizeof(unsigned char), hzCodeTable.sizeHZList, ifile)
00202        != hzCodeTable.sizeHZList))
00203     {
00204       fprintf (stderr, "File Read Error\n");
00205       exit (1);
00206     }
00207 
00208   /* Read Key Prompt List */
00209   if (GETBIT(hzCodeTable.bSectionsFlag, KEYPROMPT_SECTION))
00210     {
00211       if ((fread ((char *)(&(keyprompt[0])), MAX_USEDCODES_NUM, sizeof(keyPrompt), ifile)) 
00212          != sizeof(keyPrompt)) 
00213        {
00214          fprintf (stderr, "File Read Error\n");
00215          exit (1);
00216        }
00217     }
00218 
00219   /* Read Function Key List */
00220   if (GETBIT(hzCodeTable.bSectionsFlag, FUNCTIONKEY_SECTION))
00221     {
00222       if ((fread ((char *)(&(functionkey[0])), MAX_FUNCTIONKEY_NUM, sizeof(functionKey), ifile)) 
00223          != sizeof(functionKey)) 
00224        {
00225          fprintf (stderr, "File Read Error\n");
00226          exit (1);
00227        }
00228     }
00229 
00230   /* Read Comment */
00231   if (GETBIT(hzCodeTable.bSectionsFlag, COMMENT_SECTION))
00232     {
00233       if ((fread ((char *)(&clen), sizeof (unsigned int), 1, ifile) != 1) ||
00234          (fread (comment, 1, clen, ifile) != clen))
00235        {
00236          clen = 0;
00237        }
00238       if (clen > MAX_COMMENT_LEN)
00239        clen = MAX_COMMENT_LEN;
00240     }
00241 }
00242 
00243 int check_if_duplicate(unsigned char *hzstr, int hzlen, 
00244                      unsigned char *whole_str, int word_num, int encode)
00245 {
00246   int i, str_len;
00247   unsigned char *phz;
00248 
00249   phz = whole_str;
00250   for (i=0; i<word_num; i++) {
00251     if (*phz == HZ_PHRASE_TAG) {
00252       /* phrase */
00253       str_len = *(phz+1);
00254       phz += 2;
00255       if (str_len == hzlen) {
00256        if(!strncmp((char *)hzstr, (char *)phz, hzlen))
00257          return(1);
00258       }
00259     } else {
00260       /* hanzi */
00261       str_len = get_char_len_by_encodeid(encode, phz);
00262       if (str_len == hzlen) {
00263        if(!strncmp((char *)hzstr, (char *)phz, hzlen))
00264          return(1);
00265       }
00266     }
00267 
00268     phz += str_len;
00269   }
00270 
00271   return(0);
00272 }
00273 
00274 /* output line by line while traveling the tableNodes */
00275 static void LineOut (top, nodeIdx, mode, ofile)
00276      int top;
00277      unsigned int nodeIdx;
00278      int mode;
00279      FILE *ofile;
00280 {
00281   register tableNode *tnptr = &nodeList[nodeIdx];
00282   unsigned char *phz;
00283   int i, j, hzlen, encode, cnt, nSingle, nPhrase;
00284   int num;   /* number of choices under the key sequence in stack */
00285   int max_char_num, bDup, newline;
00286 
00287   keystack[top] = tnptr->key;
00288   num = tnptr->num_HZchoice;
00289   encode = hzCodeTable.Encode;
00290 
00291   /*
00292     printf("top:%d, key:%c, num_NextKeys:%d, num:%d\n", top, tnptr->key, tnptr->num_NextKeys, num);
00293   */
00294   /* if there are strings in this node */
00295   if (num > 0){
00296     /* there are HZs exclusively under the keys, or no more input keys */
00297               
00298     /* compute the Single HZ num and Phrase num in this string */
00299     nSingle = 0; 
00300     nPhrase = 0;
00301     phz = &hzList[tnptr->pos_HZidx];
00302     for ( i = 0; i < num; i++ )
00303       {
00304        if (*phz == HZ_PHRASE_TAG) {
00305          phz += *(phz+1) + 2;
00306          nPhrase ++;
00307        } else {
00308          hzlen = get_char_len_by_encodeid(encode, phz);
00309          phz += hzlen;
00310          nSingle ++;
00311        }
00312       }
00313 
00314     if (mode == SINGLE_SECTION && nSingle == 0) return;
00315     if (mode == PHRASE_SECTION && nPhrase == 0) return;
00316  
00317     /* print Single HZ string or Phrase string */
00318     if (mode == SINGLE_SECTION)
00319       max_char_num = MAX_WORD_NUM_PER_LINE;
00320     else 
00321       max_char_num = MAX_CHAR_NUM_PER_LINE;
00322 
00323     cnt = 0;
00324     newline = 1;
00325     phz = &hzList[tnptr->pos_HZidx];
00326     for (i = 0; i < num; i++) {
00327       /* begin a new line */
00328       if (cnt >= max_char_num) {
00329        cnt = 0;
00330        newline = 1;
00331       }
00332       if (newline == 1) {
00333        register int j;
00334 
00335        /* print external code */
00336        (void) putc ('\n', ofile);
00337        for (j = 1; j <= top; j++) {
00338          (void) putc (keystack[j], ofile);
00339        }
00340        (void) putc ('\t', ofile);
00341        newline = 0;
00342       }
00343 
00344       if (*phz == HZ_PHRASE_TAG) {
00345        /* phrase */
00346        hzlen = *(phz+1);
00347        phz += 2;
00348        if (mode == PHRASE_SECTION) {
00349          bDup = check_if_duplicate(phz, hzlen, phz+hzlen, num-i-1, encode);
00350          if(!bDup){
00351            for (j = 0; j < hzlen; j++) {
00352              (void) putc (*(phz+j), ofile);
00353            }
00354            (void) putc (' ', ofile);
00355            cnt += hzlen;
00356          }
00357        }
00358       } else {
00359        /* hanzi */
00360        hzlen = get_char_len_by_encodeid(encode, phz);
00361 
00362        if (mode == SINGLE_SECTION) {
00363          bDup = check_if_duplicate(phz, hzlen, phz+hzlen, num-i-1, encode);
00364          if(!bDup){
00365            for (j = 0; j < hzlen; j++) {
00366              (void) putc (*(phz+j), ofile);
00367            }
00368            cnt ++;
00369          }
00370        }
00371       }
00372       phz += hzlen;
00373     }
00374   }
00375 
00376   for (i = 0; i < tnptr->num_NextKeys; i++)
00377     LineOut (top + 1, tnptr->pos_NextKey + i, mode, ofile);
00378 }
00379 
00380 /* convert key value to "^A" format or normal visible character */
00381 static int Putc (ch, ofile)
00382      unsigned char ch;
00383      FILE *ofile;
00384 {
00385   /*
00386     printf("ch:  0x%x, 0x%x, %c\n", ch, ch & 0x7f, ch & 0x7f);
00387   */
00388 
00389   if (ch & 0x80) {
00390     fprintf (ofile, " ^%c", ch & 0x7f);
00391   } else {
00392     (void) putc (ch, ofile);
00393   }
00394   return (1);
00395 }
00396 
00397 static void Output (ofile)
00398      FILE *ofile;
00399 {
00400   int i;
00401 
00402   fprintf (ofile, "## HANZI codetable input table\n");
00403 
00404   /* description section */
00405   fprintf (ofile, "\n[ %s ]\n", DESCRIPTION_STR);
00406   fprintf (ofile, "%s\t\t%s\n", LOCALE_STR, hzCodeTable.Lname);
00407   fprintf (ofile, "%s\t\t%s\n", NAME_STR, hzCodeTable.Cname);
00408   fprintf (ofile, "%s\t\t%s\n", ENCODE_STR, (char *)get_name_from_encodeid(hzCodeTable.Encode));
00409   fprintf (ofile, "%s\t%s\n", USEDCODES_STR, hzCodeTable.UsedCodes);
00410   fprintf (ofile, "%s\t%s\n", WILDCHAR_STR, hzCodeTable.WildChar);
00411   fprintf (ofile, "%s\t%d\n", MAXCODES_STR, hzCodeTable.MaxCodes);
00412        
00413   /* Key_Prompt section */
00414   if (GETBIT(hzCodeTable.bSectionsFlag, KEYPROMPT_SECTION))
00415     {
00416       fprintf (ofile, "\n[ %s ]\n", KEYPROMPT_STR);
00417       for (i=0; i<MAX_USEDCODES_NUM; i++)
00418        {
00419          if (keyprompt[i].prompt[0] != 0)
00420            fprintf (ofile, "%c\t%s\n", i, keyprompt[i].prompt);
00421        }
00422     }
00423 
00424   /* Function_Key section */
00425   if (GETBIT(hzCodeTable.bSectionsFlag, FUNCTIONKEY_SECTION))
00426     {
00427       fprintf (ofile, "\n[ %s ]\n", FUNCTIONKEY_STR);
00428       if (functionkey[PAGEUP_KEY_ID].keylist[0] != 0)
00429        {
00430          fprintf(ofile, "%s\t\t", PAGEUP_KEY_STR);
00431          for (i=0; i<strlen(functionkey[PAGEUP_KEY_ID].keylist); i++)
00432            Putc(functionkey[PAGEUP_KEY_ID].keylist[i], ofile);
00433          fprintf(ofile, "\n");
00434        }
00435 
00436       if (functionkey[PAGEDOWN_KEY_ID].keylist[0] != 0)
00437        {
00438          fprintf(ofile, "%s\t", PAGEDOWN_KEY_STR);
00439          for (i=0; i<strlen(functionkey[PAGEDOWN_KEY_ID].keylist); i++)
00440            Putc(functionkey[PAGEDOWN_KEY_ID].keylist[i], ofile);
00441          fprintf(ofile, "\n");
00442        }
00443 
00444       if (functionkey[BACKSPACE_KEY_ID].keylist[0] != 0)
00445        {
00446          fprintf(ofile, "%s\t", BACKSPACE_KEY_STR);
00447          for (i=0; i<strlen(functionkey[BACKSPACE_KEY_ID].keylist); i++)
00448            Putc(functionkey[BACKSPACE_KEY_ID].keylist[i],ofile);
00449          fprintf(ofile, "\n");
00450        }
00451 
00452       if (functionkey[CLEARALL_KEY_ID].keylist[0] != 0)
00453        {
00454          fprintf(ofile, "%s\t", CLEARALL_KEY_STR);
00455          for (i=0; i<strlen(functionkey[CLEARALL_KEY_ID].keylist); i++)
00456            Putc(functionkey[CLEARALL_KEY_ID].keylist[i],ofile);
00457          fprintf(ofile, "\n");
00458        }
00459     }
00460 
00461   /* Options section */
00462   if (GETBIT(hzCodeTable.bSectionsFlag, OPTIONS_SECTION))
00463     {
00464       fprintf (ofile, "\n[ %s ]\n", OPTIONS_STR);
00465       if (hzCodeTable.nKeyByKeyMode)
00466        fprintf(ofile, "%s\t\t%s\n", KEYBYKEY_MODE_STR, OPTION_OPEN_STR);
00467       else
00468        fprintf(ofile, "%s\t\t%s\n", KEYBYKEY_MODE_STR, OPTION_CLOSE_STR);
00469 
00470       if (hzCodeTable.nHelpInfoMode)
00471        fprintf(ofile, "%s\t\t%s\n", HELPINFO_MODE_STR, OPTION_OPEN_STR);
00472       else
00473        fprintf(ofile, "%s\t\t%s\n", HELPINFO_MODE_STR, OPTION_CLOSE_STR);
00474 
00475       if (hzCodeTable.nAutoSelectMode)
00476        fprintf(ofile, "%s\t%s\n", AUTOSELECT_MODE_STR, OPTION_OPEN_STR);
00477       else
00478        fprintf(ofile, "%s\t%s\n", AUTOSELECT_MODE_STR, OPTION_CLOSE_STR);
00479 
00480       if (hzCodeTable.nKeyPromptMode)
00481        fprintf(ofile, "%s\t\t%s\n", KEYPROMPT_MODE_STR, OPTION_OPEN_STR);
00482       else
00483        fprintf(ofile, "%s\t\t%s\n", KEYPROMPT_MODE_STR, OPTION_CLOSE_STR);
00484       /*
00485        if (hzCodeTable.nDisplayOnSpotMode)
00486        fprintf(ofile, "%s\t%s\n", DISPLAYONSPOT_MODE_STR, OPTION_OPEN_STR);
00487        else
00488        fprintf(ofile, "%s\t%s\n", DISPLAYONSPOT_MODE_STR, OPTION_CLOSE_STR);
00489       */
00490       fprintf(ofile, "%s\t\t%s\n", SELECTKEY_MODE_STR, val2str(selectkeymode_str_val, hzCodeTable.nSelectKeyMode));
00491     }
00492 
00493   /* comment section */
00494   if (GETBIT(hzCodeTable.bSectionsFlag, COMMENT_SECTION))
00495     {
00496       if (clen > 0)
00497        {
00498          fprintf (ofile, "\n[ %s ]\n", COMMENT_STR);
00499          fprintf (ofile, "%s\n", comment);
00500        }
00501     }
00502 
00503   /* single hanzi section */
00504   if (GETBIT(hzCodeTable.bSectionsFlag, SINGLE_SECTION))
00505     {
00506       fprintf (ofile, "\n[ %s ]", SINGLE_STR);
00507       LineOut(0, 0, SINGLE_SECTION, ofile);
00508       fprintf (ofile, "\n");
00509     }
00510 
00511   /* phrase section */
00512   if (GETBIT(hzCodeTable.bSectionsFlag, PHRASE_SECTION))
00513     {
00514       fprintf (ofile, "\n[ %s ]", PHRASE_STR);
00515       LineOut(0, 0, PHRASE_SECTION, ofile);
00516       fprintf (ofile, "\n");
00517     }
00518 }
00519 
00520 int main(argc, argv)
00521      int argc;
00522      char *argv[];
00523 {
00524   FILE *ifile, *ofile;
00525   char *inputfile_name = "wbtest.txt";
00526   char *outputfile_name = "test.data";
00527 
00528   if (argc != 3)
00529     {
00530       printf("%s inputfile outputfile\n", argv[0]);
00531       exit(0);
00532     }
00533 
00534   inputfile_name = argv[1];
00535   outputfile_name = argv[2];
00536 
00537   /*
00538     printf("Begin converting from binary file to text file:\n");
00539     printf("Binary file:%s\nText file:%s\n",inputfile_name, outputfile_name);
00540   */
00541 
00542   ifile = fopen(inputfile_name, "r");
00543   if (! ifile) 
00544     {
00545       perror(inputfile_name);
00546       exit(1);
00547     }
00548 
00549   ofile = fopen(outputfile_name, "w+");
00550   if (! ofile) 
00551     {
00552       fclose(ifile);
00553       exit(1);
00554     }
00555 
00556   ReadInput(ifile);
00557   fclose(ifile);
00558 
00559   /*
00560     PrintTableInfo();
00561     PrintNodeList();
00562   */
00563 
00564   Output(ofile);
00565   fclose(ofile);
00566 
00567   /*
00568     printf("Completed converting from binary file to text file\n");
00569   */
00570   exit(0);
00571 }
00572