struct f_tree * f_new_tree(void) { struct f_tree * ret; ret = cfg_alloc(sizeof(struct f_tree)); ret->left = ret->right = NULL; ret->from.type = ret->to.type = T_VOID; ret->from.val.i = ret->to.val.i = 0; ret->data = NULL; return ret; }
/*}}}*/ } priv_t; static char * xfree (char *s) /*{{{*/ { if (s) free (s); return NULL; }/*}}}*/ static bool_t xcopy (char **buf, const char *str) /*{{{*/ { if (*buf) free (*buf); *buf = str ? strdup (str) : NULL; return (! str) || *buf ? true : false; }/*}}}*/ static void priv_clear (priv_t *p) /*{{{*/ { if (p) { p -> x_agn = 0; p -> from = xfree (p -> from); p -> receiver = charc_free_all (p -> receiver); p -> prev = NULL; p -> info = xfree (p -> info); } }/*}}}*/ static priv_t * priv_free (priv_t *p) /*{{{*/ { if (p) { priv_clear (p); if (p -> lg) log_free (p -> lg); if (p -> cfg) cfg_free (p -> cfg); free (p); } return NULL; }/*}}}*/ static priv_t * priv_alloc (void) /*{{{*/ { priv_t *p; if (p = (priv_t *) malloc (sizeof (priv_t))) if (p -> cfg = cfg_alloc (cfgfile)) { p -> is_local = false; p -> x_agn = 0; p -> from = NULL; p -> receiver = NULL; p -> prev = NULL; p -> info = NULL; if (! (p -> lg = log_alloc (NULL, program, loglevel))) p = priv_free (p); } else { free (p); p = NULL; } return p; }/*}}}*/
/* * test parsing a file or a buffer directly. when calling a parsing method * the memory pointed by the cfg_t will be released automatically. * when you are done with the library make sure to call cfg_free(). */ int main(void) { cfg_status_t err; cfg_t *st; cfg_entry_t *entry; char buf[] = "key1=value1\n\n\n\n" \ "[s]\n" \ "key2=value2\n" \ "key3=value3\n" \ "key4=value4\n"; char in_file[] = "test.cfg"; char out_file[] = "out.cfg"; cfg_char *write_buf, *ptr; cfg_uint32 write_len; clock_t begin, end; double time_spent; (void)entry; puts("[cfg2 test]"); puts("* init"); /* init the structure with cache buffer size of 4. this means that 4 unique * (and fast) entries will be cached at all times. */ st = cfg_alloc(); err = cfg_verbose_set(st, VERBOSE); err = cfg_cache_size_set(st, 4); puts("* parse"); err = cfg_buffer_parse(st, buf, strlen(buf), CFG_TRUE); begin = clock(); err = cfg_file_parse(st, in_file); end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("time spent parsing %s: %.4f sec\n", in_file, time_spent); if (err > 0) { printf("cfg_file_parse() ERROR: %d\n", err); goto exit; } #if (PRINT_TESTS == 1) printf("press any key to continue..."); getchar(); puts(""); puts("* actions"); /* test setting a new value */ puts("\nattempting to set value..."); err = cfg_value_set(st, "section1", "key1", "\tnew value1", CFG_FALSE); if (!err) printf("new value: %s\n", cfg_value_get(st, "section1", "key1")); else printf("setting value error: %d\n", err); /* create an entry in the root section */ puts(""); entry = cfg_root_entry_add(st, "some_new_key", "some_new_value"); printf("entry added: %s\n", entry ? "OK" : "ERROR"); /* print some values */ puts(""); printf("find value by key (key1): %s\n", cfg_value_get(st, "section1", "key1")); printf("find value by key (key=8): %s\n", cfg_value_get(st, "section1", "key=8")); /* test a section */ puts(""); entry = cfg_entry_get(st, "section1", "key6"); printf("test entry from section: %f\n", (entry) ? cfg_value_to_double(cfg_entry_value_get(st, entry)) : -1.0); entry = cfg_entry_get(st, "section1", "key9"); printf("test entry from section: %s\n", (entry) ? cfg_entry_value_get(st, entry) : "not found"); puts(""); puts("test conversations:"); printf("%u\n", cfg_value_to_bool("0")); /* test hex <-> char */ puts(""); entry = cfg_entry_get(st, "section2", "key13"); ptr = cfg_hex_to_char(cfg_entry_value_get(st, entry)); puts(ptr); puts(cfg_char_to_hex(ptr)); #endif puts(""); printf("test delete: %d\n", cfg_section_delete(st, "section2")); #if (PRINT_WRITE_BUF == 1) puts(""); err = cfg_buffer_write(st, &write_buf, &write_len); printf("write buf (%d):\n", write_len); if (write_buf) { puts(write_buf); free(write_buf); } #else (void)write_buf, (void)write_len; #endif begin = clock(); err = cfg_file_write(st, out_file); end = clock(); printf("write_file() status: %d\n", err); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("time spent writing %s: %.4f sec\n", out_file, time_spent); exit: puts(""); puts("* free"); cfg_free(st); puts("* end"); return 0; }