From 5bb20bdb9e4a1079b4fa45e1e78ccb985efda130 Mon Sep 17 00:00:00 2001 From: carl Date: Sat, 11 May 2019 00:52:40 +0800 Subject: [PATCH] Add init/free functions and rename parameters. --- modules/src/data/smap.c | 55 ++++++++++++++++++++++++++--------- modules/src/data/smap.h | 37 +++++++++++++++++++---- modules/src/data/stringlist.c | 50 ++++++++++++++++++++++++++++++- modules/src/data/stringlist.h | 6 +++- 4 files changed, 128 insertions(+), 20 deletions(-) diff --git a/modules/src/data/smap.c b/modules/src/data/smap.c index 312241a20..45e25ebbf 100644 --- a/modules/src/data/smap.c +++ b/modules/src/data/smap.c @@ -3,14 +3,15 @@ #include #include "smap.h" -static void append(void* mapp, const char* left, void* right) +#define INCR_SIZE 8 + +static void append(void* mapp, char* key, void* value) { struct smap* map = mapp; struct smap_node* node; - if (map->count == map->max) { - int newmax = (map->max == 0) ? 8 : (map->max * 2); + int newmax = (map->max == 0) ? INCR_SIZE : (map->max * 2); struct smap_node* newarray = realloc(map->item, newmax * sizeof(*newarray)); map->max = newmax; @@ -20,11 +21,12 @@ static void append(void* mapp, const char* left, void* right) node = &map->item[map->count]; map->count++; - node->left = left; - node->right = right; + node->left = key; + node->right = value; } -void smap_put(void* mapp, const char* left, void* right) + +void smap_put(struct smap *mapp, char* key, void* value) { struct smap* map = mapp; int i; @@ -32,17 +34,44 @@ void smap_put(void* mapp, const char* left, void* right) for (i=0; icount; i++) { struct smap_node* node = &map->item[i]; - if (strcmp(node->left, left) == 0) + if (strcmp(node->left, key) == 0) { - node->right = right; + node->right = value; return; } } - append(map, left, right); + append(map, key, value); } -void smap_add(void* mapp, const char* left, void* right) +void smap_init(struct smap *mapp) +{ + mapp->count = 0; + mapp->item = NULL; + mapp->max = 0; +} + + +void smap_free(struct smap *mapp, int free_key, int free_value) +{ + int i; + for (i=0; icount; i++) + { + struct smap_node* node = &mapp->item[i]; + if (free_key) + { + free(node->left); + } + if (free_value) + { + free(node->right); + } + } + mapp->count = 0; + free(mapp->item); +} + +void smap_add(struct smap *mapp, char* key, void* value) { struct smap* map = mapp; int i; @@ -50,14 +79,14 @@ void smap_add(void* mapp, const char* left, void* right) for (i=0; icount; i++) { struct smap_node* node = &map->item[i]; - if ((strcmp(node->left, left) == 0) && (node->right == right)) + if ((strcmp(node->left, key) == 0) && (node->right == value)) return; } - append(map, left, right); + append(map, key, value); } -void* smap_get(void* mapp, const char* left) +void* smap_get(struct smap *mapp, const char* left) { struct smap* map = mapp; int i; diff --git a/modules/src/data/smap.h b/modules/src/data/smap.h index 7ccdb51de..f01b38010 100644 --- a/modules/src/data/smap.h +++ b/modules/src/data/smap.h @@ -1,9 +1,14 @@ +/** @file + * String map implementation. + * + */ + #ifndef SMAP_H #define SMAP_H struct smap_node { - const char* left; + char* left; void* right; }; @@ -18,14 +23,36 @@ struct smap #define SMAPOF(RIGHT) \ struct { \ - struct { const char* left; RIGHT* right; }* item; \ + struct { char* left; RIGHT* right; }* item; \ int count; \ int max; \ } -extern void smap_put(void* map, const char* left, void* right); -extern void smap_add(void* map, const char* left, void* right); -extern void* smap_get(void* map, const char* left); +/** Initializes a string map and returns the initialized + * handle in `mapp`. + */ +extern void smap_init(struct smap *mapp); +/** Adds a new item with name `key` in the string map if + * it does not already exist, otherwise replaces the + * value `value` associated with the existing `key`. + */ +extern void smap_put(struct smap *mapp, char* key, void* value); +/** Adds a new item in a string map only if `key` does + * not already exist in the string map. + */ +extern void smap_add(struct smap *mapp, char* key, void* value); +/** Returns the value associated with the specified `key`, returns + * NULL if `key` is not present in the string map. + * + */ +extern void* smap_get(struct smap* mapp, const char* key); +/** Frees the data structure associated with the string map. + * Also frees the memory associated with the key if + * `free_key` is non-zero, and free the memory associated + * with the value if `free_value` is non-zero. + * + */ +extern void smap_free(struct smap *mapp, int free_key, int free_value); #endif diff --git a/modules/src/data/stringlist.c b/modules/src/data/stringlist.c index 28322bf98..74d73b331 100644 --- a/modules/src/data/stringlist.c +++ b/modules/src/data/stringlist.c @@ -1,3 +1,7 @@ +/** @file + * String list implementation. + * + */ #include #include "stringlist.h" @@ -26,18 +30,62 @@ void stringlist_addall(struct stringlist* list, struct stringlist* src) } } -void stringlist_free(struct stringlist* list) +void stringlist_free(struct stringlist* list, int freedata) { struct stringfragment* f = list->first; while (f) { struct stringfragment* next = f->next; + if (freedata) + { + free(f->data); + f->data = NULL; + } free(f); f = next; } } +void stringlist_init(struct stringlist* list) +{ + list->first = NULL; + list->last = NULL; +} + + +char* stringlist_get(struct stringlist *list, int index) +{ + struct stringfragment* f = list->first; + int i = 0; + + while (f) + { + if (i == index) + { + return f->data; + } + f = f->next; + i++; + + } + return i; + +} + +int stringlist_count(struct stringlist *list) +{ + struct stringfragment* f = list->first; + int i = 0; + + while (f) + { + f = f->next; + i++; + } + return i; +} + /* vim: set sw=4 ts=4 expandtab : */ diff --git a/modules/src/data/stringlist.h b/modules/src/data/stringlist.h index 493d7ff1e..3d9b0c4c1 100644 --- a/modules/src/data/stringlist.h +++ b/modules/src/data/stringlist.h @@ -15,7 +15,11 @@ struct stringlist extern void stringlist_add(struct stringlist* list, const char* data); extern void stringlist_addall(struct stringlist* list, struct stringlist* src); -extern void stringlist_free(struct stringlist* list); +extern void stringlist_free(struct stringlist* list, int freedata); +extern void stringlist_init(struct stringlist* list); +extern int stringlist_count(struct stringlist *list); +extern char* stringlist_get(struct stringlist *list, int index); + #endif