Back to index

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