Back to index

im-sdk  12.3.91
thai_config.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "thai_keymap.h"
00003 
00004 #define MAX_LINE_LEN    256
00005 
00006 #define KEYMAP_STRING           "keymap"
00007 #define KEYMAP_NAME_STRING      "name"
00008 
00009 enum {
00010        THAI_DEFAULT_SECTION = 0,
00011        THAI_KEYMAP_SECTION,
00012 };
00013 
00014 /**************************************************************
00015 The format of Thai configure file as follow:
00016 
00017 [keymap name="ThaiKeymapName"]
00018 key     ThaiSymbol_Lower  ThaiSymbol_Upper
00019 
00020 **************************************************************/
00021 
00022 #define THAI_STANDARD_KEYMAP_NUM  3
00023 
00024 # if 0
00025 /* Thai Keymap on Solaris 8 */
00026 unsigned char _Thai_Enhanced_Kedmanee_Keymap[97] = {
00027 /*        !     "     #     $     %     &     '     (     )     *     +     ,     -     .     /      */
00028 /*        +     .                                                                       */
00029     0x20, 0x2b, 0x2e, 0xf2, 0xf3, 0xf4, 0xdf, 0xa7, 0xf6, 0xf7, 0xf5, 0xf9, 0xc1, 0xa2, 0xe3, 0xbd, 
00030 /*  0     1     2     3     4     5     6     7     8     9     :     ;     <     =     >     ?      */
00031 /*            /     -                                                                  */
00032     0xa8, 0xe5, 0x2f, 0x2d, 0xc0, 0xb6, 0xd8, 0xd6, 0xa4, 0xb5, 0xab, 0xc7, 0xb2, 0xaa, 0xcc, 0xc6, 
00033 /*  @     A     B     C     D     E     F     G     H     I     J     K     L     M     N     O      */
00034 /*                                                                   ?                */
00035     0xf1, 0xc4, 0xda, 0xa9, 0xaf, 0xae, 0xe2, 0xac, 0xe7, 0xb3, 0xeb, 0xc9, 0xc8, 0x3f, 0xec, 0xcf, 
00036 /*  P     Q     R     S     T     U     V     W     X     Y     Z     [     \     ]     ^     _      */
00037 /*                                     "     )          (          _                     */
00038     0xad, 0xf0, 0xb1, 0xa6, 0xb8, 0xea, 0xce, 0x22, 0x29, 0xed, 0x28, 0xba, 0x5f, 0xc5, 0xd9, 0xf8, 
00039 /*  `     a     b     c     d     e     f     g     h     i     j     k     l     m     n     o      */
00040 /*                                                                                   */
00041     0xa3, 0xbf, 0xd4, 0xe1, 0xa1, 0xd3, 0xb4, 0xe0, 0xe9, 0xc3, 0xe8, 0xd2, 0xca, 0xb7, 0xd7, 0xb9, 
00042 /*  p     q     r     s     t     u     v     w     x     y     z     {     |     }     ~            */
00043 /*                                                              %     ,                 */
00044     0xc2, 0xe6, 0xbe, 0xcb, 0xd0, 0xd5, 0xcd, 0xe4, 0xbb, 0xd1, 0xbc, 0xb0, 0x25, 0x2c, 0xa5, 0x7f, 
00045 };
00046 #endif
00047 
00048 unsigned char _Thai_Enhanced_Kedmanee_Keymap[97] = {
00049 /*        !     "     #     $     %     &     '     (     )     *     +     ,     -     .     /      */
00050 /*             .                                                                       */
00051     0x20, 0xe5, 0x2e, 0xf2, 0xf3, 0xf4, 0xee, 0xa7, 0xf6, 0xf7, 0xf5, 0xf9, 0xc1, 0xa2, 0xe3, 0xbd, 
00052 /*  0     1     2     3     4     5     6     7     8     9     :     ;     <     =     >     ?      */
00053 /*            /     -                                                                  */
00054     0xa8, 0xdf, 0x2f, 0x2d, 0xc0, 0xb6, 0xd8, 0xd6, 0xa4, 0xb5, 0xab, 0xc7, 0xb2, 0xaa, 0xcc, 0xc6, 
00055 /*  @     A     B     C     D     E     F     G     H     I     J     K     L     M     N     O      */
00056 /*                                                                   ?                */
00057     0xf1, 0xc4, 0xda, 0xa9, 0xaf, 0xae, 0xe2, 0xac, 0xe7, 0xb3, 0xeb, 0xc9, 0xc8, 0x3f, 0xec, 0xcf, 
00058 /*  P     Q     R     S     T     U     V     W     X     Y     Z     [     \     ]     ^     _      */
00059 /*                                     "     )          (                               */
00060     0xad, 0xf0, 0xb1, 0xa6, 0xb8, 0xea, 0xce, 0x22, 0x29, 0xed, 0x28, 0xba, 0xa5, 0xc5, 0xd9, 0xf8, 
00061 /*  `     a     b     c     d     e     f     g     h     i     j     k     l     m     n     o      */
00062 /*                                                                                   */
00063     0xef, 0xbf, 0xd4, 0xe1, 0xa1, 0xd3, 0xb4, 0xe0, 0xe9, 0xc3, 0xe8, 0xd2, 0xca, 0xb7, 0xd7, 0xb9, 
00064 /*  p     q     r     s     t     u     v     w     x     y     z     {     |     }     ~            */
00065 /*                                                                   ,                 */
00066     0xc2, 0xe6, 0xbe, 0xcb, 0xd0, 0xd5, 0xcd, 0xe4, 0xbb, 0xd1, 0xbc, 0xb0, 0xa3, 0x2c, 0xfb, 0x7f, 
00067 };
00068 
00069 unsigned char _Thai_Kedmanee_Keymap[97] = {
00070 /*        !     "     #     $     %     &     '     (     )     *     +     ,     -     .     /      */
00071 /*        +     .                                                                       */
00072     0x20, 0x2b, 0x2e, 0xf2, 0xf3, 0xf4, 0xdf, 0xa7, 0xf6, 0xf7, 0xf5, 0xf9, 0xc1, 0xa2, 0xe3, 0xbd, 
00073 /*  0     1     2     3     4     5     6     7     8     9     :     ;     <     =     >     ?      */
00074 /*            /     -                                                                  */
00075     0xa8, 0xe5, 0x2f, 0x2d, 0xc0, 0xb6, 0xd8, 0xd6, 0xa4, 0xb5, 0xab, 0xc7, 0xb2, 0xaa, 0xcc, 0xc6, 
00076 /*  @     A     B     C     D     E     F     G     H     I     J     K     L     M     N     O      */
00077 /*                                                                   ?                */
00078     0xf1, 0xc4, 0xda, 0xa9, 0xaf, 0xae, 0xe2, 0xac, 0xe7, 0xb3, 0xeb, 0xc9, 0xc8, 0x3f, 0xec, 0xcf, 
00079 /*  P     Q     R     S     T     U     V     W     X     Y     Z     [     \     ]     ^     _      */
00080 /*                                     "     )          (                               */
00081     0xad, 0xf0, 0xb1, 0xa6, 0xb8, 0xea, 0xce, 0x22, 0x29, 0xed, 0x28, 0xba, 0xa3, 0xc5, 0xd9, 0xf8, 
00082 /*  `     a     b     c     d     e     f     g     h     i     j     k     l     m     n     o      */
00083 /*  _                                                                                 */
00084     0x5f, 0xbf, 0xd4, 0xe1, 0xa1, 0xd3, 0xb4, 0xe0, 0xe9, 0xc3, 0xe8, 0xd2, 0xca, 0xb7, 0xd7, 0xb9, 
00085 /*  p     q     r     s     t     u     v     w     x     y     z     {     |     }     ~            */
00086 /*                                                                   ,     %            */
00087     0xc2, 0xe6, 0xbe, 0xcb, 0xd0, 0xd5, 0xcd, 0xe4, 0xbb, 0xd1, 0xbc, 0xb0, 0xa5, 0x2c, 0x25, 0x7f, 
00088 };
00089 
00090 unsigned char _Thai_Pattachote_Keymap[97] = {
00091 /*        !     "     #     $     %     &     '     (     )     *     +     ,     -     .     /      */
00092 /*        +     .                                                                       */
00093     0x20, 0x2b, 0x2e, 0xf2, 0xf3, 0xf4, 0xdf, 0xa7, 0xf6, 0xf7, 0xf5, 0xf9, 0xc1, 0xa2, 0xe3, 0xbd, 
00094 /*  0     1     2     3     4     5     6     7     8     9     :     ;     <     =     >     ?      */
00095 /*            /     -                                                                  */
00096     0xa8, 0xe5, 0x2f, 0x2d, 0xc0, 0xb6, 0xd8, 0xd6, 0xa4, 0xb5, 0xab, 0xc7, 0xb2, 0xaa, 0xcc, 0xc6, 
00097 /*  @     A     B     C     D     E     F     G     H     I     J     K     L     M     N     O      */
00098 /*  "                                                                 ?                */
00099     0x22, 0xc4, 0xda, 0xa9, 0xaf, 0xae, 0xe2, 0xac, 0xe7, 0xb3, 0xeb, 0xc9, 0xc8, 0x3f, 0xec, 0xcf, 
00100 /*  P     Q     R     S     T     U     V     W     X     Y     Z     [     \     ]     ^     _      */
00101 /*                                     "     )          (          _                     */
00102     0xad, 0xf0, 0xb1, 0xa6, 0xb8, 0xea, 0xce, 0x22, 0x29, 0xed, 0x28, 0xba, 0x5f, 0xc5, 0xd9, 0xf8, 
00103 /*  `     a     b     c     d     e     f     g     h     i     j     k     l     m     n     o      */
00104 /*                                                                                   */
00105     0xa3, 0xbf, 0xd4, 0xe1, 0xa1, 0xd3, 0xb4, 0xe0, 0xe9, 0xc3, 0xe8, 0xd2, 0xca, 0xb7, 0xd7, 0xb9, 
00106 /*  p     q     r     s     t     u     v     w     x     y     z     {     |     }     ~            */
00107 /*                                                              %     ,                 */
00108     0xc2, 0xe6, 0xbe, 0xcb, 0xd0, 0xd5, 0xcd, 0xe4, 0xbb, 0xd1, 0xbc, 0xb0, 0x25, 0x2c, 0xa5, 0x7f, 
00109 };
00110 
00111 /*
00112 TThaiKeymap Thai_Standard_Keymaps[THAI_STANDARD_KEYMAP_NUM] = {
00113        { (unsigned char *)" (͡.-)",  _Thai_Enhanced_Kedmanee_Keymap},
00114        { (unsigned char *)"",                 _Thai_Kedmanee_Keymap         },
00115        { (unsigned char *)"ѵ⪵",               _Thai_Pattachote_Keymap        }
00116 };
00117 */
00118 
00119 TThaiKeymap Thai_Standard_Keymaps[THAI_STANDARD_KEYMAP_NUM] = {
00120        { (unsigned char *)"Enhanced Kedmanee(TIS820-2538)",     _Thai_Enhanced_Kedmanee_Keymap},
00121        { (unsigned char *)"Kedmanee(TIS820-2531)",     _Thai_Kedmanee_Keymap         },
00122        { (unsigned char *)"Pattachote",  _Thai_Pattachote_Keymap       }
00123 };
00124 
00125 char get_upper_key(char ch)
00126 {
00127        static char lower_keys[] = "`1234567890-=qwertyuiop[]\\asdfghjkl;\'zxcvbnm,./";
00128        static char upper_keys[] = "~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?";
00129 
00130        int i, len;
00131 
00132        ch = tolower(ch);
00133 
00134        len = strlen(lower_keys);
00135        for (i=0; i< len; i++) {
00136               if (ch == lower_keys[i]) {
00137                      return(upper_keys[i]);
00138               }
00139        }
00140 
00141        return(0);
00142 }
00143 
00144 char *skip_space(char *s)
00145 {
00146         while (*s && (*s == ' ' || *s == '\t')) s++;
00147         return s;
00148 }
00149 
00150 char *to_space(char *s)
00151 {
00152        while (*s && !(*s == ' ' || *s == '\t')) s++;
00153         return s;
00154 }
00155 
00156 char *trim_string(char *s)
00157 {
00158        char *head_ptr, *tail_ptr;
00159 
00160        if (!s || !*s) return(s);
00161 
00162        head_ptr = s;
00163 
00164        /* trim left space */
00165        while(*head_ptr && (*head_ptr == ' ' || *head_ptr == '\t' || *head_ptr == '\n'))
00166               head_ptr ++;
00167 
00168        if (!*head_ptr) return(head_ptr);
00169 
00170        /* trim right space */
00171        tail_ptr = head_ptr + strlen(head_ptr) - 1;
00172        
00173        while(*tail_ptr && (*tail_ptr == ' ' || *tail_ptr == '\t' || *tail_ptr == '\n')) {
00174               *tail_ptr = '\0';
00175               tail_ptr --;
00176        }
00177 
00178        return(head_ptr);
00179 }
00180 
00181 int parse_line_for_pair(char *line_buf, char **key_ptr, char **value_ptr)
00182 {
00183        char *ptr;
00184 
00185        *key_ptr = ptr = skip_space(line_buf);
00186 
00187        while(*ptr && !(*ptr==' ' || *ptr=='\t' || *ptr=='\n' || *ptr=='=' || *ptr==':')) ptr++;
00188 
00189        if (*ptr=='=' || *ptr==':') {
00190               *ptr = '\0';
00191               ptr = skip_space(ptr+1);
00192        } else if (*ptr==' ' || *ptr=='\t') {
00193               *ptr = '\0';
00194               ptr = skip_space(ptr+1);
00195               if (*ptr=='=' || *ptr==':') {
00196                      ptr = skip_space(ptr+1);
00197               }
00198        } else if (*ptr=='\n') {
00199               *ptr = '\0';
00200        }
00201 
00202        /* skip " */
00203        if (*ptr == '\"') {
00204               ptr++;
00205               if (*(ptr + strlen(ptr) - 1) == '\"')
00206                      *(ptr + strlen(ptr) - 1) = '\0';
00207        }
00208 
00209        *value_ptr = ptr;
00210        
00211        return(0);
00212 }
00213 
00214 int parse_line_for_tripple(char *line_buf, char **key_ptr, char **value1_ptr, char **value2_ptr)
00215 {
00216        char *ptr;
00217 
00218        *key_ptr = ptr = skip_space(line_buf);
00219 
00220        while(*ptr && !(*ptr==' ' || *ptr=='\t' || *ptr=='\n')) ptr++;
00221 
00222        if (*ptr==' ' || *ptr=='\t' || *ptr=='\n') {
00223               *ptr = '\0';
00224               ptr = skip_space(ptr+1);
00225        }
00226 
00227        *value1_ptr = ptr;
00228 
00229        while(*ptr && !(*ptr==' ' || *ptr=='\t' || *ptr=='\n')) ptr++;
00230 
00231        if (*ptr==' ' || *ptr=='\t' || *ptr=='\n') {
00232               *ptr = '\0';
00233               ptr = skip_space(ptr+1);
00234        }
00235 
00236        *value2_ptr = ptr;
00237 
00238        while(*ptr && !(*ptr==' ' || *ptr=='\t' || *ptr=='\n')) ptr++;
00239 
00240        if (*ptr==' ' || *ptr=='\t' || *ptr=='\n') {
00241               *ptr = '\0';
00242        }
00243 
00244        return(0);
00245 }
00246 
00247 int parse_line_for_keymap_name(TThaiKeymapList *pThaiKeymapList, char *line_buf)
00248 {
00249        char *key_ptr, *value_ptr;
00250        int ret, nKeymap_ID;
00251 
00252        parse_line_for_pair(line_buf, &key_ptr, &value_ptr);
00253        if (strcasecmp(key_ptr, KEYMAP_NAME_STRING)) {
00254               return(THAI_KEYMAP_ERROR);
00255        }
00256 
00257        nKeymap_ID = pThaiKeymapList->nNum_Keymaps;
00258        ret = ThaiKeymapList_Item_Prepare(pThaiKeymapList, nKeymap_ID);
00259        if (ret == THAI_KEYMAP_ERROR) 
00260               return(THAI_KEYMAP_ERROR);
00261 
00262        ret = ThaiKeymapList_Item_Set_Name(pThaiKeymapList, nKeymap_ID, (unsigned char *)value_ptr);
00263        if (ret == THAI_KEYMAP_ERROR)
00264               return(THAI_KEYMAP_ERROR);
00265 
00266        pThaiKeymapList->nNum_Keymaps ++;
00267        return(THAI_KEYMAP_OK);
00268 }
00269 
00270 int parse_line_for_keymap_section(TThaiKeymapList *pThaiKeymapList, char *line_buf)
00271 {
00272        char *key_ptr, *value1_ptr, *value2_ptr, ch;
00273        int nKeymap_ID;
00274 
00275        nKeymap_ID = pThaiKeymapList->nNum_Keymaps - 1;
00276        if (nKeymap_ID < 0)
00277               return(THAI_KEYMAP_ERROR);
00278 
00279        parse_line_for_tripple(line_buf, &key_ptr, &value1_ptr, &value2_ptr);
00280 
00281        ch = tolower(*key_ptr);
00282        ThaiKeymapList_Item_Set_KeymapValue(pThaiKeymapList, nKeymap_ID, ch, *value1_ptr);
00283 
00284        ch = get_upper_key(*key_ptr);
00285        ThaiKeymapList_Item_Set_KeymapValue(pThaiKeymapList, nKeymap_ID, ch, *value2_ptr);
00286 
00287        return(THAI_KEYMAP_OK);
00288 }
00289 
00290 int parse_line_for_section_flag(TThaiKeymapList *pThaiKeymapList, char *line_buf)
00291 {
00292        char *flag_section_str;
00293 
00294        int flag_section = THAI_DEFAULT_SECTION;
00295 
00296        /* Extract the name of section */
00297        line_buf[strlen(line_buf) - 1] = '\0';
00298        flag_section_str = trim_string(line_buf+1);
00299 
00300        if (!*flag_section_str) return(THAI_DEFAULT_SECTION);
00301 
00302        /* get flag_section */
00303        if (!(strncasecmp(flag_section_str, KEYMAP_STRING, strlen(KEYMAP_STRING)))) {
00304               flag_section = THAI_KEYMAP_SECTION;
00305               parse_line_for_keymap_name(pThaiKeymapList, flag_section_str + strlen(KEYMAP_STRING));
00306        }
00307 
00308        return(flag_section);
00309 }
00310 
00311 int Thai_Read_Config(char *file_name, TThaiKeymapList *pThaiKeymapList)
00312 {
00313        FILE *ifile;
00314        char line_buf[MAX_LINE_LEN], *line_ptr;
00315        int flag_section;
00316        int idx;
00317 
00318        for (idx = 0; idx < THAI_STANDARD_KEYMAP_NUM - 1; idx++) {
00319               ThaiKeymapList_Item_Add_Keymap(pThaiKeymapList,  Thai_Standard_Keymaps[idx].pName, Thai_Standard_Keymaps[idx].pKeymap);
00320        }
00321 
00322        ifile = fopen(file_name, "r");
00323        if (ifile == NULL) {
00324               fprintf(stderr, "Error: open file %s\n", file_name);
00325               return(THAI_KEYMAP_ERROR);
00326        }
00327 
00328        while(fgets(line_buf, MAX_LINE_LEN, ifile) != NULL) {
00329               line_ptr = trim_string(line_buf);
00330 
00331               if (*line_ptr && line_ptr[0] == '[' && line_ptr[strlen(line_ptr)-1] == ']') {
00332                      flag_section = parse_line_for_section_flag(pThaiKeymapList, line_ptr);
00333                      continue;
00334               }
00335 
00336               /* If space line or comment line */
00337               if (*line_ptr == '\0' || *line_ptr == '#') continue;
00338 
00339               switch (flag_section) {
00340                      case THAI_DEFAULT_SECTION:
00341                             break;
00342 
00343                      case THAI_KEYMAP_SECTION:
00344                             parse_line_for_keymap_section(pThaiKeymapList, line_ptr);
00345                             break;
00346               }
00347        }
00348 
00349        fclose(ifile);
00350 
00351        return(THAI_KEYMAP_OK);
00352 }
00353