Back to index

im-sdk  12.3.91
lookup_data.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <sys/stat.h>
00003 
00004 #ifdef HAVE_CONFIG_H
00005 #include "config.h"
00006 #endif
00007 
00008 #include "lookup_data.h"
00009 
00010 #include "imbean.h"
00011 
00012 #include <libxml/xmlmemory.h>
00013 #include <libxml/parser.h>
00014 
00015 int lookup_data_read_profile_to_memory(char **info_ptr);
00016 int lookup_data_parse_profile(lookup_data_t *lookup_data, char *xml_str, int size);
00017 
00018 /*********************************************************************/
00019 /*                       symbol_group utilities                      */
00020 /*********************************************************************/
00021 symbol_group_t *symbol_group_new()
00022 {
00023     symbol_group_t *symbol_group = NULL;
00024 
00025     symbol_group = (symbol_group_t *) calloc(1, sizeof(symbol_group_t));
00026 
00027     return symbol_group;
00028 }
00029 
00030 void symbol_group_destroy(symbol_group_t *symbol_group)
00031 {
00032     if (symbol_group == NULL)
00033         return;
00034 
00035     if (symbol_group->id)
00036         free ((char *)symbol_group->id);
00037 
00038     if (symbol_group->name)
00039         free ((char *)symbol_group->name);
00040 
00041     if (symbol_group->symbols != NULL) {
00042         int i;
00043         for (i = 0; i < symbol_group->num_symbols; i ++) {
00044             if (symbol_group->symbols[i])
00045                 free ((char *) symbol_group->symbols[i]);
00046         }
00047         free ((char *)symbol_group->symbols);
00048     }
00049 
00050     free ((char *)symbol_group);
00051 }
00052 
00053 void symbol_group_print(symbol_group_t *symbol_group)
00054 {
00055 #if DEBUG
00056     if (symbol_group == NULL)
00057         return;
00058 
00059     printf("Symbol Group: \n");
00060     if (symbol_group->id)
00061         printf("    id: %s\n", symbol_group->id);
00062     if (symbol_group->name)
00063         printf("    name: %s\n", symbol_group->name);
00064 
00065     if (symbol_group->symbols != NULL) {
00066         int i;
00067         printf("    symbols: ");
00068         for (i = 0; i < symbol_group->num_symbols; i ++) {
00069             if (symbol_group->symbols[i])
00070                 printf ("%s ", (char *) symbol_group->symbols[i]);
00071         }
00072         printf("\n");
00073     }
00074 #endif
00075 }
00076 
00077 #define SYMBOL_LIST_NUM_ALLOC  10
00078 int symbol_group_pushback_symbol(symbol_group_t *symbol_group,
00079                                  char *symbol)
00080 {
00081     int i, num_symbols;
00082 
00083     if (symbol_group == NULL || symbol == NULL)
00084         return LOOKUP_ERROR;
00085 
00086     if (symbol_group->symbols == NULL) {
00087         symbol_group->symbols = (char **) calloc (SYMBOL_LIST_NUM_ALLOC,
00088                                                   sizeof(char *));
00089         if (symbol_group->symbols == NULL)
00090             return LOOKUP_ERROR;
00091     }
00092 
00093     num_symbols = symbol_group->num_symbols;
00094     if ((num_symbols + 1) % SYMBOL_LIST_NUM_ALLOC == 0) {
00095         int num = num_symbols + 1 + SYMBOL_LIST_NUM_ALLOC;
00096 
00097         symbol_group->symbols = (char **)realloc(symbol_group->symbols,
00098                                                  num * sizeof(char *));
00099         if (symbol_group->symbols == NULL)
00100             return LOOKUP_ERROR;
00101 
00102         for (i = num_symbols; i < num; i++)
00103             symbol_group->symbols[i] = NULL;
00104     }
00105 
00106     symbol_group->symbols[num_symbols] = (char *)strdup(symbol);
00107     if (symbol_group->symbols[num_symbols] == NULL)
00108         return LOOKUP_ERROR;
00109 
00110     symbol_group->num_symbols ++;
00111 
00112     return LOOKUP_OK;
00113 }
00114 
00115 /*********************************************************************/
00116 /*                       lookup_data_t  utilities                    */
00117 /*********************************************************************/
00118 lookup_data_t *lookup_data_new()
00119 {
00120     lookup_data_t *lookup_data = NULL;
00121     char *xml_str = NULL;
00122     int  len_xml_str;
00123 
00124     lookup_data = (lookup_data_t *) calloc(1, sizeof(lookup_data_t));
00125     if (lookup_data == NULL)
00126         return NULL;
00127 
00128     len_xml_str = lookup_data_read_profile_to_memory(&xml_str);
00129     if (len_xml_str <= 0) {
00130         return lookup_data;
00131     }
00132 
00133     lookup_data_parse_profile(lookup_data, xml_str, len_xml_str);
00134 
00135     if (xml_str != NULL)
00136         free ((char *) xml_str);
00137 
00138     return lookup_data;
00139 }
00140 
00141 void lookup_data_destroy(lookup_data_t *lookup_data)
00142 {
00143     if (lookup_data == NULL)
00144         return;
00145 
00146     if (lookup_data->symbol_groups) {
00147         int i;
00148 
00149         for (i = 0; i < lookup_data->num_symbol_groups; i++) {
00150             if (lookup_data->symbol_groups[i])
00151                 symbol_group_destroy(lookup_data->symbol_groups[i]);
00152         }
00153         free ((char *)lookup_data->symbol_groups);
00154     }
00155 
00156     free ((char *)lookup_data);
00157 }
00158 
00159 void lookup_data_print(lookup_data_t *lookup_data)
00160 {
00161     if (lookup_data == NULL)
00162         return;
00163 
00164     if (lookup_data->symbol_groups) {
00165         int i;
00166 
00167         for (i = 0; i < lookup_data->num_symbol_groups; i++) {
00168             if (lookup_data->symbol_groups[i])
00169                 symbol_group_print(lookup_data->symbol_groups[i]);
00170         }
00171     }
00172 }
00173 
00174 #define SYMBOL_GROUP_LIST_NUM_ALLOC  10
00175 int lookup_data_pushback_symbol_group(lookup_data_t *lookup_data,
00176                                       symbol_group_t *symbol_group)
00177 {
00178     int i, num_symbol_groups;
00179 
00180     if (lookup_data == NULL || symbol_group == NULL)
00181         return LOOKUP_ERROR;
00182 
00183     if (lookup_data->symbol_groups == NULL) {
00184         lookup_data->symbol_groups = (symbol_group_t **)
00185                                           calloc (SYMBOL_GROUP_LIST_NUM_ALLOC,
00186                                                   sizeof(symbol_group_t *));
00187         if (lookup_data->symbol_groups == NULL)
00188             return LOOKUP_ERROR;
00189     }
00190 
00191     num_symbol_groups = lookup_data->num_symbol_groups;
00192     if ((num_symbol_groups + 1) % SYMBOL_GROUP_LIST_NUM_ALLOC == 0) {
00193         int num = num_symbol_groups + 1 + SYMBOL_GROUP_LIST_NUM_ALLOC;
00194 
00195         lookup_data->symbol_groups = (symbol_group_t **)realloc(lookup_data->symbol_groups,
00196                                                                 num * sizeof(symbol_group_t *));
00197         if (lookup_data->symbol_groups == NULL)
00198             return LOOKUP_ERROR;
00199 
00200         for (i = num_symbol_groups; i < num; i++)
00201             lookup_data->symbol_groups[i] = NULL;
00202     }
00203 
00204     lookup_data->symbol_groups[num_symbol_groups] = symbol_group;
00205     lookup_data->num_symbol_groups ++;
00206 
00207     return LOOKUP_OK;
00208 }
00209 
00210 /*********************************************************************/
00211 /*                       profile utilities                           */
00212 /*********************************************************************/
00213 #define LOOKUO_DATA_FILE_NAME "symbol_groups_tw.xml"
00214 int lookup_data_read_profile_to_memory(char **info_ptr)
00215 {
00216     char file_name[256];
00217 
00218     struct stat file_stat;
00219     int ret, file_size;
00220     FILE *fd;
00221 
00222     char *xml_str = NULL;
00223 
00224     *info_ptr = NULL;
00225 
00226     snprintf(file_name, 256, "%s/%s", LE_AUX_MODULES_DIR, LOOKUO_DATA_FILE_NAME);
00227     DEBUG_printf("file name :%s\n", file_name);
00228 
00229     ret = stat(file_name, &file_stat);
00230     if (ret == -1) {
00231         return 0;
00232     }
00233 
00234     file_size = file_stat.st_size;
00235     if (file_size == 0)
00236         return 0;
00237 
00238     xml_str = (char *) calloc(1, file_size + 1);
00239     fd = fopen(file_name, "r");
00240     if (!fd) {
00241         free ((char *) xml_str);
00242         return 0;
00243     }
00244 
00245     fread((char *) xml_str, file_size, 1, fd);
00246     xml_str[file_size] = 0;
00247 
00248     fclose(fd);
00249 
00250     *info_ptr = xml_str;
00251     return (file_size + 1);
00252 }
00253 
00254 static char utf8_skip_data[256] = {
00255   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00256   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00257   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00258   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00259   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00260   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00261   2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
00262   3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
00263 };
00264 
00265 void lookup_data_parse_symbol_group_item(symbol_group_t *symbol_group,
00266                                          IbmlProperty *ibml_property)
00267 {
00268     char *name, *value;
00269     char symbol[16], *ptr;
00270 
00271     name = (char *) ibml_property->name;
00272     if (!name || !*name)
00273         return;
00274 
00275     value = (char *) ibml_property->value;
00276     if (!value || !*value)
00277         return;
00278 
00279     if (!strcasecmp(name, "name")) {
00280         if (symbol_group->name)
00281             free ((char *)symbol_group->name);
00282         symbol_group->name = (char *) strdup(value);
00283     } else if (!strcasecmp(name, "symbols")) {
00284         ptr = value;
00285         while (ptr && *ptr) {
00286             int len;
00287 
00288             if (*ptr == ' ' ||
00289                 *ptr == '\t' ||
00290                 *ptr == '\n') {
00291                 ptr ++;
00292                 continue;
00293             }
00294 
00295             memset(symbol, 0, 16);
00296 
00297             /* get byte len of next utf8 character */
00298             len = utf8_skip_data[*(unsigned char *)ptr];
00299             strncpy(symbol, ptr, len);
00300 
00301             if (*symbol)
00302                 symbol_group_pushback_symbol(symbol_group, symbol);
00303 
00304             ptr = ptr + len;
00305         }
00306     }
00307 
00308     return;
00309 }
00310 
00311 void lookup_data_parse_symbol_groups_info(lookup_data_t *lookup_data,
00312                                           IbmlCategory *ibml_category)
00313 {
00314     symbol_group_t *symbol_group;
00315 
00316     IbmlElement *ibml_element;
00317     IbmlProperty *ibml_property;
00318 
00319     int ret, i, j;
00320 
00321     char *id, *scope;
00322 
00323     if (ibml_category == NULL)
00324         return;
00325     if (ibml_category->num_elements <= 0)
00326         return;
00327 
00328     for (i = 0; i < ibml_category->num_elements; i++) {
00329         ibml_element = ibml_category->elements[i];
00330         if (!ibml_element)
00331             continue;
00332         if (ibml_element->num_properties <= 0)
00333             continue;
00334 
00335         id = (char *) ibml_element->id;
00336         scope = (char *) ibml_element->scope;
00337         DEBUG_printf("id:%s, scope:%s\n", id ? id : "NULL",
00338                scope ? scope : "NULL");
00339         if (!id || !*id)
00340             continue;
00341 
00342         symbol_group = (symbol_group_t *) symbol_group_new();
00343         if (symbol_group == NULL)
00344             continue;
00345 
00346         symbol_group->id = (char *) strdup(id);
00347 
00348         for (j = 0; j < ibml_element->num_properties; j++) {
00349             ibml_property = ibml_element->properties[j];
00350             if (!ibml_property)
00351                 continue;
00352 
00353             lookup_data_parse_symbol_group_item(symbol_group, ibml_property);
00354         }
00355 
00356         if (symbol_group->id == NULL ||
00357             symbol_group->name == NULL) {
00358             symbol_group_destroy (symbol_group);
00359             continue;
00360         }
00361 
00362         ret = lookup_data_pushback_symbol_group(lookup_data, symbol_group);
00363         if (ret == LOOKUP_ERROR) {
00364             symbol_group_destroy (symbol_group);
00365             continue;
00366         }
00367     }
00368 
00369     return;
00370 }
00371 
00372 #define SYMBOL_GROUPS_CATAGORY "symbol_groups"
00373 int lookup_data_parse_profile(lookup_data_t *lookup_data, char *xml_str, int size)
00374 {
00375     IbmlData *ibml_data;
00376     IbmlCategory *ibml_category;
00377     int i;
00378 
00379     if (xml_str == NULL || size <= 0)
00380         return LOOKUP_ERROR;
00381 
00382     ibml_data = (IbmlData *) imbean_config_new_from_memory(xml_str, size);
00383     if (ibml_data == NULL)
00384         return LOOKUP_ERROR;
00385 
00386     for (i = 0; i < ibml_data->num_categories; i++) {
00387         ibml_category = ibml_data->categories[i];
00388 
00389         if (!ibml_category->scope || !*ibml_category->scope)
00390             continue;
00391 
00392         if (!strcasecmp(ibml_category->scope, SYMBOL_GROUPS_CATAGORY)) {
00393             lookup_data_parse_symbol_groups_info(lookup_data, ibml_category);
00394         }
00395     }
00396 
00397     ibml_data_free(ibml_data);
00398     return LOOKUP_OK;
00399 }