Back to index

im-sdk  12.3.91
le_info.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include "le_info.h"
00004 
00005 #define LE_CONFIG_FILE            "thai_keymap.cfg"
00006 
00007 LeResult le_info_destroy(LeInfoRec * le_info);
00008 LeResult le_info_load_config_file(LeInfoRec * le_info,
00009                               char *le_config_file);
00010 
00011 LeInfoRec *le_info_new()
00012 {
00013     LeResult result;
00014 
00015     LeInfoRec *le_info = NULL;
00016     char *le_config_file = LE_CONFIG_FILE;
00017 
00018     le_info = (LeInfoRec *) calloc(1, sizeof(LeInfoRec));
00019     if (le_info == NULL)
00020        return (NULL);
00021 
00022     le_info->keymap_list = NULL;
00023 
00024     result = le_info_load_config_file(le_info, le_config_file);
00025     if (result == LE_FAIL) {
00026         le_info_destroy(le_info);
00027         return (NULL);
00028     }
00029 
00030     return (le_info);
00031 }
00032 
00033 LeResult le_info_destroy(LeInfoRec * le_info)
00034 {
00035     int i;
00036 
00037     if (le_info == NULL)
00038        return (LE_FAIL);
00039 
00040     /* free keymapping list */
00041     if (le_info->keymap_list) {
00042        ThaiKeymapList_Free(le_info->keymap_list);
00043        free ((char *)le_info->keymap_list);
00044     }
00045 
00046     free((char *) le_info);
00047 
00048     return (LE_OK);
00049 }
00050 
00051 LeResult le_info_print(LeInfoRec * le_info)
00052 {
00053 #ifdef DEBUG
00054     if (le_info == NULL)
00055        return (LE_FAIL);
00056 
00057     /* print ime modules */
00058     if (le_info->keymap_list) {
00059        ThaiKeymapList_Print(le_info->keymap_list);
00060     }
00061 
00062     return (LE_OK);
00063 #endif
00064 }
00065 
00066 char *le_info_get_full_file_path(char *file_path)
00067 {
00068     int len;
00069     char *full_file_path = NULL;
00070 
00071     if (!file_path || !*file_path)
00072        return (NULL);
00073 
00074     if (file_path[0] == '/') {
00075        len = strlen(file_path);
00076 
00077        full_file_path = (char *) calloc(len + 1, sizeof(char));
00078        if (full_file_path == NULL)
00079            return (NULL);
00080 
00081        sprintf(full_file_path, "%s", file_path);
00082     } else {
00083        len = strlen(LE_BASE_DIR) + 1 + strlen(file_path);
00084 
00085        full_file_path = (char *) calloc(len + 1, sizeof(char));
00086        if (full_file_path == NULL)
00087            return (NULL);
00088 
00089        sprintf(full_file_path, "%s/%s", LE_BASE_DIR, file_path);
00090     }
00091 
00092     return (full_file_path);
00093 }
00094 
00095 LeResult le_info_load_config_file(LeInfoRec * le_info,
00096                               char *le_config_file)
00097 {
00098     char *le_config_file_with_full_path = NULL;
00099 
00100     if (le_info == NULL)
00101        return (LE_FAIL);
00102 
00103     le_config_file_with_full_path =
00104        (char *) le_info_get_full_file_path(le_config_file);
00105     if (!le_config_file_with_full_path || !*le_config_file_with_full_path)
00106        return (LE_FAIL);
00107 
00108     le_info->keymap_list = (TThaiKeymapList *) calloc(1, sizeof(TThaiKeymapList));
00109     if (le_info->keymap_list == NULL) {
00110         free((char *) le_config_file_with_full_path);
00111        return (LE_FAIL);
00112     }
00113 
00114     /* Get all Thai Keyboard mappings */
00115     ThaiKeymapList_Init(le_info->keymap_list);
00116     Thai_Read_Config(le_config_file_with_full_path, le_info->keymap_list);
00117 
00118     free((char *) le_config_file_with_full_path);
00119 
00120     return (LE_OK);
00121 }