summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2025-09-15 20:59:38 -0400
committerpommicket <pommicket@gmail.com>2025-09-15 20:59:38 -0400
commit09821002b314b7c8517f16d351b1c6b1088a47dc (patch)
treeaeb54e83677eb6383daebaf0aeadbc80cc63f4a8 /examples
parent06e068ea14220903bd95fea94d44fe3f1a918481 (diff)
Better examples
Diffstat (limited to 'examples')
-rw-r--r--examples/conf.pom18
-rw-r--r--examples/custom_alloc.c93
-rw-r--r--examples/read_conf.c20
3 files changed, 117 insertions, 14 deletions
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);
}