static int write_type(scconf_context * config, scconf_block * block, scconf_entry * entry, int depth) { void *parm = entry->parm; void *arg = entry->arg; int (*callback_func) (scconf_context * config, scconf_block * block, scconf_entry * entry, int depth) = (int (*)(scconf_context *, scconf_block *, scconf_entry *, int)) parm; int r = 0; if (config->debug) { fprintf(stderr, "encoding '%s'\n", entry->name); } switch (entry->type) { case SCCONF_CALLBACK: if (parm) { r = callback_func(config, block, entry, depth); } break; case SCCONF_BLOCK: if (parm) { scconf_block *subblock; const scconf_list *name = (const scconf_list *) arg; subblock = scconf_block_add(config, block, entry->name, name); r = write_entries(config, subblock, (scconf_entry *) parm, depth + 1); } break; case SCCONF_LIST: if (parm) { const scconf_list *val = (const scconf_list *) parm; scconf_item_add(config, block, NULL, SCCONF_ITEM_TYPE_VALUE, entry->name, val); if (entry->flags & SCCONF_VERBOSE) { char *buf = scconf_list_strdup(val, ", "); printf("%s = %s\n", entry->name, buf); free(buf); } } break; case SCCONF_BOOLEAN: if (parm) { const int val = * (int* ) parm; scconf_put_bool(block, entry->name, val); if (entry->flags & SCCONF_VERBOSE) { printf("%s = %s\n", entry->name, val == 0 ? "false" : "true"); } } break; case SCCONF_INTEGER: if (parm) { const int val = * (int*) parm; scconf_put_int(block, entry->name, val); if (entry->flags & SCCONF_VERBOSE) { printf("%s = %i\n", entry->name, val); } } break; case SCCONF_STRING: if (parm) { const char *val = (const char *) parm; scconf_put_str(block, entry->name, val); if (entry->flags & SCCONF_VERBOSE) { printf("%s = %s\n", entry->name, val); } } break; default: fprintf(stderr, "invalid configuration type: %d\n", entry->type); } if (r) { fprintf(stderr, "encoding of configuration entry '%s' failed.\n", entry->name); return r; } entry->flags |= SCCONF_PRESENT; return 0; }
int main(int argc, char **argv) { #ifdef ADD_TEST scconf_block *foo_block = NULL; scconf_item *foo_item = NULL; scconf_list *foo_list = NULL; #endif scconf_context *conf = NULL; scconf_entry entry[] = { {"ldap", SCCONF_CALLBACK, SCCONF_VERBOSE | SCCONF_ALL_BLOCKS, (void *) ldap_cb, NULL}, {"card", SCCONF_CALLBACK, SCCONF_VERBOSE | SCCONF_ALL_BLOCKS, (void *) card_cb, NULL}, {NULL, 0, 0, NULL, NULL} }; char *in = NULL, *out = NULL; int r; if (argc != 3) { printf("Usage: test-conf <in.conf> <out.conf>\n"); return 1; } in = argv[argc - 2]; out = argv[argc - 1]; conf = scconf_new(in); if (!conf) { printf("scconf_new failed\n"); return 1; } if (scconf_parse(conf) < 1) { printf("scconf_parse failed: %s\n", conf->errmsg); scconf_free(conf); return 1; } conf->debug = 1; if (scconf_parse_entries(conf, NULL, entry) != 0) { printf("scconf_parse_entries failed\n"); scconf_free(conf); return 1; } #ifdef ADD_TEST scconf_list_add(&foo_list, "value1"); scconf_list_add(&foo_list, "value2"); foo_block = (scconf_block *) scconf_find_block(conf, NULL, "foo"); foo_block = scconf_block_add(conf, foo_block, "block1", foo_list); foo_block = scconf_block_add(conf, foo_block, "block2", foo_list); scconf_list_add(&foo_list, "value3"); /* this will not segfault as type SCCONF_ITEM_TYPE_COMMENT is used */ scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_COMMENT, NULL, "# comment1"); scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_VALUE, "list1", foo_list); foo_block = NULL; scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_BLOCK, "block3", (void *) scconf_find_block(conf, NULL, "foo")); scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_VALUE, "list2", foo_list); scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_COMMENT, NULL, "# comment2"); if (write_entries(conf, foo_list) != 0) { printf("scconf_write_entries failed\n"); scconf_free(conf); return 1; } scconf_list_destroy(foo_list); #endif if ((r = scconf_write(conf, out)) != 0) { printf("scconf_write: %s\n", strerror(r)); } else { printf("Successfully rewrote file \"%s\" as \"%s\"\n", in, out); } scconf_free(conf); return 0; }