Back to index

im-sdk  12.3.91
bin2txt.c
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-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 
00043 /* txt2bin.c */
00044 
00045 #include <stdio.h>
00046 #include "codetable.h"
00047 #include "ctfile.h"
00048 
00049 #define       MAX_CHAR_NUM_PER_LINE       80
00050 #define       MAX_WORD_NUM_PER_LINE       30
00051 
00052 CodeTableStruct hzCodeTable;
00053 
00054 tableNode *nodeList;        /* all Nodes will be linearized to put here */
00055 unsigned char *hzList;             /* all HZ codes will be put here */
00056 
00057 unsigned int clen = 0;
00058 unsigned char keystack[80];
00059 
00060 char comment[MAX_COMMENT_LEN];
00061 keyPrompt keyprompt[MAX_USEDCODES_NUM];
00062 functionKey functionkey[MAX_FUNCTIONKEY_NUM];
00063 
00064 typedef struct _str_int {
00065        char  *string;
00066        int   value;
00067 } string_value_pair;
00068 
00069 string_value_pair selectkeymode_str_val[] = {
00070        { NUMBER0_MODE_STR, NUMBER0_MODE },
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("Cname:%s\n", hzCodeTable.Cname);
00124        printf("WildChar:%s\n", hzCodeTable.WildChar);
00125        printf("UsedCodes:%s\n", hzCodeTable.UsedCodes);
00126        printf("MaxCodes:%d\n", hzCodeTable.MaxCodes);
00127        printf("Encode:%d\n", hzCodeTable.Encode);
00128        printf("sizeNodeList:%d\n", hzCodeTable.sizeNodeList);
00129        printf("sizeHZList:%d\n", hzCodeTable.sizeHZList);
00130 
00131        printf("key_prompt\n");
00132        for (i=0; i<MAX_USEDCODES_NUM; i++)
00133        {
00134               if (keyprompt[i].prompt[0])
00135                      printf("%c:  %s\n", i, keyprompt[i].prompt);
00136        }
00137 
00138        printf("function_key\n");
00139        for (i=0; i<MAX_FUNCTIONKEY_NUM; i++)
00140        {
00141               if (functionkey[i].keylist[0])
00142                      printf("%d:  %d\n", i, functionkey[i].keylist[0]);
00143        }
00144 
00145        comment[clen] = '\0';
00146        printf("comment is \n%s\n", comment);
00147 }
00148 
00149 /*
00150  * ReadInput -- read and parse input codetable binary format file
00151  */
00152 static void ReadInput(ifile)
00153 FILE *ifile;
00154 {
00155        char ctFlag[256];
00156        int  ver;
00157 
00158        /* Read CodeTable File Flag */
00159        if (fread (ctFlag, strlen(CODETABLE_FLAG), 1, ifile) != 1)
00160        {
00161               fprintf (stderr, "File read Error\n");
00162               exit(1);
00163        }
00164 
00165        if (strncmp (ctFlag, CODETABLE_FLAG, strlen(CODETABLE_FLAG)) != 0) {
00166               fprintf (stderr, "File is not in CodeTable format\n");
00167               exit(1);
00168        }
00169 
00170        /* Read CodeTable Version Flag */
00171        if (fread ((char *)(&ver), sizeof (int), 1, ifile) != 1) 
00172        {
00173               fprintf (stderr, "File read Error\n");
00174               exit(1);
00175        }
00176 
00177        if (ver != CODETABLE_VERSION)
00178        {
00179               fprintf (stderr, "File is not in correct Version Number\n");
00180               exit(1);
00181        }
00182 
00183 
00184        /* Read CodeTable Header Structure */
00185        if (fread (&hzCodeTable, sizeof(CodeTableStruct), 1, ifile) == 0) {
00186               fprintf (stderr, "File read Error\n");
00187               exit(1);
00188        }
00189 
00190        /* Read Node List and HanZi List */
00191        nodeList = (tableNode *) calloc(hzCodeTable.sizeNodeList,sizeof(tableNode));
00192        hzList = (unsigned char *) calloc(hzCodeTable.sizeHZList, sizeof(unsigned char));
00193        if ((! nodeList) || (! hzList)) {
00194               perror ("Memory allocation");
00195               exit (1);
00196        }
00197 
00198        if ((fread (nodeList, sizeof(tableNode), hzCodeTable.sizeNodeList, ifile)
00199               != hzCodeTable.sizeNodeList) ||
00200               (fread (hzList, sizeof(unsigned char), hzCodeTable.sizeHZList, ifile)
00201               != hzCodeTable.sizeHZList))
00202        {
00203               fprintf (stderr, "File Read Error\n");
00204               exit (1);
00205        }
00206 
00207        /* Read Key Prompt List */
00208        if (GETBIT(hzCodeTable.bSectionsFlag, KEYPROMPT_SECTION))
00209        {
00210               if ((fread ((char *)(&(keyprompt[0])), MAX_USEDCODES_NUM, sizeof(keyPrompt), ifile)) 
00211                      != sizeof(keyPrompt)) 
00212               {
00213                      fprintf (stderr, "File Read Error\n");
00214                      exit (1);
00215               }
00216        }
00217 
00218        /* Read Function Key List */
00219        if (GETBIT(hzCodeTable.bSectionsFlag, FUNCTIONKEY_SECTION))
00220        {
00221               if ((fread ((char *)(&(functionkey[0])), MAX_FUNCTIONKEY_NUM, sizeof(functionKey), ifile)) 
00222                      != sizeof(functionKey)) 
00223               {
00224                      fprintf (stderr, "File Read Error\n");
00225                      exit (1);
00226               }
00227        }
00228 
00229        /* Read Comment */
00230        if (GETBIT(hzCodeTable.bSectionsFlag, COMMENT_SECTION))
00231        {
00232               if ((fread ((char *)(&clen), sizeof (unsigned int), 1, ifile) != 1) ||
00233                      (fread (comment, 1, clen, ifile) != clen))
00234               {
00235                      clen = 0;
00236               }
00237               if (clen > MAX_COMMENT_LEN)
00238                      clen = MAX_COMMENT_LEN;
00239        }
00240 }
00241 
00242 /* output line by line while traveling the tableNodes */
00243 static void LineOut (top, nodeIdx, mode, ofile)
00244 int top;
00245 unsigned int nodeIdx;
00246 int mode;
00247 FILE *ofile;
00248 {
00249        register tableNode *tnptr = &nodeList[nodeIdx];
00250        unsigned char *phz;
00251        int i, j, hzlen, encode, cnt, nSingle, nPhrase;
00252        int num;   /* number of choices under the key sequence in stack */
00253        int max_char_num, bDup, newline;
00254 
00255        keystack[top] = tnptr->key;
00256        num = tnptr->num_HZchoice;
00257        encode = hzCodeTable.Encode;
00258 
00259 /*
00260        printf("top:%d, key:%c, num_NextKeys:%d, num:%d\n", top, tnptr->key, tnptr->num_NextKeys, num);
00261 */
00262        /* if there are strings in this node */
00263        if (num > 0){
00264               /* there are HZs exclusively under the keys, or no more input keys */
00265               
00266               /* compute the Single HZ num and Phrase num in this string */
00267               nSingle = 0; 
00268               nPhrase = 0;
00269               phz = &hzList[tnptr->pos_HZidx];
00270               for ( i = 0; i < num; i++ )
00271               {
00272                      if (*phz == HZ_PHRASE_TAG) {
00273                             phz += *(phz+1) + 2;
00274                             nPhrase ++;
00275                      } else {
00276                             hzlen = get_char_len_by_encodeid(encode, phz);
00277                             phz += hzlen;
00278                             nSingle ++;
00279                      }
00280               }
00281 
00282               if (mode == SINGLE_SECTION && nSingle == 0) return;
00283               if (mode == PHRASE_SECTION && nPhrase == 0) return;
00284  
00285               /* print Single HZ string or Phrase string */
00286               if (mode == SINGLE_SECTION)
00287                      max_char_num = MAX_WORD_NUM_PER_LINE;
00288               else 
00289                      max_char_num = MAX_CHAR_NUM_PER_LINE;
00290 
00291               cnt = 0;
00292               newline = 1;
00293               phz = &hzList[tnptr->pos_HZidx];
00294               for (i = 0; i < num; i++) {
00295                      /* begin a new line */
00296                      if (cnt >= max_char_num) {
00297                             cnt = 0;
00298                             newline = 1;
00299                      }
00300                      if (newline == 1) {
00301                             register int j;
00302 
00303                             /* print external code */
00304                             (void) putc ('\n', ofile);
00305                             for (j = 1; j <= top; j++) {
00306                                    (void) putc (keystack[j], ofile);
00307                             }
00308                             (void) putc ('\t', ofile);
00309                             newline = 0;
00310                      }
00311 
00312                      if (*phz == HZ_PHRASE_TAG) {
00313                             /* phrase */
00314                                    hzlen = *(phz+1);
00315                             phz += 2;
00316                             if (mode == PHRASE_SECTION) {
00317                                           for (j = 0; j < hzlen; j++) {
00318                                           (void) putc (*(phz+j), ofile);
00319                                           }
00320                                    (void) putc (' ', ofile);
00321                                    cnt += hzlen;
00322                             }
00323                      } else {
00324                             /* hanzi */
00325                                    hzlen = get_char_len_by_encodeid(encode, phz);
00326 
00327                             if (mode = SINGLE_SECTION) {
00328                                           for (j = 0; j < hzlen; j++) {
00329                                           (void) putc (*(phz+j), ofile);
00330                                           }
00331                                    cnt ++;
00332                             }
00333                      }
00334                      phz += hzlen;
00335               }
00336        }
00337 
00338        for (i = 0; i < tnptr->num_NextKeys; i++)
00339               LineOut (top + 1, tnptr->pos_NextKey + i, mode, ofile);
00340 }
00341 
00342 /* convert key value to "^A" format or normal visible character */
00343 static int Putc (ch, ofile)
00344 unsigned char ch;
00345 FILE *ofile;
00346 {
00347 /*
00348        printf("ch:  0x%x, 0x%x, %c\n", ch, ch & 0x7f, ch & 0x7f);
00349 */
00350 
00351        if (ch & 0x80) {
00352               fprintf (ofile, " ^%c", ch & 0x7f);
00353        } else {
00354                      (void) putc (ch, ofile);
00355        }
00356               return (1);
00357 }
00358 
00359 static void Output (ofile)
00360 FILE *ofile;
00361 {
00362        int i;
00363 
00364        fprintf (ofile, "# HANZI codetable input table\n");
00365 
00366        /* description section */
00367        fprintf (ofile, "\n[ %s ]\n", DESCRIPTION_STR);
00368        fprintf (ofile, "%s\t\t%s\n", UUID_STR, hzCodeTable.UUID);
00369        fprintf (ofile, "%s\t\t%s\n", VERSION_STR, hzCodeTable.Version);
00370        fprintf (ofile, "%s\t\t%s\n", ICONPATH_STR, hzCodeTable.IconPath);
00371        fprintf (ofile, "%s\t\t%s\n", AUTHOR_STR, hzCodeTable.Author);
00372        fprintf (ofile, "%s\t\t%s\n", COPYRIGHT_STR, hzCodeTable.Copyright);
00373        fprintf (ofile, "%s\t\t%s\n", HINTING_STR, hzCodeTable.Hinting);
00374        fprintf (ofile, "%s\t\t%s\n", NAME_STR, hzCodeTable.Cname);
00375        fprintf (ofile, "%s\t\t%s\n", ENCODE_STR, get_name_from_encodeid(hzCodeTable.Encode));
00376        fprintf (ofile, "%s\t%s\n", USEDCODES_STR, hzCodeTable.UsedCodes);
00377        fprintf (ofile, "%s\t%s\n", WILDCHAR_STR, hzCodeTable.WildChar);
00378        fprintf (ofile, "%s\t%d\n", MAXCODES_STR, hzCodeTable.MaxCodes);
00379        
00380        /* Key_Prompt section */
00381        if (GETBIT(hzCodeTable.bSectionsFlag, KEYPROMPT_SECTION))
00382        {
00383               fprintf (ofile, "\n[ %s ]\n", KEYPROMPT_STR);
00384               for (i=0; i<MAX_USEDCODES_NUM; i++)
00385               {
00386                      if (keyprompt[i].prompt[0] != 0)
00387                             fprintf (ofile, "%c\t%s\n", i, keyprompt[i].prompt);
00388               }
00389        }
00390 
00391        /* Function_Key section */
00392        if (GETBIT(hzCodeTable.bSectionsFlag, FUNCTIONKEY_SECTION))
00393        {
00394               fprintf (ofile, "\n[ %s ]\n", FUNCTIONKEY_STR);
00395               if (functionkey[PAGEUP_KEY_ID].keylist[0] != 0)
00396               {
00397                      fprintf(ofile, "%s\t\t", PAGEUP_KEY_STR);
00398                      for (i=0; i<MAX_FUNCTIONKEY_LEN; i++) {
00399                             if (functionkey[PAGEUP_KEY_ID].keylist[i])
00400                                    Putc(functionkey[PAGEUP_KEY_ID].keylist[i], ofile);
00401                      }
00402                      fprintf(ofile, "\n");
00403               }
00404 
00405               if (functionkey[PAGEDOWN_KEY_ID].keylist[0] != 0)
00406               {
00407                      fprintf(ofile, "%s\t", PAGEDOWN_KEY_STR);
00408                      for (i=0; i<MAX_FUNCTIONKEY_LEN; i++) {
00409                             if (functionkey[PAGEDOWN_KEY_ID].keylist[i])
00410                                    Putc(functionkey[PAGEDOWN_KEY_ID].keylist[i], ofile);
00411                      }
00412                      fprintf(ofile, "\n");
00413               }
00414 
00415               if (functionkey[BACKSPACE_KEY_ID].keylist[0] != 0)
00416               {
00417                      fprintf(ofile, "%s\t", BACKSPACE_KEY_STR);
00418                      for (i=0; i<MAX_FUNCTIONKEY_LEN; i++) {
00419                             if (functionkey[BACKSPACE_KEY_ID].keylist[i])
00420                                    Putc(functionkey[BACKSPACE_KEY_ID].keylist[i],ofile);
00421                      }
00422                      fprintf(ofile, "\n");
00423               }
00424 
00425               if (functionkey[CLEARALL_KEY_ID].keylist[0] != 0)
00426               {
00427                      fprintf(ofile, "%s\t", CLEARALL_KEY_STR);
00428                      for (i=0; i<MAX_FUNCTIONKEY_LEN; i++) {
00429                             if (functionkey[CLEARALL_KEY_ID].keylist[i])
00430                                    Putc(functionkey[CLEARALL_KEY_ID].keylist[i],ofile);
00431                      }
00432                      fprintf(ofile, "\n");
00433               }
00434        }
00435 
00436        /* Options section */
00437        if (GETBIT(hzCodeTable.bSectionsFlag, OPTIONS_SECTION))
00438        {
00439               fprintf (ofile, "\n[ %s ]\n", OPTIONS_STR);
00440               if (hzCodeTable.nKeyByKeyMode)
00441                      fprintf(ofile, "%s\t\t%s\n", KEYBYKEY_MODE_STR, OPTION_OPEN_STR);
00442               else
00443                      fprintf(ofile, "%s\t\t%s\n", KEYBYKEY_MODE_STR, OPTION_CLOSE_STR);
00444 
00445               if (hzCodeTable.nHelpInfoMode)
00446                      fprintf(ofile, "%s\t\t%s\n", HELPINFO_MODE_STR, OPTION_OPEN_STR);
00447               else
00448                      fprintf(ofile, "%s\t\t%s\n", HELPINFO_MODE_STR, OPTION_CLOSE_STR);
00449 
00450               if (hzCodeTable.nAutoSelectMode)
00451                      fprintf(ofile, "%s\t%s\n", AUTOSELECT_MODE_STR, OPTION_OPEN_STR);
00452               else
00453                      fprintf(ofile, "%s\t%s\n", AUTOSELECT_MODE_STR, OPTION_CLOSE_STR);
00454 
00455               if (hzCodeTable.nKeyPromptMode)
00456                      fprintf(ofile, "%s\t\t%s\n", KEYPROMPT_MODE_STR, OPTION_OPEN_STR);
00457               else
00458                      fprintf(ofile, "%s\t\t%s\n", KEYPROMPT_MODE_STR, OPTION_CLOSE_STR);
00459 /*
00460               if (hzCodeTable.nDisplayOnSpotMode)
00461                      fprintf(ofile, "%s\t%s\n", DISPLAYONSPOT_MODE_STR, OPTION_OPEN_STR);
00462               else
00463                      fprintf(ofile, "%s\t%s\n", DISPLAYONSPOT_MODE_STR, OPTION_CLOSE_STR);
00464 */
00465               fprintf(ofile, "%s\t\t%s\n", SELECTKEY_MODE_STR, val2str(selectkeymode_str_val, hzCodeTable.nSelectKeyMode));
00466        }
00467 
00468        /* comment section */
00469        if (GETBIT(hzCodeTable.bSectionsFlag, COMMENT_SECTION))
00470        {
00471               if (clen > 0)
00472               {
00473                      fprintf (ofile, "\n[ %s ]\n", COMMENT_STR);
00474                      fprintf (ofile, "%s\n", comment);
00475               }
00476        }
00477 
00478        /* single hanzi section */
00479        if (GETBIT(hzCodeTable.bSectionsFlag, SINGLE_SECTION))
00480        {
00481               fprintf (ofile, "\n[ %s ]", SINGLE_STR);
00482               LineOut(0, 0, SINGLE_SECTION, ofile);
00483               fprintf (ofile, "\n");
00484        }
00485 
00486        /* phrase section */
00487        if (GETBIT(hzCodeTable.bSectionsFlag, PHRASE_SECTION))
00488        {
00489               fprintf (ofile, "\n[ %s ]", PHRASE_STR);
00490               LineOut(0, 0, PHRASE_SECTION, ofile);
00491               fprintf (ofile, "\n");
00492        }
00493 }
00494 
00495 main(argc, argv)
00496 int argc;
00497 char *argv[];
00498 {
00499        FILE *ifile, *ofile;
00500         char *inputfile_name = "wbtest.txt";
00501         char *outputfile_name = "test.data";
00502 
00503         if (argc != 3)
00504         {
00505                 printf("%s inputfile outputfile\n", argv[0]);
00506                 exit(0);
00507         }
00508 
00509         inputfile_name = argv[1];
00510         outputfile_name = argv[2];
00511 
00512        printf("Begin converting from binary file to text file:\n");
00513         printf("Binary file:%s\nText file:%s\n",inputfile_name, outputfile_name);
00514 
00515        ifile = fopen(inputfile_name, "r");
00516        if (! ifile) 
00517        {
00518               perror(inputfile_name);
00519               exit(1);
00520        }
00521 
00522        ofile = fopen(outputfile_name, "w+");
00523        if (! ofile) 
00524        {
00525               fclose(ifile);
00526               exit(1);
00527        }
00528 
00529        ReadInput(ifile);
00530        fclose(ifile);
00531 
00532 /*
00533        PrintTableInfo();
00534        PrintNodeList();
00535 */
00536 
00537        Output(ofile);
00538        fclose(ofile);
00539 
00540        printf("Completed converting from binary file to text file\n");
00541        exit(0);
00542 }
00543