aboutsummaryrefslogtreecommitdiffstats
path: root/src/config/repo.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-10-23 10:31:31 +0000
committerLaurent Bercot <ska@appnovation.com>2023-10-23 10:31:31 +0000
commit12891d0e8551e3d6bb7bf1429f936e04be4da8b5 (patch)
tree7e4ac4bb5dc895b1b7604c8c9f76cca470964162 /src/config/repo.c
parent941ff50d2183999d8f1cbd249b6a892f70091ded (diff)
downloadtipidee-12891d0e8551e3d6bb7bf1429f936e04be4da8b5.tar.gz
Refactor tipidee-config code to accommodate for headers
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/config/repo.c')
-rw-r--r--src/config/repo.c46
1 files changed, 46 insertions, 0 deletions
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 <stdint.h>
+#include <string.h>
+
+#include <skalibs/genalloc.h>
+#include <skalibs/avltree.h>
+
+#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) ;
+}