diff options
author | pommicket <pommicket@gmail.com> | 2025-09-14 00:33:18 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-09-14 00:33:18 -0400 |
commit | d8192f73672488234bd12319229402153e7b6c21 (patch) | |
tree | 930ab2f93e337dfa6680945521e48154d51b33f9 | |
parent | 1406758a5a2e38392e54cb7ba7a00a0f80ee1567 (diff) |
pom_conf_print implementation
-rw-r--r-- | examples/conf.pom | 7 | ||||
-rw-r--r-- | examples/read_conf.c | 9 | ||||
-rw-r--r-- | pom.c | 29 |
3 files changed, 34 insertions, 11 deletions
diff --git a/examples/conf.pom b/examples/conf.pom index 27037d9..0edc83f 100644 --- a/examples/conf.pom +++ b/examples/conf.pom @@ -1,4 +1,7 @@ [number] one = 1 -two = 2 -three = 3 +two = 2x"\? +three = "3 +is +the + best" diff --git a/examples/read_conf.c b/examples/read_conf.c index 45a6520..6b7c18f 100644 --- a/examples/read_conf.c +++ b/examples/read_conf.c @@ -21,15 +21,8 @@ int main(int argc, char **argv) { free(error); return EXIT_FAILURE; } - pom_item_iter *iter = NULL; - const pom_item *item; pom_conf_merge(conf,pom_conf_section(conf2,"j")); - while ((item = pom_conf_next_item(conf, &iter))) { - printf("Key: %s, Value: %s\n", item->key, item->value); - const char *file; uint64_t line; - pom_conf_location(conf,item->key,&file,&line); - printf(" Defined at %s:%" PRIu64 "\n",file,line); - } + pom_conf_print(conf); pom_conf_free(conf); pom_conf_free(conf2); } @@ -2,7 +2,6 @@ TODO: - error_to_string - conf_copy -- conf_print - typed get functions */ #include "pom.h" @@ -1412,3 +1411,31 @@ pom_conf_merge(pom_conf *conf, const pom_conf *other) { *conf = new_conf; return true; } + +void +pom_conf_print(const pom_conf *conf) { + const pom_item *item; + pom_item_iter *iter = NULL; + while ((item = pom_conf_next_item(conf, &iter))) { + printf("%s: \"", item->key); + char c; + for (const char *p = item->value; (c = *p); p++) { + if (c == '\n') { + printf("\\n"); + } else if (c == '\t') { + printf("\\t"); + } else if (c == '\r') { + printf("\\r"); + } else if (c == '\\') { + printf("\\\\"); + } else if (c == '"') { + printf("\\\""); + } else if (c < 0x20) { + printf("\\x%02X", c); + } else { + putchar(c); + } + } + printf("\"\n"); + } +} |