diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/list.c | 29 | ||||
-rw-r--r-- | src/utils/list.h | 10 |
2 files changed, 28 insertions, 11 deletions
diff --git a/src/utils/list.c b/src/utils/list.c index c5152a3..030c759 100644 --- a/src/utils/list.c +++ b/src/utils/list.c @@ -25,14 +25,31 @@ void destroy_list(struct list* l) struct list* save; while (l != NULL) { save = l->next; + free(l->key); free(l); l = save; } } -void add_elt(struct list** l, const char* key, json_object* value) +void add_elt(struct list** l, const char* key, const char* suffix, json_object* value) { - struct list* new_elt; + struct list* new_elt = NULL; + char *suffixed_key = NULL; + size_t key_len = strlen(key), suffix_len = -1; + + if (suffix == NULL) { + suffix = ""; + suffix_len=0; + } else { + suffix_len = strlen(suffix); + } + suffixed_key = malloc(key_len + suffix_len + 1); + if (!suffixed_key) { + fprintf(stderr, "list.c, add_elt() : can't allocate 'suffixed_key' (out of memory)"); + exit(1); + } + strncpy(suffixed_key, key, key_len); + strncpy(suffixed_key+key_len, suffix, suffix_len+1); // search tail new_elt = *l; @@ -44,19 +61,19 @@ void add_elt(struct list** l, const char* key, json_object* value) // alloc new elem new_elt = malloc(sizeof(struct list)); if (!new_elt) { - fprintf(stderr, "OUT of memory"); + fprintf(stderr, "list.c, add_elt() : can't allocate 'new_elt' (out of memory)"); exit(1); } *l = new_elt; - new_elt->key = key; + new_elt->key = suffixed_key; new_elt->value = value; new_elt->next = NULL; } -void add_key(struct list** l, const char* key) +void add_key(struct list** l, const char* key, const char* suffix) { - add_elt(l, key, NULL); + add_elt(l, key, suffix, NULL); } int set_value(struct list* l, json_object* val, int index) diff --git a/src/utils/list.h b/src/utils/list.h index 9f93cb6..c0d559f 100644 --- a/src/utils/list.h +++ b/src/utils/list.h @@ -21,17 +21,17 @@ #include <json-c/json.h> struct list { - const char *key; + char *key; json_object *value; struct list *next; }; void destroy_list(struct list *l); -void add_elt(struct list **l, const char *key, json_object *value); -void add_key(struct list **l, const char *key); +void add_elt(struct list **l, const char *key, const char* suffix, json_object *value); +void add_key(struct list **l, const char *key, const char* suffix); int set_value(struct list *l, json_object *val, int index); struct list *get_elt(struct list *l, int index); -struct list *find_elt_from_key(struct list *l, const char *key); -json_object *find_key_value(struct list *l, const char *key); +struct list *find_elt_from_key(struct list *l, const char *suffixed_key); +json_object *find_key_value(struct list *l, const char *suffixed_key); #endif |