diff options
author | pommicket <pommicket@gmail.com> | 2025-09-15 21:31:02 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-09-15 21:31:02 -0400 |
commit | b93385c0fded8d6c232267a8bd9293153baea777 (patch) | |
tree | e13f68e0802310be4ee5f68217da700e51d8ff86 /examples | |
parent | 09821002b314b7c8517f16d351b1c6b1088a47dc (diff) |
all_functions example
Diffstat (limited to 'examples')
-rw-r--r-- | examples/all_functions.c | 156 | ||||
-rw-r--r-- | examples/conf.pom | 4 |
2 files changed, 158 insertions, 2 deletions
diff --git a/examples/all_functions.c b/examples/all_functions.c new file mode 100644 index 0000000..843cf03 --- /dev/null +++ b/examples/all_functions.c @@ -0,0 +1,156 @@ +// Demonstrates almost all of libpom's API + +#include <pom.h> +#include <stdlib.h> +#include <string.h> +#include <fcntl.h> +#include <unistd.h> +#include <inttypes.h> +#include <assert.h> + +static size_t custom_read(void *udata, char *buf, size_t size) { + int fd = (int)(intptr_t)udata; + size_t total_read = 0; + while (true) { + // must call read in a loop to fill buf up as much as possible! + // (read isn't guaranteed to read len bytes even if it could) + ssize_t ret = read(fd, buf, size); + if (ret <= 0) { + // read error/end-of-file + break; + } else { + total_read += ret; + buf += ret; + size -= ret; + } + } + return total_read; +} + +int main(void) { + pom_error *error; + // ordinary usage: load from a path + pom_conf *conf = pom_load_path(NULL, "conf.pom", &error); + if (error) { + pom_error_print(error); + free(error); + return EXIT_FAILURE; + } + // get a key + const char *indentation_type = pom_conf_get(conf, "indentation-type"); + printf("indentation-type: %s\n", indentation_type ? indentation_type : "(none)"); + // free configuration + pom_conf_free(conf); + + pom_settings settings = {0}; + strcpy(settings.error_lang, "fr"); // erreurs en français + // load configuration with custom settings and a custom reader + int fd = open("conf.pom", O_RDONLY); + conf = pom_load(&settings, + "conf.pom", // file name for error messages + custom_read, // read function + (void *)(intptr_t)fd, // will be passed as 1st argument to custom_read + &error + ); + if (error) { + pom_error_print(error); + free(error); + return EXIT_FAILURE; + } + + // nicer way of doing what we did above + printf("indentation-type: %s\n", pom_conf_get_or_default(conf, "indentation-type", "(none)")); + + // parse value as signed integer + int64_t int_val; + const char *bad_val; + if ((bad_val = pom_conf_get_int(conf, "tab-size", &int_val))) { + printf("bad tab size: %s\n", *bad_val ? bad_val : "<missing>"); + } else { + printf("tab size: %" PRId64 "\n", int_val); + } + + // parse value as unsigned integer, use default of 2 + uint64_t uint_val; + if ((bad_val = pom_conf_get_uint_or_default(conf, "padding-pixels", &uint_val, 2))) { + printf("bad padding pixels: %s\n", bad_val); + } else { + printf("padding pixels: %" PRIu64 "\n", uint_val); + } + + // parse value as double + double float_val; + if ((bad_val = pom_conf_get_float_or_default(conf, "font-size", &float_val, 12.0))) { + printf("bad font size: %s\n", bad_val); + } else { + printf("font size: %f\n", float_val); + } + + // parse value as boolean + bool bool_val; + if ((bad_val = pom_conf_get_bool_or_default(conf, "show-line-numbers", &bool_val, true))) { + // get location where key was defined + const char *file; + uint64_t line; + pom_conf_location(conf, "show-line-numbers", &file, &line); + printf("bad show-line-numbers: %s (defined at %s:%" PRIu64 ")\n", + bad_val, file, line); + } else { + printf("show line numbers: %d\n", bool_val); + } + + // extract section out of configuration + const pom_conf *file_extensions = pom_conf_section(conf, "file-extensions"); + + // parse value as list + char **Cpp_extensions = pom_conf_get_list(file_extensions, "Cpp"); + if (Cpp_extensions) { + for (size_t i = 0; Cpp_extensions[i]; i++) + printf("- Defined C++ extension %s\n", Cpp_extensions[i]); + } else { + printf("no extensions defined for C++\n"); + } + free(Cpp_extensions); + + // iterate over keys in section + pom_key_iter *key_iter = NULL; + const char *key; + const pom_conf *plugins = pom_conf_section(conf, "plug-in"); + while ((key = pom_conf_next_key(plugins, &key_iter))) { + const pom_conf *plugin = pom_conf_section(plugins, key); + const char *path = pom_conf_get_or_default(plugin, "path", "(none)"); + bool enabled; + pom_conf_get_bool_or_default(plugin, "enabled", &enabled, true); + printf("plug-in: %s (path = %s, enabled = %d)\n", key, path, enabled); + } + + // load config from string + pom_conf *overrides = pom_load_string(NULL, "<built-in overrides>", + "tab-size = 12", &error); + assert(!error); + + // create a copy of a configuration + pom_conf *copy = pom_conf_copy(conf); + + // merge configurations + pom_conf_merge(copy, overrides); + + // iterate over items (key-value pairs) in configuration + const pom_item *item; + pom_item_iter *item_iter = NULL; + while ((item = pom_conf_next_item(copy, &item_iter))) { + if (strchr(item->key, 'b')) + printf("\t%s: %s\n", item->key, item->value); + } + + pom_unread_key_iter *unread_iter = NULL; + const char *unread; + while ((unread = pom_conf_next_unread_key(copy, &unread_iter))) { + printf("unrecognized key %s\n", unread); + } + + pom_conf_free(copy); + pom_conf_free(overrides); + pom_conf_free(conf); + return 0; +} diff --git a/examples/conf.pom b/examples/conf.pom index fe9fe2f..82e1abd 100644 --- a/examples/conf.pom +++ b/examples/conf.pom @@ -4,8 +4,8 @@ tab-size = 4 font-size = "18" [file-extensions] -C = .c -Cpp = .cpp, .h, .hpp +C = .c, .h +Cpp = .cpp, .hpp, .cc, .hh [plug-in.edit-over-ssh] path = ~/misc/edit-over-ssh.so |