Back to index

im-sdk  12.3.91
phonetic_table.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <ctype.h>
00004 #include <locale.h>
00005 
00006 #include "encode.h"
00007 #include "phonetic_im.h"
00008 #include "logf.h"
00009 
00010 #define MAX_LINE_LEN  256
00011 
00012 int lineno = 0;
00013 
00014 char *skip_space(char *s)
00015 {
00016         while (*s && (*s==' ' || *s=='\t')) s++;
00017         return s;
00018 }
00019 
00020 int LoadTableHeader (file_name, indic_table)
00021 char *file_name;
00022 TableStruct *indic_table;
00023 {
00024        FILE *ifile;
00025   char line_buf[256], line[MAX_LINE_LEN];
00026   char *key, *value_str;
00027   register char *ptr;
00028   int len, line_index;
00029   int flag_section = DEFAULT_SECTION;
00030 
00031        log_f("phonetic_table: ==== LoadTableHeader ====\n");
00032 
00033        /* read table from file to memory buffer  */
00034        ifile = fopen (file_name, "r");
00035        if (! ifile) {
00036     fprintf(stderr, "Unable to open the input table file \"%s\"\n",file_name);
00037     return(-1);
00038   }
00039 
00040   while(fgets(line_buf, 255, ifile) != NULL) {
00041     lineno++;
00042 
00043     if((line_buf[0] == '#') && (line_buf[1] == '#')){
00044       log_f("COMMENTS \n");
00045       continue;
00046     }
00047 
00048               value_str = line_buf;
00049               ptr = skip_space(value_str);
00050 
00051               if (*ptr == '\0') break;
00052 
00053               /* if is space line, get new line 
00054               if (flag_section != COMMENT_SECTION) {
00055                      if (*ptr == '\n')
00056                             continue;
00057               }
00058     */
00059 
00060               line_index = 0;
00061               while(*ptr != '\n' && *ptr != '\0' && line_index < MAX_LINE_LEN) 
00062                      line[line_index++] = *ptr++;
00063 
00064               /* trim right space */
00065               while (line_index > 0 && isspace(line[line_index-1])) line_index--;
00066               line[line_index] = '\0';
00067 
00068               key = line;
00069               len = strlen(line);
00070               log_f("len:%d, %s\n", strlen(line), line);
00071               if (line[0] == '[' && line[len-1] == ']') {
00072                      /* get section name of the item */
00073                      ptr = line + 1;
00074                      while(isspace(*ptr)) ptr++;
00075                      key = ptr;
00076 
00077                      ptr = line + len - 2;
00078                      while(isspace(*ptr)) ptr--;
00079                      *(ptr+1) = '\0';
00080 
00081                      if (!*key) continue;
00082 
00083                      if (!(strncasecmp(key, DESCRIPTION_STR, strlen(DESCRIPTION_STR)))) {
00084                             flag_section = DESCRIPTION_SECTION;
00085                             continue;
00086               } 
00087               }
00088 
00089     switch (flag_section) {
00090                 case DEFAULT_SECTION:
00091                         break;
00092 
00093                 case DESCRIPTION_SECTION:
00094          if(!(strncasecmp(key, LOCALE_STR, strlen(LOCALE_STR)))) {
00095               value_str = skip_space(key+strlen(LOCALE_STR));
00096               if ((*value_str == '\0') || (*value_str == '\n'))
00097                       continue;
00098               strcpy(indic_table->Lname, value_str);
00099               break;
00100          }
00101 
00102                      if (!(strncasecmp(key, NAME_STR, strlen(NAME_STR)))) {
00103                             value_str = skip_space(key+strlen(NAME_STR));
00104                             if ((*value_str == '\0') || (*value_str == '\n'))
00105                                    continue;
00106 
00107                             strcpy(indic_table->Cname, value_str);
00108                             break;
00109                      }
00110 
00111                      if (!(strncasecmp(key, ENCODE_STR, strlen(ENCODE_STR)))) {
00112                             value_str = skip_space(key+strlen(ENCODE_STR));
00113                             if ((*value_str == '\0') || (*value_str == '\n'))
00114                                    continue;
00115 
00116                             indic_table->Encode = get_encodeid_from_name(value_str);
00117                             indic_table->Output_Encode = indic_table->Encode;
00118                             break;
00119                      }
00120 
00121                      if (!(strncasecmp(key, FUNCTION_STR, strlen(FUNCTION_STR)))) {
00122                             value_str = skip_space(key+strlen(FUNCTION_STR));
00123                             if ((*value_str == '\0') || (*value_str == '\n'))
00124                                    continue;
00125 
00126                             strcpy(indic_table->Fname, value_str);
00127                             break;
00128                      }
00129 
00130                      if (!(strncasecmp(key, USEDCODES_STR, strlen(USEDCODES_STR)))) {
00131                             value_str = skip_space(key+strlen(USEDCODES_STR));
00132                             if ((*value_str == '\0') || (*value_str == '\n'))
00133                                    continue;
00134 
00135                             strncpy(indic_table->UsedCodes, value_str, MAX_USEDCODES_NUM);
00136                             break;
00137                      }
00138               }
00139        } 
00140   fclose (ifile);
00141   return(0);
00142 }