ui_elem* ui_elem_new_type_id(char* name, int type_id) { if ( dictionary_contains(ui_elems, name) ) { error("UI Manager already contains element called %s!", name); } debug("Creating UI Element %s (%s)", name, type_id_name(type_id)); ui_elem* ui_e = NULL; for(int i = 0; i < num_ui_elem_handlers; i++) { ui_elem_handler ui_hand = ui_elem_handlers[i]; if (ui_hand.type_id == type_id) { ui_e = ui_hand.new_func(); } } if (ui_e == NULL) { error("Don't know how to create ui element %s. No handler for type %s!", name, type_id_name(type_id)); } dictionary_set(ui_elems, name, ui_e); int* type_ptr = malloc(sizeof(int)); *type_ptr = type_id; dictionary_set(ui_elem_types, name, type_ptr); char* name_copy = malloc(strlen(name) + 1); strcpy(name_copy, name); list_push_back(ui_elem_names, name_copy); return ui_e; }
/* Tool function to create and populate a generic non-empty dictionary */ static dictionary * generate_dictionary(unsigned sections, unsigned entries_per_section) { unsigned i, j ; dictionary * dic; char sec_name[32]; char key_name[64]; char key_value[32]; dic = dictionary_new(sections + sections * entries_per_section); if (dic == NULL) return NULL; /* Insert the sections */ for (i = 0; i < sections; ++i) { sprintf(sec_name, "sec%d", i); dictionary_set(dic, sec_name, ""); for (j = 0; j < entries_per_section; ++j) { /* Populate the section with the entries */ sprintf(key_name, "%s:key%d", sec_name, j); sprintf(key_value, "value-%d/%d", i, j); dictionary_set(dic, key_name, key_value); } } return dic; }
void Test_dictionary_growing(CuTest *tc) { int i, j; char sec_name[32]; char key_name[64]; dictionary *dic; dic = dictionary_new(DICTMINSZ); CuAssertPtrNotNull(tc, dic); CuAssertIntEquals(tc, 0, dic->n); /* Makes the dictionary grow */ for (i = 1 ; i < 101; ++i) { sprintf(sec_name, "sec%d", i); CuAssertIntEquals(tc, 0, dictionary_set(dic, sec_name, "")); for (j = 1 ; j < 11; ++j) { sprintf(key_name, "%s:key%d", sec_name, j); CuAssertIntEquals(tc, 0, dictionary_set(dic, key_name, "dummy_value")); CuAssertIntEquals(tc, i + (i - 1) * 10 + j, dic->n); } } /* Shrink the dictionary */ for (i = 100 ; i > 0; --i) { sprintf(sec_name, "sec%d", i); for (j = 10 ; j > 0; --j) { sprintf(key_name, "%s:key%d", sec_name, j); dictionary_unset(dic, key_name); } dictionary_unset(dic, sec_name); CuAssertIntEquals(tc, (i - 1) * (11), dic->n); } dictionary_del(dic); }
void Test_dictionary_unset(CuTest *tc) { int i, j; char sec_name[32]; char key_name[64]; dictionary *dic1; dictionary *dic2; char *dic1_dump; char *dic2_dump; /* try dummy unsets */ dictionary_unset(NULL, NULL); dictionary_unset(NULL, key_name); /* Generate two similar dictionaries */ dic1 = dictionary_new(DICTMINSZ); CuAssertPtrNotNull(tc, dic1); for (i = 1 ; i < 10; ++i) { sprintf(sec_name, "sec%d", i); dictionary_set(dic1, sec_name, ""); for (j = 1 ; j < 10; ++j) { sprintf(key_name, "%s:key%d", sec_name, j); dictionary_set(dic1, key_name, "dummy_value"); } } dic2 = dictionary_new(DICTMINSZ); CuAssertPtrNotNull(tc, dic2); for (i = 1 ; i < 10; ++i) { sprintf(sec_name, "sec%d", i); dictionary_set(dic2, sec_name, ""); for (j = 1 ; j < 10; ++j) { sprintf(key_name, "%s:key%d", sec_name, j); dictionary_set(dic2, key_name, "dummy_value"); } } /* Make sure the dictionaries are the same */ dic1_dump = get_dump(dic1); dic2_dump = get_dump(dic2); CuAssertStrEquals(tc, dic1_dump, dic2_dump); free(dic1_dump); free(dic2_dump); /* Those tests should not change the dictionary */ dictionary_unset(dic2, NULL); dictionary_unset(dic2, "bad_key"); /* dic1 and dic2 must still be the same */ dic1_dump = get_dump(dic1); dic2_dump = get_dump(dic2); CuAssertStrEquals(tc, dic1_dump, dic2_dump); free(dic1_dump); free(dic2_dump); }
int iniparser_setstr(dictionary * ini, char * entry, char * val) { // Check whether the dictionary is case-sensitive if (ini->caseSensitive) { dictionary_set(ini, entry, val); } else { dictionary_set(ini, inistrlwc(entry), val); } return 0 ; }
void Test_dictionary_dump(CuTest *tc) { int i, j; char sec_name[32]; char key_name[64]; dictionary *dic; char *dump_buff; const char dump_real[] = "\ sec1\t[]\n\ sec1:key1\t[dummy_value]\n\ sec1:key2\t[dummy_value]\n\ sec1:key3\t[dummy_value]\n\ sec1:key4\t[dummy_value]\n\ sec2\t[]\n\ sec2:key1\t[dummy_value]\n\ sec2:key2\t[dummy_value]\n\ sec2:key3\t[dummy_value]\n\ sec2:key4\t[dummy_value]\n\ "; dic = dictionary_new(DICTMINSZ); CuAssertPtrNotNull(tc, dic); /* Try dummy values */ dictionary_dump(NULL, NULL); dictionary_dump(dic, NULL); /* Try with empty dictionary first */ dump_buff = get_dump(dic); CuAssertStrEquals(tc, "empty dictionary\n", dump_buff); free(dump_buff); /* Populate the dictionary */ for (i = 1 ; i < 3; ++i) { sprintf(sec_name, "sec%d", i); dictionary_set(dic, sec_name, ""); for (j = 1 ; j < 5; ++j) { sprintf(key_name, "%s:key%d", sec_name, j); dictionary_set(dic, key_name, "dummy_value"); } } /* Check the dump file */ dump_buff = get_dump(dic); CuAssertStrEquals(tc, dump_real, dump_buff); free(dump_buff); dictionary_del(dic); }
/* Apply template search from dictionary src to dictionary dst */ static int applyTemplateFromDictionary(dictionary *dict, dictionary *src, char *search, int overwrite) { char *key, *value, *pos; int i = 0; int found; /* -1 on not found, 0+ on actual applies */ found = -1; for(i=0; i<src->n; i++) { key = src->key[i]; value = src->val[i]; pos = strstr(key, ":"); if(value != NULL && pos != NULL) { *pos = '\0'; pos++; if(!strcmp(search, key) && (strstr(pos,":") != NULL)) { if(found < 0) found = 0; if( overwrite || !CONFIG_ISSET(pos)) { DBG("template %s setting %s to %s\n", search, pos, value); dictionary_set(dict, pos, value); found++; } } /* reverse the damage - no need for strdup() */ pos--; *pos = ':'; } } return found; }
REGISTRY_PERSON *registry_person_allocate(const char *person_guid, time_t when) { debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): allocating new person, sizeof(PERSON)=%zu", (person_guid)?person_guid:"", sizeof(REGISTRY_PERSON)); REGISTRY_PERSON *p = mallocz(sizeof(REGISTRY_PERSON)); if(!person_guid) { for(;;) { uuid_t uuid; uuid_generate(uuid); uuid_unparse_lower(uuid, p->guid); debug(D_REGISTRY, "Registry: Checking if the generated person guid '%s' is unique", p->guid); if (!dictionary_get(registry.persons, p->guid)) { debug(D_REGISTRY, "Registry: generated person guid '%s' is unique", p->guid); break; } else info("Registry: generated person guid '%s' found in the registry. Retrying...", p->guid); } } else strncpyz(p->guid, person_guid, GUID_LEN); debug(D_REGISTRY, "Registry: registry_person_allocate('%s'): creating dictionary of urls", p->guid); avl_init(&p->person_urls, person_url_compare); p->first_t = p->last_t = (uint32_t)when; p->usages = 0; registry.persons_memory += sizeof(REGISTRY_PERSON); registry.persons_count++; dictionary_set(registry.persons, p->guid, p, sizeof(REGISTRY_PERSON)); return p; }
// Check if the key is in the form section:key and if yes create the section in the dictionnary // if it doesn't exist. void InitParser::make_section_from_key(const string& key) { int pos = key.find(':'); if (!pos) return; // No ':' were found string sec = key.substr(0,pos); if (find_entry(sec)) return; // The section is already present into the dictionnary dictionary_set(dico, sec.c_str(), NULL); // Add the section key }
int main3(int argc, char * argv[]) { dictionary * ini ; char* k1 = "hello:a"; char* v1 = "nihao"; char* k2 = "hello:b"; char* v2 = "heihei"; ini = dictionary_new(0); dictionary_set(ini, k1, v1); dictionary_set(ini, "hello", NULL); dictionary_set(ini, k2, v2); iniparser_dump_json(ini, stdout); iniparser_freedict(ini); return 0 ; }
void ui_elem_add_type_id(char* name, int type_id, ui_elem* ui_elem) { if ( dictionary_contains(ui_elems, name) ) { error("UI Manager already contains element called %s!", name); } dictionary_set(ui_elems, name, ui_elem); int* type_ptr = malloc(sizeof(int)); *type_ptr = type_id; dictionary_set(ui_elem_types, name, type_ptr); char* name_copy = malloc(strlen(name) + 1); strcpy(name_copy, name); list_push_back(ui_elem_names, name_copy); }
int iniparser_setstr( dictionary * ini, char * entry, char * val ) { dictionary_set(ini, entry, val); return 0 ; }
/*--------------------------------------------------------------------------*/ int iniparser_set(dictionary * ini, const char * entry, const char * val) { int result = 0; char *lc_entry = xstrdup(entry); strlwc(lc_entry); result = dictionary_set(ini, lc_entry, val) ; free(lc_entry); return result; }
// Set the given entry with the provided value. If the entry cannot be found // -1 is returned and the entry is created. Else 0 is returned. int InitParser::set_str(const string& key, const string& val) { make_section_from_key(key); int return_val; if (find_entry(key)) return_val = 0; else return_val = -1; dictionary_set(dico, key.c_str(), val.c_str()); return return_val; }
void update_config(char *token, char *username) { bstring file = locate_config_file(); bstring tmp = bstrcpy(file); bcatcstr(tmp, ".tmp_XXXXXX"); int fd = mkstemp((char *) tmp->data); if (fd < 0) { perror("mkstemp"); exit(1); } FILE *fp = fdopen(fd, "w"); if (fp == 0) { perror("fdopen"); exit(1); } dictionary *config; struct stat statbuf; int rc = stat((char *) file->data, &statbuf); if (rc < 0 || statbuf.st_size == 0) { /* create a new empty dictionary */ config = dictionary_new(0); dictionary_set(config, "authentication", 0); } else { config = iniparser_load(bdata(file)); } iniparser_set(config, "authentication:token", token); if (username) iniparser_set(config, "authentication:user_id", username); iniparser_dump_ini(config, fp); iniparser_freedict(config); fclose(fp); if (rename(bdata(tmp), bdata(file)) < 0) { fprintf(stderr, "Error rename %s to %s: %s\n", bdata(tmp), bdata(file), strerror(errno)); exit(1); } bdestroy(tmp); bdestroy(file); }
int InitParser::set_double(const string& key, double val) { make_section_from_key(key); int return_val; if (find_entry(key)) return_val = 0; else return_val = -1; ostringstream os; os << setprecision(16) << val; dictionary_set(dico, key.c_str(), os.str().c_str()); return return_val; }
int main(int argc, char* argv[]) { dictionary* d ; char* val ; int i ; char cval[90] ; /* Allocate dictionary */ printf("allocating...\n"); d = dictionary_new(0); /* Set values in dictionary */ printf("setting %d values...\n", NVALS); for (i=0 ; i<NVALS ; i++) { sprintf(cval, "%04d", i); dictionary_set(d, cval, "salut"); } printf("getting %d values...\n", NVALS); for (i=0 ; i<NVALS ; i++) { sprintf(cval, "%04d", i); val = dictionary_get(d, cval, DICT_INVALID_KEY); if (val==DICT_INVALID_KEY) { printf("cannot get value for key [%s]\n", cval); } } printf("unsetting %d values...\n", NVALS); for (i=0 ; i<NVALS ; i++) { sprintf(cval, "%04d", i); dictionary_unset(d, cval); } if (d->n != 0) { printf("error deleting values\n"); } printf("deallocating...\n"); dictionary_del(d); return 0 ; }
frame_t *frame_factory_get(const char *key) { frame_t *frame; char *addr_str; // try to load frame using config from config cache static char key_buf[MAX_TEXTURE_NAME+4+1]; static char addr_buff[16+1]; char *file; int pf, loc; int namelen = strlen(key); if (frame_cache == NULL) { printf("frame factory not inited!\n"); return NULL; } addr_str = dictionary_get(frame_cache, (char *)key, "0x0"); frame = (frame_t *)(strtol(addr_str, NULL, 0)); if (frame != 0) { return frame; } if (strlen(key) > MAX_TEXTURE_NAME) { oslFatalError("config key too long! %s", key); } strcpy(key_buf, key); strcat(key_buf, ":f"); file = iniparser_getstring(texture_cfg, key_buf, NULL); printf("search key %s, value %s\n", key_buf, file); printf("texture cfg %p\n", texture_cfg); key_buf[namelen] = '\0'; strcat(key_buf, ":pf"); pf = iniparser_getint(texture_cfg, key_buf, OSL_IN_VRAM); key_buf[namelen] = '\0'; strcat(key_buf, ":loc"); loc = iniparser_getint(texture_cfg, key_buf, TAIKO_PF); printf("ok so far!\n");; frame = frame_create_simple(file, pf, loc); if (frame != NULL) { key_buf[namelen] = '\0'; sprintf(addr_buff, "%d", (int)frame); dictionary_set(frame_cache, key_buf, addr_buff); } return frame; }
int main (int argc, char *argv[]) { LPDICTIONARY lpDict; // Ptr to workspace dictionary LPWCHAR lpwVal; int i; WCHAR cval[90]; /* Allocate dictionary */ printf ("allocating...\n"); lpDict = dictionary_new (0); /* Set values in dictionary */ printf ("setting %d values...\n", NVALS); for (i = 0; i < NVALS; i++) { wsprintfW (cval, L"%04d", i); dictionary_set (lpDict, cval, L"salut"); } // End FOR printf ("getting %d values...\n", NVALS); for (i = 0; i < NVALS; i++) { wsprintfW (cval, L"%04d", i); lpwVal = dictionary_get (lpDict, cval, DICT_INVALID_KEY, NULL); if (lpwVal EQ DICT_INVALID_KEY) printf ("cannot get value for key [%s]\n", cval); } // End FOR printf ("unsetting %d values...\n", NVALS); for (i = 0; i < NVALS; i++) { wsprintfW (cval, L"%04d", i); dictionary_unset (lpDict, cval); } // End FOR if (lpDict->n NE 0) printf ("error deleting values\n"); printf ("deallocating...\n"); dictionary_del (lpDict); lpDict = NULL; return 0; } // End main
/*! * \file * \brief Operator \c def: add an entry into the \c dictionary. * * The entry is composed of a protected label on the top of the stack and the value under it. * Both values are removed from the stack. * Nothing else is modified. * * If the stack is not deep enough or the first one is not a \c value_protected_label, a \c basic_type_error is returned. * * assert is enforced. * * \author Jérôme DURAND-LOSE * \version 1 * \date 2015 * \copyright GNU Public License. */ static basic_type operator_def_evaluate ( chunk const ch , va_list va ) { interpretation_context ic = va_arg( va , interpretation_context); chunk ch1 = linked_list_chunk_pop_front(ic->stack); chunk ch2 = linked_list_chunk_pop_front(ic-> stack); if(!(ch1) || !(ch2)){ return basic_type_error; } if(value_is_protected_label(ch1)){ dictionary_set(ic->dic , basic_type_get_pointer(chunk_answer_message(ch1 , "value_get_value")) , ch2); chunk_destroy(ch1); chunk_destroy(ch2); return basic_type_void; } chunk_destroy(ch1); chunk_destroy(ch2); return basic_type_error; }
/* Private: add an entry to the dictionary */ void iniparser_add_entry (dictionary * d, char * sec, char * key, char * val) { char longkey[2*ASCIILINESZ+1]; /* Make a key as section:keyword */ if (key) sprintf (longkey, "%s:%s", sec, key); else strcpy (longkey, sec); /* Add (key,val) to dictionary */ dictionary_set (d, longkey, val); }
/* Private: add an entry to the dictionary return 0 on success, non-zero on error */ static int iniparser_add_entry( dictionary * d, char * sec, char * key, char * val) { char longkey[2*ASCIILINESZ+1]; /* Make a key as section:keyword */ if (key!=NULL) { sprintf(longkey, "%s:%s", sec, key); } else { strcpy(longkey, sec); } /* Add (key,val) to dictionary */ return dictionary_set(d, longkey, val); }
REGISTRY_MACHINE_URL *registry_machine_url_allocate(REGISTRY_MACHINE *m, REGISTRY_URL *u, time_t when) { debug(D_REGISTRY, "registry_machine_url_allocate('%s', '%s'): allocating %zu bytes", m->guid, u->url, sizeof(REGISTRY_MACHINE_URL)); REGISTRY_MACHINE_URL *mu = mallocz(sizeof(REGISTRY_MACHINE_URL)); mu->first_t = mu->last_t = (uint32_t)when; mu->usages = 1; mu->url = u; mu->flags = REGISTRY_URL_FLAGS_DEFAULT; registry.machines_urls_memory += sizeof(REGISTRY_MACHINE_URL); debug(D_REGISTRY, "registry_machine_url_allocate('%s', '%s'): indexing URL in machine", m->guid, u->url); dictionary_set(m->machine_urls, u->url, mu, sizeof(REGISTRY_MACHINE_URL)); registry_url_link(u); return mu; }
void Test_iniparser_getdouble(CuTest *tc) { dictionary *dic; /* NULL test */ CuAssertDblEquals(tc, -42, iniparser_getdouble(NULL, NULL, -42), 0); CuAssertDblEquals(tc, 4.2, iniparser_getdouble(NULL, "dummy", 4.2), 0); /* Check the def return element */ dic = dictionary_new(10); CuAssertDblEquals(tc, 3.1415, iniparser_getdouble(dic, "dummy", 3.1415), 0); CuAssertDblEquals(tc, 0xFFFFFFFF, iniparser_getdouble(dic, NULL, 0xFFFFFFFF), 0); CuAssertDblEquals(tc, -0xFFFFFFFF, iniparser_getdouble(dic, "dummy", -0xFFFFFFFF), 0); /* Insert some values */ dictionary_set(dic, "double", ""); dictionary_set(dic, "double:good0", "0"); dictionary_set(dic, "double:good1", "-0"); dictionary_set(dic, "double:good2", "1.0"); dictionary_set(dic, "double:good3", "3.1415"); dictionary_set(dic, "double:good4", "6.6655957"); dictionary_set(dic, "double:good5", "-123456789.123456789"); /* Add dummy stuff too */ dictionary_set(dic, "double:bad0", "foo"); /* Get back the values */ CuAssertDblEquals(tc, 0, iniparser_getdouble(dic, "double:good0", 0xFF), 0); CuAssertDblEquals(tc, 0, iniparser_getdouble(dic, "double:good1", 0xFF), 0); CuAssertDblEquals(tc, 1.0, iniparser_getdouble(dic, "double:good2", 0xFF), 0); CuAssertDblEquals(tc, 3.1415, iniparser_getdouble(dic, "double:good3", 0xFF), 0); CuAssertDblEquals(tc, 6.6655957, iniparser_getdouble(dic, "double:good4", 0xFF), 0); CuAssertDblEquals(tc, -123456789.123456789, iniparser_getdouble(dic, "double:good5", 0xFF), 0); CuAssertDblEquals(tc, 0, iniparser_getdouble(dic, "double:bad0", 42.42), 0); dictionary_del(dic); }
REGISTRY_MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) { debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(REGISTRY_MACHINE)); REGISTRY_MACHINE *m = mallocz(sizeof(REGISTRY_MACHINE)); strncpyz(m->guid, machine_guid, GUID_LEN); debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid); m->machine_urls = dictionary_create(DICTIONARY_FLAGS); m->first_t = m->last_t = (uint32_t)when; m->usages = 0; registry.machines_memory += sizeof(REGISTRY_MACHINE); registry.machines_count++; dictionary_set(registry.machines, m->guid, m, sizeof(REGISTRY_MACHINE)); return m; }
/* Private: add an entry to the dictionary */ static void iniparser_add_entry( dictionary * d, char * sec, char * key, char * val) { // TODO: Better check on this again! // fixed length, i dunno char longkey[2*ASCIILINESZ+1]; /* Make a key as section:keyword */ if (key!=NULL) { sprintf(longkey, "%s:%s", sec, key); } else { strcpy(longkey, sec); } /* Add (key,val) to dictionary */ dictionary_set(d, longkey, val); return ; }
int dictionary_merge(dictionary* source, dictionary* dest, int overwrite, int warn, const char* warnStr) { int i = 0; int clobber = 1; if( source == NULL || dest == NULL) return -1; if(warnStr == NULL) warnStr = ""; for(i = 0; i < source->n; i++) { clobber = 1; if(source->key[i] == NULL) continue; /* do not overwrite with an empty key */ if((source->val[i] == NULL) || (strlen(source->val[i])==0)) continue; /* no need to warn for settings whose value will not change */ if((strcmp(dictionary_get(dest,source->key[i],""),"") != 0) && (strcmp(dictionary_get(dest,source->key[i],""),source->val[i]) != 0)) { if(overwrite && warn) { WARNING("Warning: %s=\"%s\" : value \"%s\" takes priority %s\n", source->key[i], dictionary_get(dest,source->key[i],""), source->val[i], warnStr); } if(!overwrite) { clobber = 0; } } if (clobber) { if(dictionary_set( dest, source->key[i], source->val[i]) != 0) { return -1; } } } return 0; }
/*--------------------------------------------------------------------------*/ dictionary * iniparser_load(const char * ininame) { FILE * in ; char line [ASCIILINESZ+1] ; char section [ASCIILINESZ+1] ; char key [ASCIILINESZ+1] ; char tmp [ASCIILINESZ+1] ; char val [ASCIILINESZ+1] ; int last=0 ; int len ; int lineno=0 ; int errs=0; dictionary * dict ; if ((in=fopen(ininame, "r"))==NULL) { fprintf(stderr, "iniparser: cannot open %s\n", ininame); return NULL ; } dict = dictionary_new(0) ; if (!dict) { fclose(in); return NULL ; } memset(line, 0, ASCIILINESZ); memset(section, 0, ASCIILINESZ); memset(key, 0, ASCIILINESZ); memset(val, 0, ASCIILINESZ); last=0 ; while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) { lineno++ ; len = (int)strlen(line)-1; /* Safety check against buffer overflows */ if (line[len]!='\n') { fprintf(stderr, "iniparser: input line too long in %s (%d)\n", ininame, lineno); dictionary_del(dict); fclose(in); return NULL ; } /* Get rid of \n and spaces at end of line */ while ((len>=0) && ((line[len]=='\n') || (isspace(line[len])))) { line[len]=0 ; len-- ; } /* Detect multi-line */ if (line[len]=='\\') { /* Multi-line value */ last=len ; continue ; } else { last=0 ; } switch (iniparser_line(line, section, key, val)) { case LINE_EMPTY: case LINE_COMMENT: break ; case LINE_SECTION: errs = dictionary_set(dict, section, NULL); break ; case LINE_VALUE: sprintf(tmp, "%s:%s", section, key); errs = dictionary_set(dict, tmp, val) ; break ; case LINE_ERROR: fprintf(stderr, "iniparser: syntax error in %s (%d):\n", ininame, lineno); fprintf(stderr, "-> %s\n", line); errs++ ; break; default: break ; } memset(line, 0, ASCIILINESZ); last=0; if (errs<0) { fprintf(stderr, "iniparser: memory allocation failure\n"); break ; } } if (errs) { dictionary_del(dict); dict = NULL ; } fclose(in); return dict ; }
/*--------------------------------------------------------------------------*/ int iniparser_set(dictionary * ini, char * entry, char * val) { return dictionary_set(ini, strlwc(entry), val) ; }
void Test_iniparser_getint(CuTest *tc) { unsigned i; char key_name[64]; dictionary *dic; const struct { int num; const char *value; } good_val[] = { { 0, "0" }, { 1, "1" }, { -1, "-1" }, { 1000, "1000" }, { 077, "077" }, { -01000, "-01000" }, { 0xFFFF, "0xFFFF" }, { -0xFFFF, "-0xFFFF" }, { 0x4242, "0x4242" }, { 0, NULL} /* must be last */ }; const char *bad_val[] = { "", "notanumber", "0x", "k2000", " ", "0xG1" }; /* NULL test */ CuAssertIntEquals(tc, -42, iniparser_getint(NULL, NULL, -42)); CuAssertIntEquals(tc, -42, iniparser_getint(NULL, "dummy", -42)); /* Check the def return element */ dic = dictionary_new(10); CuAssertIntEquals(tc, 42, iniparser_getint(dic, "dummy", 42)); CuAssertIntEquals(tc, 0xFFFF, iniparser_getint(dic, NULL, 0xFFFF)); CuAssertIntEquals(tc, -0xFFFF, iniparser_getint(dic, "dummy", -0xFFFF)); dictionary_del(dic); /* Generic dictionary */ dic = dictionary_new(10); for (i = 0; good_val[i].value != NULL; ++i) { sprintf(key_name, "int:value%d", i); dictionary_set(dic, key_name, good_val[i].value); } for (i = 0; good_val[i].value != NULL; ++i) { sprintf(key_name, "int:value%d", i); CuAssertIntEquals(tc, good_val[i].num, iniparser_getint(dic, key_name, 0)); } dictionary_del(dic); /* Test bad names */ dic = dictionary_new(10); for (i = 0; i < sizeof (bad_val) / sizeof (char *); ++i) { sprintf(key_name, "int:bad%d", i); dictionary_set(dic, key_name, bad_val[i]); } for (i = 0; i < sizeof (bad_val) / sizeof (char *); ++i) { sprintf(key_name, "int:bad%d", i); CuAssertIntEquals(tc, 0, iniparser_getint(dic, key_name, 0)); } dictionary_del(dic); }