Back to index

im-sdk  12.3.91
OptionConfig.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <locale.h>
00003 #include <sys/types.h>
00004 #include <sys/stat.h>
00005 
00006 #include "hzinput.h"
00007 #include "OptionConfig.h"
00008 #include "kolelog.h"
00009 
00010 #define MAX_LINE_LEN 256
00011 
00012 int           gIMEOptionsNum;
00013 IMEOption_Item       *gIMEOptions[MAX_ENGINE_NUM];
00014               
00015 IMEOption_Item *OptionItem_Malloc()
00016 {
00017        IMEOption_Item *pOptionItem;
00018 
00019        pOptionItem = (IMEOption_Item *)calloc(1, sizeof(IMEOption_Item));
00020        if (pOptionItem == NULL) return(NULL);
00021 
00022        pOptionItem->ime_ename = NULL;
00023        pOptionItem->ime_cname = NULL;
00024        pOptionItem->args_list = NULL;
00025 
00026        return(pOptionItem);
00027 }
00028 
00029 void OptionItem_Free(IMEOption_Item *pOptionItem)
00030 {
00031        int i;
00032        char *arg_name;
00033 
00034        if (pOptionItem == NULL) return;
00035 
00036        if (pOptionItem->ime_ename) 
00037               free((char *)pOptionItem->ime_ename);
00038        if (pOptionItem->ime_cname)
00039               free((char *)pOptionItem->ime_cname);
00040 
00041        if (pOptionItem->args_list) {
00042               for (i=0; i<MAX_ARGS_NUM; i++) {
00043                      arg_name = pOptionItem->args_list->args[i].name;
00044                      if (arg_name)
00045                             free((char *)arg_name);
00046               }
00047               free((char *)pOptionItem->args_list);
00048        }
00049 
00050        free((char *)pOptionItem);
00051 }
00052 
00053 void OptionsList_Init()
00054 {
00055        int i;
00056        
00057        gIMEOptionsNum = 0;
00058        for (i=0; i<MAX_ENGINE_NUM; i++) 
00059               gIMEOptions[i] = NULL;
00060 }
00061 
00062 void OptionsList_Free()
00063 {
00064        int i;
00065 
00066        for (i=0; i<MAX_ENGINE_NUM; i++) {
00067               OptionItem_Free(gIMEOptions[i]);
00068               gIMEOptions[i] = NULL;
00069        }
00070 
00071        gIMEOptionsNum = 0;
00072 }
00073 
00074 void get_option_line(FILE *fd, char *line)
00075 {
00076        int line_ptr;
00077        char line_buf[256], *ptr;
00078 
00079        line_ptr = 0;
00080        line[0] = '\0';
00081 
00082        /* get line with no space */
00083        while(fgets(line_buf, 255, fd) != NULL) {
00084               ptr = line_buf;
00085 
00086               /* skip space keys */
00087               while(*ptr && isspace(*ptr)) ptr++;
00088 
00089               /* if is space line, get new line */
00090               if (*ptr == '\n' || *ptr == '\0')
00091                      continue;
00092 
00093               while(*ptr != '\n' && *ptr != '\0' && line_ptr < MAX_LINE_LEN) 
00094                      line[line_ptr++] = *ptr++;
00095 
00096               /* trim right space */
00097               while (isspace(line[line_ptr-1])) line_ptr--;
00098               line[line_ptr] = '\0';
00099 
00100               /* if the line end with '\', then continue read the next line */
00101               if (line[line_ptr-1] == '\\') {
00102                      line_ptr--;
00103                      line[line_ptr] = '\0';
00104                      continue;
00105               }
00106 
00107               break;
00108        }
00109 }
00110 
00111 void Get_OptionsList_From_Profile()
00112 {
00113        char            file_name[256], line[MAX_LINE_LEN]; 
00114        int           i;
00115        IMEOption_Item       *pOptionItem;
00116     
00117        register char        *ptr, *ime_ename, *pOption_Value;
00118        FILE          *fd;
00119 
00120        char          *locale_name;
00121 
00122        locale_name = setlocale(LC_CTYPE, NULL);
00123 
00124        /* read profile from file to memory buffer  */ 
00125        sprintf(file_name, "%s/%s/%s/%s", getenv("HOME"), 
00126               USER_PROFILE_PATH, locale_name, USER_OPTION_FILE_NAME);
00127        KOLE_LOG (LOGDEST_STDOUT, "file name :%s\n",file_name);
00128 
00129        fd = fopen(file_name, "r");
00130        if (! fd) {
00131               return;
00132        }
00133 
00134        gIMEOptionsNum = 0;
00135        do {
00136               get_option_line(fd, line);
00137 
00138               if (line[0] == '\0') break;
00139               if (line[0] == '#') continue;
00140 
00141               /* get IME language engine name */
00142               ime_ename = line;
00143               ptr = line;
00144               while (*ptr && !isspace(*ptr)) ptr++;
00145               if (*ptr) {
00146                      *ptr = '\0';
00147                      ptr++;
00148               }
00149 
00150               /* skip space */
00151               while (*ptr && isspace(*ptr)) ptr++;
00152 
00153               /* get option value */
00154               pOption_Value = ptr;
00155               while (*ptr && !isspace(*ptr)) ptr++;
00156               if (*ptr) {
00157                      *ptr = '\0';
00158               }
00159 
00160               pOptionItem = (IMEOption_Item *)OptionItem_Malloc();
00161               if (pOptionItem == NULL) continue;
00162 
00163               pOptionItem->ime_ename = (char *)strdup(ime_ename);
00164               if (pOptionItem->ime_ename == NULL) {
00165                      OptionItem_Free(pOptionItem);
00166                      break;
00167               }
00168        
00169               pOptionItem->ime_cname = NULL;
00170               pOptionItem->opt_value = atol(pOption_Value);
00171               pOptionItem->args_list = NULL;
00172               gIMEOptions[gIMEOptionsNum] = pOptionItem;
00173               gIMEOptionsNum ++;
00174        } while (1);
00175 
00176        fclose(fd);
00177 
00178        for (i=0; i<gIMEOptionsNum; i++) {
00179               KOLE_LOG (LOGDEST_STDOUT, "IME Name: %s,  Option: %d\n", 
00180                      gIMEOptions[i]->ime_ename,
00181                      gIMEOptions[i]->opt_value);
00182        }
00183        return;
00184 }
00185 
00186 void Save_OptionsList_To_Profile()
00187 {
00188        FILE *fd;
00189        int ret, i;
00190        struct stat file_stat;
00191        char path_name[256], tmp_name[256];
00192        char *locale_name;
00193 
00194        /* Create Path $(HOME)/.Xlocale */
00195        sprintf(path_name, "%s/%s", (char *)getenv("HOME"), USER_PROFILE_PATH);
00196        KOLE_LOG (LOGDEST_STDOUT, "path_name: %s\n", path_name);
00197 
00198        ret = stat(path_name, &file_stat);
00199        if (ret == -1) {
00200               KOLE_LOG (LOGDEST_STDOUT, "%s not exist\n", path_name);
00201               ret = mkdir(path_name, 0777);
00202               if (ret == -1) {
00203                      KOLE_LOG (LOGDEST_STDOUT, "can not create path %s\n", path_name);
00204                      return;
00205               }
00206        }
00207 
00208        /* Create Path $(HOME)/.Xlocale/<locale> */
00209        locale_name = setlocale(LC_CTYPE, NULL);
00210 
00211        sprintf(tmp_name, "%s/%s", path_name, locale_name);
00212        strcpy(path_name, tmp_name);
00213        KOLE_LOG (LOGDEST_STDOUT, "path_name: %s\n", path_name);
00214 
00215        ret = stat(path_name, &file_stat);
00216        if (ret == -1) {
00217               KOLE_LOG (LOGDEST_STDOUT, "%s not exist\n", path_name);
00218               ret = mkdir(path_name, 0777);
00219               if (ret == -1) {
00220                      KOLE_LOG (LOGDEST_STDOUT, "can not create path %s\n", path_name);
00221                      return;
00222               }
00223        }
00224 
00225        /* Create File $(HOME)/.Xlocale/<locale>/option.cfg */
00226        sprintf(tmp_name, "%s/%s", path_name, USER_OPTION_FILE_NAME);
00227        strcpy(path_name, tmp_name);
00228        KOLE_LOG (LOGDEST_STDOUT, "path_name: %s\n", path_name);
00229        fd = fopen(path_name, "wb");
00230        if (! fd) {
00231               KOLE_LOG (LOGDEST_STDOUT, "unable open the file: %s\n", path_name);
00232               return;
00233        }
00234 
00235        for (i=0; i<gIMEOptionsNum; i++) {
00236               fprintf(fd, "%s\t%d\n", gIMEOptions[i]->ime_ename, gIMEOptions[i]->opt_value);
00237        }
00238 
00239        fclose(fd);
00240 }
00241 
00242 void Add_OptionItem_To_List(IMEOption_Item *pOptionItem)
00243 {
00244        int  i;
00245        char *name;
00246        IMEOption_Item *pNewOptionItem;
00247 
00248        if (pOptionItem == NULL) return;
00249 
00250        pNewOptionItem = (IMEOption_Item *)OptionItem_Malloc();
00251        if (pNewOptionItem == NULL) return;
00252 
00253        name = (char *)pOptionItem->ime_ename;
00254        if (name && *name) {
00255               pNewOptionItem->ime_ename = (char *)strdup(name);
00256               if (pNewOptionItem->ime_ename == NULL) {
00257                      OptionItem_Free(pNewOptionItem);
00258                      return;
00259               }
00260        }
00261 
00262        name = (char *)pOptionItem->ime_cname;
00263        if (name && *name) {
00264               pNewOptionItem->ime_cname = (char *)strdup(name);
00265               if (pNewOptionItem->ime_cname == NULL) {
00266                      OptionItem_Free(pNewOptionItem);
00267                      return;
00268               }
00269        }
00270 
00271        pNewOptionItem->opt_value = pOptionItem->opt_value;
00272 
00273        pNewOptionItem->args_list = (IMEArgListRec *)calloc(1, sizeof(IMEArgListRec));
00274        if (pNewOptionItem->args_list == NULL) {
00275               OptionItem_Free(pNewOptionItem);
00276               return;
00277        }
00278 
00279        pNewOptionItem->args_list->args_num = pOptionItem->args_list->args_num;
00280        for (i=0; i<MAX_ARGS_NUM; i++) {
00281               int arg_type, arg_value;
00282               char *arg_name;
00283 
00284               pNewOptionItem->args_list->args[i].name = NULL;
00285               arg_name = (char *)pOptionItem->args_list->args[i].name;
00286               if (arg_name && *arg_name)
00287                      pNewOptionItem->args_list->args[i].name = (char *)strdup(arg_name);
00288               
00289               pNewOptionItem->args_list->args[i].type = pOptionItem->args_list->args[i].type;
00290               pNewOptionItem->args_list->args[i].value = pOptionItem->args_list->args[i].value;
00291        }
00292 
00293        if (gIMEOptions[gIMEOptionsNum]) OptionItem_Free(gIMEOptions[gIMEOptionsNum]);
00294 
00295        gIMEOptions[gIMEOptionsNum] = pNewOptionItem;
00296        gIMEOptionsNum ++;
00297 }
00298 
00299 
00300 void Set_OptionValue_From_ArgList(IMEOption_Item *pOptionItem)
00301 {
00302        int  i, j, opt_value;
00303        int cur_bit_pos, cur_bit_len, value, mask;
00304 
00305        if (pOptionItem->args_list == NULL) return;
00306 
00307        opt_value = 0;
00308        cur_bit_pos = 0;
00309 
00310        for (i=0; i<pOptionItem->args_list->args_num; i++) {
00311               value = pOptionItem->args_list->args[i].value;
00312 
00313               cur_bit_len = pOptionItem->args_list->args[i].type;
00314               if (cur_bit_len <= 0) {
00315                      cur_bit_len = 1;
00316                      value = (value ? 1: 0);
00317               }
00318 
00319               mask = 0;
00320               for (j=0; j<cur_bit_len; j++) 
00321                      mask |= (1 << j);
00322 
00323               opt_value |= ((value & mask) << cur_bit_pos);
00324 
00325               cur_bit_pos += cur_bit_len;
00326        }
00327 
00328        pOptionItem->opt_value = opt_value;
00329 }
00330 
00331 void Set_ArgList_From_OptionValue(IMEOption_Item *pOptionItem)
00332 {
00333        int  i, args_num, opt_value;
00334        int  cur_bit_pos, cur_bit_len, mask, j;
00335 
00336        if (pOptionItem->args_list == NULL) return;
00337 
00338        args_num = pOptionItem->args_list->args_num;
00339        opt_value = pOptionItem->opt_value;
00340 
00341        cur_bit_pos = 0;
00342        for (i=0; i<args_num; i++) {
00343               if (pOptionItem->args_list->args[i].name == NULL)
00344                      continue;
00345 
00346               cur_bit_len = pOptionItem->args_list->args[i].type;
00347               if (cur_bit_len <= 0) {
00348                      cur_bit_len = 1;
00349               }
00350 
00351               mask = 0;
00352               for (j=0; j<cur_bit_len; j++)
00353                      mask |= (1<<j);
00354 
00355               pOptionItem->args_list->args[i].value = (opt_value >> cur_bit_pos) & mask;
00356 
00357               cur_bit_pos += cur_bit_len;
00358        }
00359 }
00360 
00361