Back to index

im-sdk  12.3.91
zhuyin_config.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 #include <stdio.h>
00044 #include "zhuyin_data.h"
00045 
00046 #ifdef HAVE_CONFIG_H
00047 #include "config.h"
00048 #endif
00049 
00050 #define MAX_LINE_LEN    256
00051 
00052 #define BASE_DIR_STRING         "base_dir"
00053 #define DATA_FILE_STRING        "data_file"
00054 #define KEYMAP_STRING           "keymap"
00055 
00056 #define BASE_DIR_NAME_STRING    "path"
00057 #define DATA_FILE_NAME_STRING   "file"
00058 #define KEYMAP_NAME_STRING      "name"
00059 
00060 enum {
00061        ZHUYIN_DEFAULT_SECTION = 0,
00062        ZHUYIN_BASE_DIR_SECTION,
00063        ZHUYIN_DATA_FILE_SECTION,
00064        ZHUYIN_KEYMAP_SECTION,
00065 };
00066 
00067 /**************************************************************
00068 The format of Zhuyin configure file as follow:
00069 
00070 [base_dir]
00071 path=/usr/lib/iiim/le/twle/input_methods
00072 
00073 [data_file]
00074 file=data/zhuyin.data
00075 
00076 [keymap name="ZhuyinKeymapName"]
00077 ZhuyinSymbol  ZhuyinKey
00078 
00079 **************************************************************/
00080 
00081 int parse_line_for_keymap_name(TZhuyinData *pZhuyinData, char *line_buf);
00082 
00083 char *skip_space(char *s)
00084 {
00085         while (*s && (*s == ' ' || *s == '\t')) s++;
00086         return s;
00087 }
00088 
00089 char *to_space(char *s)
00090 {
00091        while (*s && !(*s == ' ' || *s == '\t')) s++;
00092         return s;
00093 }
00094 
00095 char *trim_string(char *s)
00096 {
00097        char *head_ptr, *tail_ptr;
00098 
00099        if (!s || !*s) return(s);
00100 
00101        head_ptr = s;
00102 
00103        /* trim left space */
00104        while(*head_ptr && (*head_ptr == ' ' || *head_ptr == '\t' || *head_ptr == '\n'))
00105               head_ptr ++;
00106 
00107        if (!*head_ptr) return(head_ptr);
00108 
00109        /* trim right space */
00110        tail_ptr = head_ptr + strlen(head_ptr) - 1;
00111        
00112        while(*tail_ptr && (*tail_ptr == ' ' || *tail_ptr == '\t' || *tail_ptr == '\n')) {
00113               *tail_ptr = '\0';
00114               tail_ptr --;
00115        }
00116 
00117        return(head_ptr);
00118 }
00119 
00120 int parse_line_for_pair(char *line_buf, char **key_ptr, char **value_ptr)
00121 {
00122        char *ptr, *kptr, *vptr;
00123 
00124        *key_ptr = ptr = skip_space(line_buf);
00125 
00126        while(*ptr && !(*ptr==' ' || *ptr=='\t' || *ptr=='\n' || *ptr=='=' || *ptr==':')) ptr++;
00127 
00128        if (*ptr=='=' || *ptr==':') {
00129               *ptr = '\0';
00130               ptr = skip_space(ptr+1);
00131        } else if (*ptr==' ' || *ptr=='\t') {
00132               *ptr = '\0';
00133               ptr = skip_space(ptr+1);
00134               if (*ptr=='=' || *ptr==':') {
00135                      ptr = skip_space(ptr+1);
00136               }
00137        } else if (*ptr=='\n') {
00138               *ptr = '\0';
00139        }
00140 
00141        /* skip " */
00142        if (*ptr == '\"') ptr++;
00143        *value_ptr = ptr;
00144        
00145        ptr = to_space(ptr);
00146        if (*(ptr-1) == '\"') ptr--;
00147        *ptr = '\0';
00148 
00149        return(0);
00150 }
00151 
00152 int parse_line_for_section_flag(TZhuyinData *pZhuyinData, char *line_buf)
00153 {
00154        char *flag_section_str;
00155 
00156        int flag_section = ZHUYIN_DEFAULT_SECTION;
00157 
00158        /* Extract the name of section */
00159        line_buf[strlen(line_buf) - 1] = '\0';
00160        flag_section_str = trim_string(line_buf+1);
00161 
00162        if (!*flag_section_str) return(ZHUYIN_DEFAULT_SECTION);
00163 
00164        /* get flag_section */
00165        if (!(strncasecmp(flag_section_str, BASE_DIR_STRING, strlen(BASE_DIR_STRING)))) {
00166               flag_section = ZHUYIN_BASE_DIR_SECTION;
00167        } else if (!(strncasecmp(flag_section_str, DATA_FILE_STRING, strlen(DATA_FILE_STRING)))) {
00168               flag_section = ZHUYIN_DATA_FILE_SECTION;
00169        } else if (!(strncasecmp(flag_section_str, KEYMAP_STRING, strlen(KEYMAP_STRING)))) {
00170               flag_section = ZHUYIN_KEYMAP_SECTION;
00171               parse_line_for_keymap_name(pZhuyinData, flag_section_str + strlen(KEYMAP_STRING));
00172        }
00173 
00174        return(flag_section);
00175 }
00176 
00177 int parse_line_for_basedir_section(TZhuyinData *pZhuyinData, char *line_buf, char **pBaseDir)
00178 {
00179        char *key_ptr, *value_ptr;
00180 
00181        parse_line_for_pair(line_buf, &key_ptr, &value_ptr);
00182 
00183        if (!strcasecmp(key_ptr, BASE_DIR_NAME_STRING)) {
00184               if (*pBaseDir != NULL) free((char *)*pBaseDir);
00185               *pBaseDir = (char *)strdup(value_ptr);
00186        }
00187        return(ZHUYIN_OK);
00188 }
00189 
00190 int parse_line_for_datafile_section(TZhuyinData *pZhuyinData, char *line_buf, char **pDataFile)
00191 {
00192        char *key_ptr, *value_ptr;
00193 
00194        parse_line_for_pair(line_buf, &key_ptr, &value_ptr);
00195 
00196        if (!strcasecmp(key_ptr, DATA_FILE_NAME_STRING)) {
00197               if (*pDataFile != NULL) free((char *)*pDataFile);
00198               *pDataFile = (char *)strdup(value_ptr);
00199        }
00200        return(ZHUYIN_OK);
00201 }
00202 
00203 int parse_line_for_keymap_section(TZhuyinData *pZhuyinData, char *line_buf)
00204 {
00205        char *key_ptr, *value_ptr;
00206        int nKeymap_ID;
00207 
00208        parse_line_for_pair(line_buf, &key_ptr, &value_ptr);
00209 
00210        nKeymap_ID = pZhuyinData->pZhuyinKeymapList->nNum_Keymaps - 1;
00211        ZhuyinKeymapList_Item_Set_KeymapValue(pZhuyinData->pZhuyinKeymapList, nKeymap_ID, key_ptr, *value_ptr);
00212 
00213        return(ZHUYIN_OK);
00214 }
00215 
00216 int parse_line_for_keymap_name(TZhuyinData *pZhuyinData, char *line_buf)
00217 {
00218        char *key_ptr, *value_ptr;
00219        int nKeymap_ID;
00220 
00221        if (pZhuyinData->pZhuyinKeymapList->nNum_Keymaps == 0) {
00222               pZhuyinData->pZhuyinKeymapList->nNum_Keymaps++;
00223               ZhuyinKeymapList_Item_Prepare(pZhuyinData->pZhuyinKeymapList, 0);
00224               ZhuyinKeymapList_Item_Add_StandardKeymap(pZhuyinData->pZhuyinKeymapList, 0);
00225        }
00226 
00227        nKeymap_ID = pZhuyinData->pZhuyinKeymapList->nNum_Keymaps - 1;
00228        if (pZhuyinData->pZhuyinKeymapList->pKeymaps[nKeymap_ID]->pName != NULL)
00229               pZhuyinData->pZhuyinKeymapList->nNum_Keymaps ++;
00230 
00231        nKeymap_ID = pZhuyinData->pZhuyinKeymapList->nNum_Keymaps - 1;
00232        ZhuyinKeymapList_Item_Prepare(pZhuyinData->pZhuyinKeymapList, nKeymap_ID);
00233 
00234        parse_line_for_pair(line_buf, &key_ptr, &value_ptr);
00235        if (!strcasecmp(key_ptr, KEYMAP_NAME_STRING)) {
00236               ZhuyinKeymapList_Item_Set_Name(pZhuyinData->pZhuyinKeymapList, nKeymap_ID, value_ptr);
00237        }
00238 }
00239 
00240 ZhuyinData_Read_Config(char *file_name, TZhuyinData *pZhuyinData)
00241 {
00242        FILE *ifile;
00243        char line_buf[MAX_LINE_LEN], *line_ptr;
00244        int flag_section;
00245        char *pBaseDir = NULL;
00246        char *pDataFile = NULL;
00247        char *data_base_dir, *data_file_name;
00248        char data_file_path[MAX_LINE_LEN];
00249 
00250        ifile = fopen(file_name, "r");
00251        if (ifile == NULL) {
00252               fprintf(stderr, "Error: open file %s\n", file_name);
00253               return(ZHUYIN_ERROR);
00254        }
00255 
00256        while(fgets(line_buf, MAX_LINE_LEN, ifile) != NULL) {
00257               line_ptr = trim_string(line_buf);
00258 
00259               if (*line_ptr && line_ptr[0] == '[' && line_ptr[strlen(line_ptr)-1] == ']') {
00260                      flag_section = parse_line_for_section_flag(pZhuyinData, line_ptr);
00261                      continue;
00262               }
00263 
00264               /* If space line or comment line */
00265               if (*line_ptr == '\0' || *line_ptr == '#') continue;
00266 
00267               switch (flag_section) {
00268                      case ZHUYIN_DEFAULT_SECTION:
00269                             break;
00270 
00271                      case ZHUYIN_BASE_DIR_SECTION:
00272                             parse_line_for_basedir_section(pZhuyinData, line_ptr, &pBaseDir);
00273                             break;
00274 
00275                      case ZHUYIN_DATA_FILE_SECTION:
00276                             parse_line_for_datafile_section(pZhuyinData, line_ptr, &pDataFile);
00277                             break;
00278 
00279                      case ZHUYIN_KEYMAP_SECTION:
00280                             parse_line_for_keymap_section(pZhuyinData, line_ptr);
00281                             break;
00282               }
00283        }
00284 
00285        pZhuyinData->data_file = NULL;
00286 
00287        if (pBaseDir)
00288               data_base_dir = pBaseDir;
00289        else
00290               data_base_dir = LE_IME_MODULES_DIR;
00291 
00292        if (pDataFile)
00293               data_file_name = pDataFile;
00294        else
00295               data_file_name = "data/zhuyin.data";
00296 
00297        snprintf(data_file_path, MAX_LINE_LEN, "%s/%s", data_base_dir, data_file_name);
00298        pZhuyinData->data_file = (char *)strdup(data_file_path);
00299 
00300        if (pBaseDir) free((char *)pBaseDir);
00301        if (pDataFile) free((char *)pDataFile);
00302 
00303        fclose(ifile);
00304 
00305        return(ZHUYIN_OK);
00306 }