Back to index

im-sdk  12.3.91
x_txt2bin.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 <ctype.h>
00043 #include <stdio.h>
00044 #include <string.h>
00045 #include <stdlib.h>
00046 #include <unistd.h>
00047 #include "codetable.h"
00048 #include "ctfile.h"
00049 #include "encode.h"
00050 
00051 #define MAX_LINE_LEN 256
00052 
00053 typedef struct _dynNode {
00054   int keycode;
00055   int modifier;
00056   char     *hzptr;          /* HZ strings */
00057   struct _dynNode *next;    /* next node in the same level */
00058   struct _dynNode *son;     /* next node down one level */
00059 } dynNode;
00060 
00061 static dynNode rootNode = {
00062   0,0, NULL, (struct _dynNode *)NULL, (struct _dynNode *)NULL
00063 };
00064 
00065 static dynNode *root = &rootNode;  /* root pointer to the dynNode */
00066 
00067 CodeTableStruct hzCodeTable;
00068 
00069 tableNode *nodeList;        /* all Nodes will be linearized to put here */
00070 unsigned char *hzList;             /* all HZ codes will be put here */
00071 unsigned  int pNodeList;    /* pointer to Node List */
00072 unsigned  int pHZList;             /* pointer to HZ List */
00073 
00074 static int totalHZbytes = 0;       /* total bytes of HZs */
00075 static int totalTableNode = 1;     /* total # of Node nodes (including root) */
00076 
00077 int lineno = 0;
00078 unsigned int clen = 0;
00079 
00080 char comment[MAX_COMMENT_LEN];
00081 keyPrompt keyprompt[MAX_USEDCODES_NUM];
00082 functionKey functionkey[MAX_FUNCTIONKEY_NUM];
00083 
00084 /*
00085  * When we read the file line by line, keys-HZs mapping are inserted
00086  * into the dynNode.  After all lines are finished, the dynNode is
00087  * reorganized (optimized and linearized) into a static tableNode structures,
00088  *
00089  * Phrase (string of multiple hanzi) is embedded into hanzi strings.
00090  * A phrase is start with 2 bytes: (1, n), where n is the length
00091  * of the phrase in byte.  { The tag (0, n) is not good for strcpy, etc. }
00092  */
00093 
00094 typedef struct _str_int {
00095   char  *string;
00096   int   value;
00097 } string_value_pair;
00098 
00099 string_value_pair selectkeymode_str_val[] = {
00100   { NUMBER_MODE_STR, NUMBER_MODE },
00101   { LOWER_MODE_STR,  LOWER_MODE },
00102   { UPPER_MODE_STR,  UPPER_MODE },
00103   { NULL,            NUMBER_MODE }
00104 };
00105 
00106 int str2val(string_value_pair *str_val, char *str)
00107 {
00108   while (str_val->string) {
00109     /*
00110       printf("%d: %s\n", str_val->value, str_val->string);
00111     */
00112     if (!strncasecmp (str_val->string, str, strlen(str_val->string)))
00113       return (str_val->value);
00114     str_val++;
00115   }
00116   return (str_val->value);
00117 }
00118 
00119 char *val2str(string_value_pair *str_val, int value)
00120 {
00121   while (str_val->string) {
00122     if (str_val->value == value)
00123       return (str_val->string);
00124     str_val++;
00125   }
00126   return (str_val->string);
00127 }
00128 
00129 char *skip_space(char *s)
00130 {
00131   while (*s && (*s==' ' || *s=='\t')) s++;
00132   return s;
00133 }
00134 
00135 char *to_space(char *s)
00136 {
00137   while (*s && !isspace(*s)) s++;
00138   return s;
00139 }
00140 
00141 /* convert string like "^A" to integrate */
00142 int convert_octnum(buf)
00143      char buf[];
00144 {
00145   register char *p1 = buf, *p2 = buf;
00146   int bCtrlStatus = 0;
00147 
00148   while (*p1) {
00149     if (isspace(*p1) || (*p1&0x80)) {   
00150       p1++;
00151       continue;
00152     }
00153     if (*p1 == '^') {
00154       bCtrlStatus = 1;
00155       p1++;
00156       continue;
00157     }
00158     if (bCtrlStatus) {
00159       /*
00160        printf("Ctrl: %c, upper(%c): %c, 0x%x, 0x%x\n",
00161        *p1, *p1, toupper(*p1),
00162        toupper(*p1), toupper(*p1)|0x80); 
00163       */
00164       if (*p1 >= 'a' && *p1 <= 'z') {
00165        *p2++ = toupper(*p1) | 0x80;
00166       } else if (*p1 >= 'A' && *p1 <= 'Z') {
00167        *p2++ = *p1 | 0x80;
00168       } else if (index(",/;'[]-=", *p1)) {
00169        *p2++ = *p1 | 0x80;
00170       } else if (*p1 == '?') {
00171        *p2++ = '/' | 0x80;
00172       } else if (*p1 == '<') {
00173        *p2++ = ',' | 0x80;
00174       } else if (*p1 == '>') {
00175        *p2++ = '.' | 0x80;
00176       } else if (*p1 == ':') {
00177        *p2++ = ';' | 0x80;
00178       }
00179 
00180       p1++;
00181       bCtrlStatus = 0;
00182       continue;
00183     }
00184     *p2++ = *p1++;
00185   }
00186   *p2 = '\0';
00187 
00188   return(0);
00189 }
00190 
00191 /* Not in use 
00192 static void Warning(str)
00193      char *str;
00194 {
00195   fprintf (stderr, "Warning: %s (at line %d)\n", str, lineno);
00196 }
00197 */
00198 
00199 static void Error(str)
00200      char *str;
00201 {
00202   fprintf (stderr, "%s (at line %d)\n", str, lineno);
00203   exit (1);
00204 }
00205 
00206 static dynNode *NewNode(keycode, modifier, son, next)
00207      int keycode;
00208      int modifier;
00209      dynNode *son;
00210      dynNode *next;
00211 {
00212   register dynNode *t = (dynNode *) malloc (sizeof(dynNode));
00213 
00214   if (t == NULL)
00215     Error ("Run out of memory");
00216   t->hzptr = NULL;
00217   t->keycode = keycode;
00218   t->modifier = modifier;
00219   t->son = son;
00220   t->next = next;
00221   if (keycode)
00222     totalTableNode++ ;
00223   return (t);
00224 }
00225 
00226 /* 
00227  * InsertNode -- insert the keys-HZs pair into dynNode structure
00228  */
00229 static void InsertNode(kptr, hzptr, tlen)
00230      char *kptr;
00231      char *hzptr;
00232      int tlen;
00233 {
00234   register dynNode *tptr = root;
00235   int kc, mo;
00236   char *tok;
00237   int flag;
00238 
00239   kc=mo=0;
00240 
00241   if ((tok = (char *)strtok((char *)kptr,":")) == (char *)NULL){
00242     tok = "";
00243   }
00244   kc = atoi(tok);
00245   if ((tok = (char *)strtok((char *)NULL,":")) == (char *)NULL){
00246     tok = "";
00247   }
00248   mo = atoi(tok);
00249   flag = 0;
00250   printf("InsertNode: kc[%d], mo[%d], tlen[%d]\n",kc,mo,tlen);
00251   while (tlen--) {
00252     /*
00253       printf("tlen [%d]\n",tlen);
00254     */
00255     if(flag){
00256       if ((tok = (char *)strtok((char *)NULL,":")) == (char *)NULL){
00257         tok = "";
00258       }
00259       kc = atoi(tok);
00260       if ((tok = (char *)strtok((char *)NULL,":")) == (char *)NULL){
00261         tok = "";
00262       }
00263       mo = atoi(tok);
00264       printf("InsertNode: kc[%d], mo[%d], tlen[%d]\n",kc,mo,tlen);
00265     }
00266     flag = 1;
00267     if (tptr->son == NULL) {
00268       tptr->son = NewNode(kc, mo, (dynNode *)NULL, (dynNode *)NULL);
00269       tptr = tptr->son;
00270     } else if ((tptr->son->keycode > kc) || ((tptr->son->keycode == kc) && (tptr->son->modifier < mo))) {
00271       tptr->son = NewNode(kc, mo, (dynNode *)NULL, tptr->son);
00272       tptr = tptr->son;
00273     } else if ((tptr->son->keycode == kc) && (tptr->son->modifier == mo)) {
00274       tptr = tptr->son;
00275     } else {
00276       tptr = tptr->son;
00277       while ((tptr->next != NULL) && ((tptr->next->keycode < kc) || ((tptr->next->keycode == kc) && (tptr->next->modifier < mo)))) {
00278        tptr = tptr->next;   /* try next */
00279       }
00280       if ((tptr->next == NULL) || (tptr->next->keycode > kc) || ((tptr->next->keycode == kc) && (tptr->next->modifier > mo))) {
00281        tptr->next = NewNode(kc, mo, (dynNode *)NULL, tptr->next);
00282        tptr = tptr->next;
00283       } else {       /* tptr->next->key == key */
00284        tptr = tptr->next;
00285       }
00286     }
00287     kc=mo=0;
00288   }
00289 
00290   /* come to the end of the key string kptr */
00291 
00292   if (tptr->son == NULL) {
00293     tptr->son = NewNode (0,0, (dynNode *)NULL, (dynNode *)NULL);
00294     tptr->son->hzptr = (char *)malloc (strlen (hzptr) + 1);
00295     if (tptr->son->hzptr == NULL)
00296       Error ("Run out of memory");
00297     strcpy (tptr->son->hzptr, hzptr);
00298   } else if (tptr->son->keycode != 0) {
00299     /* new key should be the 1st son, the old one becomes the next */
00300     tptr->son = NewNode(0,0, (dynNode *)NULL, tptr->son);
00301     tptr->son->hzptr = (char *)malloc (strlen (hzptr) + 1);
00302     if (tptr->son->hzptr == NULL)
00303       Error ("Run out of memory");
00304     strcpy (tptr->son->hzptr, hzptr);
00305   } else {
00306     tptr->son->hzptr = (char *)realloc (tptr->son->hzptr,
00307                                    strlen (tptr->son->hzptr) + strlen (hzptr) + 1);
00308     if (tptr->son->hzptr == NULL)
00309       Error ("Run out of memory");
00310     strcat (tptr->son->hzptr, hzptr);
00311   }
00312   totalHZbytes += strlen (hzptr);
00313 }
00314 
00315 /*
00316  * Linearize -- make the node tree into a linear array, for I/O.
00317  *            returns the number of choices under the node.
00318  */
00319 static int Linearize(idxNode, tablenode)
00320      unsigned int idxNode;
00321      register dynNode *tablenode;
00322 {
00323   register dynNode *tptr = tablenode->son;
00324   register unsigned int i, hzlen;
00325   dynNode *tptr2;
00326   unsigned int bNodeList = pNodeList;
00327   unsigned int bHZList = pHZList;
00328   int numChoice = 0;
00329 
00330   /* if leaf node */
00331   if (tptr->keycode == 0) {
00332     char *hzptr = tptr->hzptr;
00333 
00334     while (*hzptr) {
00335       if (*hzptr != HZ_PHRASE_TAG) {
00336        /* a single character */
00337        hzlen = get_char_len_by_encodeid(hzCodeTable.Encode, hzptr);
00338       } else {
00339        /* a phrase:  "PHRASE flag" | length | Phrase String */
00340        hzlen = *(hzptr+1) + 2;
00341       }
00342       for (i = 0; i < hzlen; i++) {
00343        hzList[pHZList++] = *hzptr++;
00344       }
00345 
00346       numChoice ++ ;
00347     }
00348     tptr = tptr->next;
00349   }
00350 
00351   tptr2 = tptr;      /* save for second pass */
00352   while (tptr) {
00353     nodeList[ pNodeList].keycode = tptr->keycode;
00354     nodeList[ pNodeList].modifier = tptr->modifier;
00355     pNodeList++;
00356     tptr = tptr->next;
00357   }
00358 
00359   nodeList[ idxNode ].pos_NextKey = bNodeList;
00360   nodeList[ idxNode ].num_NextKeys = pNodeList - bNodeList;
00361   nodeList[ idxNode ].pos_HZidx = bHZList;
00362   nodeList[ idxNode ].num_HZchoice = numChoice;
00363 
00364   /* linearize every sibling node */
00365   for (tptr = tptr2, i = bNodeList; tptr; i++, tptr = tptr->next) {
00366     numChoice += Linearize (i, tptr); 
00367   }
00368 
00369   /*
00370     printf("%c === num:%d\n", tptr2->key ,numChoice);
00371   */
00372   return (numChoice);
00373 }
00374 
00375 /*
00376  * BuildTableNode -- reorganize the dynNode into static tableNode structure
00377  */
00378 static void BuildTableNode()
00379 {
00380   nodeList = (tableNode *) calloc (totalTableNode, sizeof(tableNode));
00381   hzList = (unsigned char *) malloc (totalHZbytes);
00382   if ((! hzList) || (! nodeList)) {
00383     perror ("BuildTableNode");
00384     exit (1);
00385   }
00386 
00387   pNodeList = 1;     /* 0 is the root, start from index 1 */
00388   pHZList = 0;
00389 
00390   (void) Linearize(0, root);
00391   nodeList[0].num_HZchoice = 0;    /* no choice for the upmost node */
00392   nodeList[0].pos_HZidx = 0;
00393 
00394   /* ASSERT: pNodeList = totalTableNode, pHZList = totalHZbytes/2 */
00395   hzCodeTable.sizeNodeList = pNodeList;
00396   hzCodeTable.sizeHZList = pHZList;
00397 }
00398 
00399 void PrintNodeList()
00400 {
00401   int i;
00402 
00403   for (i=0; i<totalTableNode; i++)
00404     {
00405       printf("id :%d\n", i);
00406       printf("keycode: %d\n", nodeList[i].keycode);
00407       printf("modifier: %d\n", nodeList[i].modifier);
00408       printf("num_NextKeys: %d\n", nodeList[i].num_NextKeys);
00409       printf("num_HZchoice: %d\n", nodeList[i].num_HZchoice);
00410       printf("pos_NextKey: %d\n", nodeList[i].pos_NextKey);
00411       printf("pos_HZidx: %d\n", nodeList[i].pos_HZidx);
00412       printf("\n");
00413     }
00414 }
00415 
00416 void PrintNode (tptr)
00417      dynNode *tptr;
00418 {
00419   if (tptr == NULL) return;
00420 
00421   if (tptr->keycode != 0)
00422     printf("[%d] [%d]", tptr->keycode, tptr->modifier);
00423 
00424   if (tptr->hzptr)
00425     printf("PrintNode: [%s]\n", tptr->hzptr);
00426 
00427   PrintNode(tptr->son);
00428   PrintNode(tptr->next);
00429   printf("\n");
00430 
00431 }
00432 
00433 void PrintTableInfo()
00434 {
00435   int i;
00436 
00437   printf("Lname:%s\n", hzCodeTable.Lname);
00438   printf("Cname:%s\n", hzCodeTable.Cname);
00439   printf("WildChar:%s\n", hzCodeTable.WildChar);
00440   printf("UsedCodes:%s\n", hzCodeTable.UsedCodes);
00441   printf("MaxCodes:%d\n", hzCodeTable.MaxCodes);
00442   printf("Encode:%d\n", hzCodeTable.Encode);
00443   printf("sizeNodeList:%d\n", hzCodeTable.sizeNodeList);
00444   printf("sizeHZList:%d\n", hzCodeTable.sizeHZList);
00445 
00446   printf("key_prompt\n");
00447   for (i=0; i<MAX_USEDCODES_NUM; i++)
00448     {
00449       if (keyprompt[i].prompt[0])
00450        printf("%c:  %s\n", i, keyprompt[i].prompt);
00451     }
00452 
00453   printf("function_key\n");
00454   for (i=0; i<MAX_FUNCTIONKEY_NUM; i++)
00455     {
00456       if (functionkey[i].keylist[0])
00457        printf("%d:  %d\n", i, functionkey[i].keylist[0]);
00458     }
00459   comment[clen] = '\0';
00460   printf("comment is \n%s\n", comment);
00461 }
00462 
00463 static void InitTable()
00464 {
00465   strcpy(hzCodeTable.Lname, DEFAULT_CODETABLE_NAME);
00466   strcpy(hzCodeTable.Cname, DEFAULT_CODETABLE_NAME);
00467   strcpy(hzCodeTable.UsedCodes, DEFAULT_USEDCODES);
00468   strcpy(hzCodeTable.WildChar, DEFAULT_WILDCHARS);
00469   hzCodeTable.Encode = 0;   
00470   hzCodeTable.MaxCodes = DEFAULT_MAXCODES & 0xff;       
00471 
00472   hzCodeTable.sizeHZList = 0;
00473   hzCodeTable.sizeNodeList = 0;
00474   hzCodeTable.bSectionsFlag = 0;
00475 
00476   hzCodeTable.nKeyByKeyMode = ON_MODE;
00477   hzCodeTable.nHelpInfoMode = OFF_MODE;
00478   hzCodeTable.nAutoSelectMode = OFF_MODE;
00479   hzCodeTable.nKeyPromptMode = ON_MODE;
00480   hzCodeTable.nDisplayOnSpotMode = ON_MODE;
00481   hzCodeTable.nSelectKeyMode = NUMBER_MODE;
00482 
00483   memset(&(keyprompt[0]), 0, sizeof(keyprompt));
00484   memset(&(functionkey[0]), 0, sizeof(functionkey));
00485 }
00486 
00487 /*
00488  * ReadInput -- read and parse Input Text CodeTable format file
00489  */
00490 static void ReadInput(ifile)
00491      FILE *ifile;
00492 {
00493 
00494   char line_buf[256], line[MAX_LINE_LEN];
00495   char keybuf[MAX_LINE_LEN], valuebuf[MAX_LINE_LEN];
00496   char *key, *value_str, *kptr, *sptr, *vptr;
00497   register char *ptr;
00498   int  endofstr, ret, len, line_index;
00499   int count, token_len;
00500 
00501   int flag_section = DEFAULT_SECTION;
00502 
00503   while(fgets(line_buf, 255, ifile) != NULL) {
00504 
00505     count = token_len = 0;
00506     lineno++;
00507 
00508     if((line_buf[0] == '#') && (line_buf[1] == '#')){
00509       /*
00510        printf("COMMENTS \n");
00511       */
00512       continue;
00513     }
00514 
00515     value_str = line_buf;
00516     ptr = skip_space(value_str);
00517 
00518     if (*ptr == '\0') break;
00519 
00520     /* if is space line, get new line */
00521     if (flag_section != COMMENT_SECTION) {
00522       if (*ptr == '\n')
00523        continue;
00524     }
00525 
00526     line_index = 0;
00527     while(*ptr != '\n' && *ptr != '\0' && line_index < MAX_LINE_LEN) 
00528       line[line_index++] = *ptr++;
00529 
00530     /* trim right space */
00531     while (line_index > 0 && isspace(line[line_index-1])) line_index--;
00532     line[line_index] = '\0';
00533 
00534     key = line;
00535     len = strlen(line);
00536     /*
00537       printf("len:%d, %s\n", strlen(line), line);
00538     */
00539     if (line[0] == '[' && line[len-1] == ']') {
00540 
00541       /* get section name of the item */
00542       ptr = line + 1;
00543       while(isspace(*ptr)) ptr++;
00544       key = ptr;
00545 
00546       ptr = line + len - 2;
00547       while(isspace(*ptr)) ptr--;
00548       *(ptr+1) = '\0';
00549 
00550       if (!*key) continue;
00551 
00552       if (!(strncasecmp(key, DESCRIPTION_STR, strlen(DESCRIPTION_STR)))) {
00553 
00554        flag_section = DESCRIPTION_SECTION;
00555        continue;
00556 
00557       } else if (!(strncasecmp(key, COMMENT_STR, strlen(COMMENT_STR)))) {
00558 
00559        BITSET(hzCodeTable.bSectionsFlag , COMMENT_SECTION);
00560        flag_section = COMMENT_SECTION;
00561        continue;
00562 
00563       } else if (!(strncasecmp(key, KEYPROMPT_STR, strlen(KEYPROMPT_STR)))) {
00564 
00565        BITSET(hzCodeTable.bSectionsFlag , KEYPROMPT_SECTION);
00566        flag_section = KEYPROMPT_SECTION;
00567        continue;
00568 
00569       } else if (!(strncasecmp(key, FUNCTIONKEY_STR, strlen(FUNCTIONKEY_STR)))) {
00570        
00571        BITSET(hzCodeTable.bSectionsFlag , FUNCTIONKEY_SECTION);
00572        flag_section = FUNCTIONKEY_SECTION;
00573        continue;
00574 
00575       } else if (!(strncasecmp(key, PHRASE_STR, strlen(PHRASE_STR)))) {
00576 
00577        BITSET(hzCodeTable.bSectionsFlag , PHRASE_SECTION);
00578        flag_section = PHRASE_SECTION;
00579        continue;
00580 
00581       } else if (!(strncasecmp(key, SINGLE_STR, strlen(SINGLE_STR)))) {
00582 
00583        BITSET(hzCodeTable.bSectionsFlag , SINGLE_SECTION);
00584        flag_section = SINGLE_SECTION;
00585        continue;
00586 
00587       }
00588       else if (!(strncasecmp(key, OPTIONS_STR, strlen(OPTIONS_STR)))) {
00589 
00590        BITSET(hzCodeTable.bSectionsFlag , OPTIONS_SECTION);
00591        flag_section = OPTIONS_SECTION;
00592        continue;
00593 
00594       }
00595     }
00596 
00597     switch (flag_section) {
00598               
00599     case DEFAULT_SECTION:
00600       break;
00601 
00602     case DESCRIPTION_SECTION:
00603       if (!(strncasecmp(key, LOCALE_STR, strlen(LOCALE_STR)))) {
00604        value_str = skip_space(key+strlen(LOCALE_STR));
00605        if ((*value_str == '\0') || (*value_str == '\n'))
00606          continue;
00607 
00608        strcpy(hzCodeTable.Lname, value_str);
00609        break;
00610       }
00611 
00612       if (!(strncasecmp(key, NAME_STR, strlen(NAME_STR)))) {
00613        value_str = skip_space(key+strlen(NAME_STR));
00614        if ((*value_str == '\0') || (*value_str == '\n'))
00615          continue;
00616 
00617        strcpy(hzCodeTable.Cname, value_str);
00618        break;
00619       }
00620 
00621       if (!(strncasecmp(key, ENCODE_STR, strlen(ENCODE_STR)))) {
00622        value_str = skip_space(key+strlen(ENCODE_STR));
00623        if ((*value_str == '\0') || (*value_str == '\n'))
00624          continue;
00625 
00626        hzCodeTable.Encode = get_encodeid_from_name(value_str);
00627        hzCodeTable.Output_Encode = hzCodeTable.Encode;
00628        break;
00629       }
00630 
00631       if (!(strncasecmp(key, USEDCODES_STR, strlen(USEDCODES_STR)))) {
00632        value_str = skip_space(key+strlen(USEDCODES_STR));
00633        if ((*value_str == '\0') || (*value_str == '\n'))
00634          continue;
00635 
00636        strncpy(hzCodeTable.UsedCodes, value_str, MAX_USEDCODES_NUM);
00637        break;
00638       }
00639 
00640       if (!(strncasecmp(key, WILDCHAR_STR, strlen(WILDCHAR_STR)))) {
00641        value_str = skip_space(key+strlen(WILDCHAR_STR));
00642        if ((*value_str == '\0') || (*value_str == '\n'))
00643          continue;
00644 
00645        strncpy(hzCodeTable.WildChar, value_str, MAX_WILDCHAR_NUM);
00646        break;
00647       }
00648 
00649       if (!(strncasecmp(key, MAXCODES_STR, strlen(MAXCODES_STR)))) {
00650        value_str = skip_space(key+strlen(MAXCODES_STR));
00651        if ((*value_str == '\0') || (*value_str == '\n'))
00652          continue;
00653 
00654        hzCodeTable.MaxCodes = atoi(value_str) & 0xff;
00655        break;
00656       }
00657 
00658       break;
00659 
00660     case OPTIONS_SECTION:
00661       /*
00662        printf("Options Section ====\n");
00663        printf("OPTION_OPEN_STR:%s\n", OPTION_OPEN_STR);
00664       */
00665 
00666       if (!(strncasecmp(key,KEYBYKEY_MODE_STR,strlen(KEYBYKEY_MODE_STR)))){
00667        value_str = skip_space(key+strlen(KEYBYKEY_MODE_STR));
00668        if ((*value_str == '\0') || (*value_str == '\n'))
00669          continue;
00670        /*
00671          printf("keybykey: %s\n", value_str);
00672        */
00673        if (!(strncasecmp(value_str,OPTION_OPEN_STR, strlen(OPTION_OPEN_STR))))
00674          hzCodeTable.nKeyByKeyMode = ON_MODE;
00675        else 
00676          hzCodeTable.nKeyByKeyMode = OFF_MODE;
00677        break;
00678       }
00679 
00680       if (!(strncasecmp(key,HELPINFO_MODE_STR,strlen(HELPINFO_MODE_STR)))){
00681        value_str = skip_space(key+strlen(HELPINFO_MODE_STR));
00682        if ((*value_str == '\0') || (*value_str == '\n'))
00683          continue;
00684        /*
00685          printf("HelpInfo: %s\n", value_str);
00686        */
00687        if (!(strncasecmp(value_str,OPTION_OPEN_STR, strlen(OPTION_OPEN_STR))))
00688          hzCodeTable.nHelpInfoMode = ON_MODE;
00689        else 
00690          hzCodeTable.nHelpInfoMode = OFF_MODE;
00691        break;
00692       }
00693 
00694       if (!(strncasecmp(key,AUTOSELECT_MODE_STR,strlen(AUTOSELECT_MODE_STR)))){
00695        value_str = skip_space(key+strlen(AUTOSELECT_MODE_STR));
00696        if ((*value_str == '\0') || (*value_str == '\n'))
00697          continue;
00698        /*
00699          printf("AutoSelect: %s\n", value_str);
00700        */
00701        if (!(strncasecmp(value_str,OPTION_OPEN_STR, strlen(OPTION_OPEN_STR))))
00702          hzCodeTable.nAutoSelectMode = ON_MODE;
00703        else 
00704          hzCodeTable.nAutoSelectMode = OFF_MODE;
00705        break;
00706       }
00707 
00708       if (!(strncasecmp(key,KEYPROMPT_MODE_STR,strlen(KEYPROMPT_MODE_STR)))){
00709        value_str = skip_space(key+strlen(KEYPROMPT_MODE_STR));
00710        if ((*value_str == '\0') || (*value_str == '\n'))
00711          continue;
00712        /*
00713          printf("KeyPromptMode: %s\n", value_str);
00714        */
00715        if (!(strncasecmp(value_str,OPTION_OPEN_STR, strlen(OPTION_OPEN_STR))))
00716          hzCodeTable.nKeyPromptMode = ON_MODE;
00717        else 
00718          hzCodeTable.nKeyPromptMode = OFF_MODE;
00719        break;
00720       }
00721 
00722       if (!(strncasecmp(key,DISPLAYONSPOT_MODE_STR,strlen(DISPLAYONSPOT_MODE_STR)))){
00723        value_str = skip_space(key+strlen(DISPLAYONSPOT_MODE_STR));
00724        if ((*value_str == '\0') || (*value_str == '\n'))
00725          continue;
00726        /*
00727          printf("DisplayOnSpotMode: %s\n", value_str);
00728        */
00729        if (!(strncasecmp(value_str,OPTION_OPEN_STR, strlen(OPTION_OPEN_STR))))
00730          hzCodeTable.nDisplayOnSpotMode = ON_MODE;
00731        else 
00732          hzCodeTable.nDisplayOnSpotMode = OFF_MODE;
00733        break;
00734       }
00735 
00736       if (!(strncasecmp(key,SELECTKEY_MODE_STR,strlen(SELECTKEY_MODE_STR)))){
00737        value_str = skip_space(key+strlen(SELECTKEY_MODE_STR));
00738        if ((*value_str == '\0') || (*value_str == '\n'))
00739          continue;
00740        /*
00741          printf("SelectKeyMode: %s\n", value_str);
00742        */
00743        hzCodeTable.nSelectKeyMode = str2val(selectkeymode_str_val, value_str);
00744        break;
00745       }
00746 
00747       break;
00748 
00749     case COMMENT_SECTION:
00750       ptr = line_buf;
00751       while (*ptr && clen < MAX_COMMENT_LEN)
00752        comment[clen++] = *ptr++;
00753       break;
00754               
00755     case FUNCTIONKEY_SECTION:
00756       if (!(strncasecmp(key,PAGEUP_KEY_STR,strlen(PAGEUP_KEY_STR)))){
00757        value_str = skip_space(key+strlen(PAGEUP_KEY_STR));
00758        if ((*value_str == '\0') || (*value_str == '\n'))
00759          continue;
00760 
00761        /*
00762          printf("pageup: %s\n", value_str);
00763        */
00764        ret = convert_octnum(value_str);
00765        if (ret == -1)
00766          Error("FunctionKey Error Defination");
00767 
00768        strncpy(functionkey[PAGEUP_KEY_ID].keylist, value_str, MAX_FUNCTIONKEY_LEN);
00769        break;
00770       }
00771 
00772       if (!(strncasecmp(key,PAGEDOWN_KEY_STR,strlen(PAGEDOWN_KEY_STR)))){
00773        value_str = skip_space(key+strlen(PAGEDOWN_KEY_STR));
00774        if ((*value_str == '\0') || (*value_str == '\n'))
00775          continue;
00776 
00777        /*
00778          printf("pagedown: %s\n", value_str);
00779        */
00780        ret = convert_octnum(value_str);
00781        if (ret == -1)
00782          Error("FunctionKey Error Defination");
00783 
00784        strncpy(functionkey[PAGEDOWN_KEY_ID].keylist, value_str, MAX_FUNCTIONKEY_LEN);
00785       }
00786 
00787       if (!(strncasecmp(key,BACKSPACE_KEY_STR,strlen(BACKSPACE_KEY_STR)))){
00788        value_str = skip_space(key+strlen(BACKSPACE_KEY_STR));
00789        if ((*value_str == '\0') || (*value_str == '\n'))
00790          continue;
00791 
00792        /*
00793          printf("backspace: %s\n", value_str);
00794        */
00795        ret = convert_octnum(value_str);
00796        if (ret == -1)
00797          Error("FunctionKey Error Defination");
00798 
00799        strncpy(functionkey[BACKSPACE_KEY_ID].keylist, value_str, MAX_FUNCTIONKEY_LEN);
00800        break;
00801       }
00802                      
00803       if (!(strncasecmp(key,CLEARALL_KEY_STR,strlen(CLEARALL_KEY_STR)))){
00804        value_str = skip_space(key+strlen(CLEARALL_KEY_STR));
00805        if ((*value_str == '\0') || (*value_str == '\n'))
00806          continue;
00807 
00808        /*
00809          printf("deleteall: %s\n", value_str);
00810        */
00811        ret = convert_octnum(value_str);
00812        if (ret == -1)
00813          Error("FunctionKey Error Defination");
00814 
00815        strncpy(functionkey[CLEARALL_KEY_ID].keylist, value_str, MAX_FUNCTIONKEY_LEN);
00816       }
00817 
00818       break;
00819 
00820     case KEYPROMPT_SECTION:
00821       ptr = key;
00822       kptr = keybuf;
00823 
00824       /* get key string */
00825       while (*ptr && (*ptr!=' ') && (*ptr!='\t') && (!(*ptr & 0x80))){
00826        *(kptr++) = *(ptr++);
00827       }
00828 
00829       *kptr = '\0';
00830 
00831       /* if no key string or no prompt string */
00832       if (!(*ptr) || !(keybuf[0])) break;
00833 
00834       /* get prompt characters string */
00835       value_str = skip_space(ptr);
00836                      
00837       /* if no single character string */
00838       if ((*value_str == '\0') || (*value_str == '\n'))
00839        break;
00840 
00841       /* only get first string */
00842       vptr = value_str;
00843       sptr = to_space(vptr);
00844       *sptr = '\0';
00845       /*
00846        printf("key:%s, keyprompt:%s====\n", keybuf, value_str);
00847       */
00848       strncpy(keyprompt[keybuf[0]].prompt, value_str, MAX_KEYPROMPT_LEN);
00849       break;
00850 
00851     case PHRASE_SECTION:
00852       /*
00853        printf("line:%s====\n", key);
00854       */
00855       ptr = key;
00856       kptr = keybuf;
00857 
00858       /* get key string */
00859       while (*ptr && (*ptr!='\t') && (!(*ptr & 0x80))){
00860         if(*ptr == ':') count++;
00861        *(kptr++) = *(ptr++);
00862       }
00863       token_len = (count+1)/2;
00864       *kptr = '\0';
00865 
00866       /* if no key string or no phrase string */
00867       if (!(*ptr) || !(keybuf[0])) break;
00868 
00869       /* get phrase string */
00870       value_str = skip_space(ptr);
00871                      
00872       /* if no phrase character string */
00873       if ((*value_str == '\0') || (*value_str == '\n'))
00874        break;
00875 
00876       /*
00877        printf("key:%s, value:%s====\n", keybuf, value_str);
00878       */
00879       /* seperate the phrase string into single phrase */
00880       vptr = value_str;
00881       endofstr = 0;
00882       while (*vptr)
00883        {
00884          sptr = vptr;
00885          /*
00886            if ((*sptr == '#') && (*sptr++ == '#')){
00887            printf("Comment in the phrase line \n");
00888            break; 
00889            }
00890          */
00891 
00892          vptr = to_space(sptr);
00893 
00894          if (*vptr == '\0' || *vptr == '\n')
00895            endofstr = 1;
00896 
00897          *vptr = '\0';
00898          valuebuf[0] = HZ_PHRASE_TAG;
00899          valuebuf[1] = (unsigned char)(strlen(sptr));
00900          /*
00901            printf("str:%s, strlen:%d\n", sptr, strlen(sptr));
00902          */
00903          strcpy(valuebuf+2, sptr);
00904          InsertNode(keybuf, valuebuf, token_len);
00905                             
00906          if (endofstr)
00907            break;
00908 
00909          vptr = skip_space(vptr + 1);
00910          if (*vptr == '\n')
00911            break;
00912        }
00913       break;
00914     }
00915   } 
00916 }
00917 
00918 void Output (ofile)
00919      FILE *ofile;
00920 {
00921   int ver = CODETABLE_VERSION;
00922 
00923   /* write CodeTable File Flag */
00924   fwrite (CODETABLE_FLAG, strlen(CODETABLE_FLAG), 1, ofile);     
00925 
00926   /* write CodeTable Version Flag */
00927   fwrite((char *)(&ver), sizeof(int), 1, ofile); 
00928 
00929   /* write CodeTable Header Structure */
00930   if ((fwrite(&hzCodeTable, sizeof(CodeTableStruct), 1, ofile)) == 0)
00931     {
00932       perror ("Writing output file");
00933       exit (1);
00934     }
00935 
00936   /* write CodeTable Node List */
00937   if ((fwrite(nodeList, sizeof(tableNode), hzCodeTable.sizeNodeList, ofile))
00938       != hzCodeTable.sizeNodeList) 
00939     {
00940       perror ("Writing output file");
00941       exit (1);
00942     }
00943 
00944   /* write CodeTable HanZi List */
00945   if ((fwrite(hzList, sizeof(unsigned char), hzCodeTable.sizeHZList, ofile))
00946       != hzCodeTable.sizeHZList)
00947     {
00948       perror ("Writing output file");
00949       exit (1);
00950     }
00951 
00952   /* if [Key_Prompt] section exist, write CodeTable Key Prompt List */
00953   if (GETBIT(hzCodeTable.bSectionsFlag, KEYPROMPT_SECTION))
00954     {
00955       if ((fwrite(&(keyprompt[0]), MAX_USEDCODES_NUM, sizeof(keyPrompt), ofile))
00956          != sizeof(keyPrompt))
00957        {
00958          perror ("Writing output file");
00959          exit (1);
00960        }
00961     }
00962 
00963   /* if [Function_Key] section exist, write CodeTable Function Key List */
00964   if (GETBIT(hzCodeTable.bSectionsFlag, FUNCTIONKEY_SECTION))
00965     {
00966       if ((fwrite(&(functionkey[0]),MAX_FUNCTIONKEY_NUM,sizeof(functionKey), ofile))
00967          != sizeof(functionKey))
00968        {
00969          perror ("Writing output file");
00970          exit (1);
00971        }
00972     }
00973               
00974   /* if [Comment] section exist, write CodeTable Comment List */
00975   if (GETBIT(hzCodeTable.bSectionsFlag, COMMENT_SECTION))
00976     {
00977       if ((fwrite((char *)(&clen), sizeof(unsigned int), 1, ofile)) == 0) 
00978        {
00979          perror ("Writing output file");
00980          exit (1);
00981        }
00982 
00983       if ((fwrite(comment, 1, clen, ofile)) != clen)
00984        {
00985          perror ("Writing output file");
00986          exit (1);
00987        }
00988     }
00989 }
00990 
00991 int main(argc, argv)
00992      int argc;
00993      char *argv[];
00994 {
00995   FILE *ifile, *ofile;
00996   char *inputfile_name = "wbtest.txt";
00997   char *outputfile_name = "test.data";
00998 
00999   if (argc != 3)
01000     {
01001       printf("%s inputfile outputfile\n", argv[0]);
01002       exit(0);
01003     }
01004 
01005   inputfile_name = argv[1];
01006   outputfile_name = argv[2];
01007 
01008   /*
01009     printf("inputfile:%s, outputfile:%s\n",inputfile_name, outputfile_name);
01010   */
01011 
01012   ifile = fopen(inputfile_name, "r");
01013   if (! ifile) 
01014     {
01015       perror(inputfile_name);
01016       exit(1);
01017     }
01018 
01019   ofile = fopen(outputfile_name, "w+");
01020   if (! ofile) 
01021     {
01022       fclose(ifile);
01023       exit(1);
01024     }
01025 
01026   InitTable();
01027 
01028   ReadInput(ifile);
01029   fclose(ifile);
01030 
01031   PrintNode(root);
01032   BuildTableNode();
01033   PrintTableInfo();
01034   PrintNodeList();
01035   Output(ofile);
01036   fclose(ofile);
01037 
01038   exit(0);
01039 }
01040