From 12891d0e8551e3d6bb7bf1429f936e04be4da8b5 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Mon, 23 Oct 2023 10:31:31 +0000 Subject: Refactor tipidee-config code to accommodate for headers Signed-off-by: Laurent Bercot --- src/config/repo.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/config/repo.c (limited to 'src/config/repo.c') diff --git a/src/config/repo.c b/src/config/repo.c new file mode 100644 index 0000000..a3ba390 --- /dev/null +++ b/src/config/repo.c @@ -0,0 +1,46 @@ +/* ISC license. */ + +#include +#include + +#include +#include + +#include "tipidee-config-internal.h" + +void *node_dtok (uint32_t d, void *data) +{ + repo *r = data ; + return r->storage->s + genalloc_s(node, &r->ga)[d].key ; +} + +int node_cmp (void const *a, void const *b, void *data) +{ + (void)data ; + return strcmp((char const *)a, (char const *)b) ; +} + +node const *repo_search (repo const *r, char const *key) +{ + uint32_t i ; + return avltree_search(&r->tree, key, &i) ? genalloc_s(node const, &r->ga) + i : 0 ; +} + +void repo_add (repo *r, node const *node) +{ + uint32_t i = genalloc_len(node, &r->ga) ; + if (!genalloc_append(node, &r->ga, node)) dienomem() ; + if (!avltree_insert(&r->tree, i)) dienomem() ; +} + +void repo_update (repo *r, node const *nod) +{ + uint32_t i ; + if (avltree_search(&r->tree, r->storage->s + nod->key, &i)) + { + if (!avltree_delete(&r->tree, r->storage->s + nod->key)) dienomem() ; + genalloc_s(node, &r->ga)[i] = *nod ; + if (!avltree_insert(&r->tree, i)) dienomem() ; + } + else repo_add(r, nod) ; +} -- cgit v1.3.1