diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-04-12 09:17:02 -0400 | 
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-04-12 09:17:02 -0400 | 
| commit | 4ac5832fff29d7d22919e3268780adefe5c2bc53 (patch) | |
| tree | ea451eee936246aeddc8c3d400a0d77ee49b4c66 /src | |
| parent | 35ffcc51dd4ba254793505b5eebbdba8fd7aefc8 (diff) | |
| download | stasis-4ac5832fff29d7d22919e3268780adefe5c2bc53.tar.gz | |
Registering a variable more than once will replace the pointer associated with a key; not generate a new record
Diffstat (limited to 'src')
| -rw-r--r-- | src/template.c | 37 | 
1 files changed, 33 insertions, 4 deletions
| 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() { | 
