From 30fb06bce3c76c7f37d8649aa33927f38ed194a9 Mon Sep 17 00:00:00 2001 From: mescalinum Date: Fri, 28 Oct 2011 19:25:27 +0000 Subject: add open menu svn path=/trunk/externals/loaders/tclpd/; revision=15671 --- hashtable.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'hashtable.c') diff --git a/hashtable.c b/hashtable.c index 0b4409f..c2fa96f 100644 --- a/hashtable.c +++ b/hashtable.c @@ -15,19 +15,27 @@ uint32_t hash_str(const char *s) { list_node_t* list_add(list_node_t* head, const char* k, void* v) { list_node_t* n = (list_node_t*)malloc(sizeof(list_node_t)); n->next = head; +#ifdef HASHTABLE_COPY_KEYS n->k = strdup(k); +#else + n->k = k; +#endif n->v = v; return n; } list_node_t* list_remove(list_node_t* head, const char* k) { + if(!head) return NULL; + list_node_t* tmp; // head remove while(head && strcmp(head->k, k) == 0) { tmp = head; head = head->next; +#ifdef HASHTABLE_COPY_KEYS free(tmp->k); +#endif free(tmp); } @@ -39,6 +47,9 @@ list_node_t* list_remove(list_node_t* head, const char* k) { { tmp = p->next; p->next = p->next->next; +#ifdef HASHTABLE_COPY_KEYS + free(tmp->k); +#endif free(tmp); continue; } @@ -48,14 +59,14 @@ list_node_t* list_remove(list_node_t* head, const char* k) { return head; } -void* list_get(list_node_t* head, const char* k) { +list_node_t* list_get(list_node_t* head, const char* k) { while(head) { if(strcmp(head->k, k) == 0) { - return head->v; + return head; } head = head->next; } - return (void*)0; + return NULL; } size_t list_length(list_node_t* head) { -- cgit v1.2.1