diff options
author | pommicket <pommicket@gmail.com> | 2025-09-15 20:59:38 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-09-15 20:59:38 -0400 |
commit | 09821002b314b7c8517f16d351b1c6b1088a47dc (patch) | |
tree | aeb54e83677eb6383daebaf0aeadbc80cc63f4a8 | |
parent | 06e068ea14220903bd95fea94d44fe3f1a918481 (diff) |
Better examples
-rw-r--r-- | CMakeLists.txt | 9 | ||||
-rw-r--r-- | examples/conf.pom | 18 | ||||
-rw-r--r-- | examples/custom_alloc.c | 93 | ||||
-rw-r--r-- | examples/read_conf.c | 20 |
4 files changed, 123 insertions, 17 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 69291e6..0c81004 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,9 +14,12 @@ set_target_properties(pom-shared PROPERTIES OUTPUT_NAME pom) add_executable(tests tests/errors.c tests/location.c tests/parsing.c tests/interpretation.c tests/main.c) target_include_directories(tests PRIVATE .) target_link_libraries(tests pom) -add_executable(read_conf examples/read_conf.c) -target_include_directories(read_conf PRIVATE .) -target_link_libraries(read_conf pom) +add_executable(example_read_conf examples/read_conf.c) +target_include_directories(example_read_conf PRIVATE .) +target_link_libraries(example_read_conf pom) +add_executable(example_custom_alloc examples/custom_alloc.c) +target_include_directories(example_custom_alloc PRIVATE .) +target_link_libraries(example_custom_alloc pom) install(TARGETS pom pom-shared DESTINATION lib) install(FILES pom.pc DESTINATION lib) diff --git a/examples/conf.pom b/examples/conf.pom index 6011308..fe9fe2f 100644 --- a/examples/conf.pom +++ b/examples/conf.pom @@ -1 +1,17 @@ -things = \,,,76 +indentation-type = tabs +show-line-numbers = yes +tab-size = 4 +font-size = "18" + +[file-extensions] +C = .c +Cpp = .cpp, .h, .hpp + +[plug-in.edit-over-ssh] +path = ~/misc/edit-over-ssh.so +enabled = yes + +[plug-in.wrap-text] +path = ~/misc/wrap_text_v3.5.7.so +enabled = no + diff --git a/examples/custom_alloc.c b/examples/custom_alloc.c new file mode 100644 index 0000000..42843ce --- /dev/null +++ b/examples/custom_alloc.c @@ -0,0 +1,93 @@ +#include <stdint.h> +#include <stdlib.h> +#include <stdalign.h> +#include <stddef.h> +#include <string.h> + +#include <pom.h> + +// Custom memory allocation functions +typedef struct { + uint64_t allocations, frees; + size_t memory_used; + alignas(max_align_t) char memory[100000]; +} Memory; +static void *my_calloc(void *udata, size_t nmemb, size_t sz) { + Memory *my_memory = udata; + char *mem = (char *)my_memory->memory + my_memory->memory_used; + size_t new_memory_used = my_memory->memory_used; + while ((uintptr_t)mem & 7) + mem++, new_memory_used++; + new_memory_used += 8 + nmemb * sz; + if (new_memory_used > sizeof my_memory->memory) { + // out of memory: return NULL + return NULL; + } + my_memory->allocations++; + my_memory->memory_used = new_memory_used; + // store size of allocation + *(uint64_t *)mem = nmemb * sz; + return mem + sizeof (uint64_t); +} +static void my_free(void *udata, void *ptr) { + if (!ptr) { + // free must do nothing when null pointer is passed + return; + } + Memory *my_memory = udata; + my_memory->frees++; +} + +static void *my_realloc(void *udata, void *ptr, size_t new_size) { + if (!ptr) { + // realloc(ptr = NULL) is equivalent to malloc + return my_calloc(udata, new_size, 1); + } + // inefficient but correct realloc implementation: + // just free the old memory and allocate brand new memory + size_t old_size = ((uint64_t *)ptr)[-1]; + void *new_ptr = my_calloc(udata, new_size, 1); + if (!new_ptr) { + // out-of-memory: return NULL + return NULL; + } + memcpy(new_ptr, ptr, old_size); + my_free(udata, ptr); + return new_ptr; +} + +int main(void) { + pom_error *error; + pom_settings settings = {0}; + // set allocation functions + settings.calloc = my_calloc; + settings.realloc = my_realloc; + settings.free = my_free; + static Memory my_memory; + // set udata pointer which is passed to the functions + settings.allocator_udata = &my_memory; + + pom_conf *conf = pom_load_string(&settings, "conf.pom", + "foo = bar\n" + "[core]\n" + "list = 1,6,78,9\n", + &error); + if (!conf) { + pom_error_print(error); + // error must be freed with custom allocation function + my_free(&my_memory, error); + return EXIT_FAILURE; + } + char **list = pom_conf_get_list(conf, "core.list"); + for (size_t i = 0; list[i]; i++) + printf("- %s\n",list[i]); + // lists must be freed with custom allocation function + my_free(&my_memory, list); + pom_conf_free(conf); + if (my_memory.allocations != my_memory.frees) { + // if this ever happens, it's a bug in libpom + fprintf(stderr, "libpom is leaking memory!!!\n"); + return EXIT_FAILURE; + } + return 0; +} diff --git a/examples/read_conf.c b/examples/read_conf.c index d96e190..8f290f6 100644 --- a/examples/read_conf.c +++ b/examples/read_conf.c @@ -1,24 +1,18 @@ #include <stdlib.h> -#include <string.h> -#include <inttypes.h> - #include <pom.h> -int main(int argc, char **argv) { +int main(void) { pom_error *error; - pom_settings settings = {0}; - strcpy(settings.error_lang, "fr"); - pom_conf *conf = pom_load_path(&settings, argc >= 2 ? argv[1] : "conf.pom", &error); + pom_conf *conf = pom_load_path(NULL, "conf.pom", &error); if (!conf) { pom_error_print(error); free(error); return EXIT_FAILURE; } - char **list = pom_conf_get_list(conf, "things"); - for (size_t i = 0; list[i]; i++) { - printf("%s\n",list[i]); - } - free(list); - pom_conf_print(conf); + const char *indentation_type = pom_conf_get(conf, "indentation-type"); + if (indentation_type) + printf("Indenting with %s\n", indentation_type); + else + printf("No indentation type set!\n"); pom_conf_free(conf); } |