From 4ac5832fff29d7d22919e3268780adefe5c2bc53 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 12 Apr 2024 09:17:02 -0400 Subject: Registering a variable more than once will replace the pointer associated with a key; not generate a new record --- src/template.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/template.c b/src/template.c index 6d2ffe9..14e40c2 100644 --- a/src/template.c +++ b/src/template.c @@ -27,17 +27,46 @@ void tpl_register_func(char *key, struct tplfunc_frame *frame) { tpl_pool_func_used++; } +int tpl_key_exists(char *key) { + for (size_t i = 0; i < tpl_pool_used; i++) { + if (tpl_pool[i]->key) { + if (!strcmp(tpl_pool[i]->key, key)) { + return true; + } + } + } + return false; +} + void tpl_register(char *key, char **ptr) { struct tpl_item *item = NULL; - item = calloc(1, sizeof(*item)); + int replacing = 0; + + if (tpl_key_exists(key)) { + for (size_t i = 0; i < tpl_pool_used; i++) { + if (tpl_pool[i]->key) { + if (!strcmp(tpl_pool[i]->key, key)) { + item = tpl_pool[i]; + break; + } + } + } + replacing = 1; + } else { + item = calloc(1, sizeof(*item)); + item->key = strdup(key); + } + if (!item) { SYSERROR("unable to register tpl_item for %s", key); exit(1); } - item->key = strdup(key); + item->ptr = ptr; - tpl_pool[tpl_pool_used] = item; - tpl_pool_used++; + if (!replacing) { + tpl_pool[tpl_pool_used] = item; + tpl_pool_used++; + } } void tpl_free() { -- cgit