diff options
author | Christian Pointner <equinox@spreadspace.org> | 2010-11-26 16:11:46 +0000 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2010-11-26 16:11:46 +0000 |
commit | 668f72e224aac1e0dbecf4fa3baba567016f0f22 (patch) | |
tree | 5c732ea5c73cb74ef5568b529df24c0555b6ef4c /src/string_list.c | |
parent | moved version.h to config.h with additional info (diff) |
added generic single linked list
git-svn-id: https://svn.spreadspace.org/tcpproxy/trunk@6 e61f0598-a718-4e21-a8f0-0aadfa62ad6b
Diffstat (limited to 'src/string_list.c')
-rw-r--r-- | src/string_list.c | 57 |
1 files changed, 9 insertions, 48 deletions
diff --git a/src/string_list.c b/src/string_list.c index 3c7316d..192ff19 100644 --- a/src/string_list.c +++ b/src/string_list.c @@ -30,65 +30,26 @@ #include <stdio.h> #include "string_list.h" +#include "slist.h" -void string_list_init(string_list_t* list) +int string_list_init(string_list_t* list) { - if(!list) - return; - - list->first_ = NULL; + return slist_init(list, &free); } void string_list_clear(string_list_t* list) { - if(!list) - return; - - while(list->first_) { - string_list_element_t* tmp; - tmp = list->first_; - list->first_ = tmp->next_; - if(tmp->string_) - free(tmp->string_); - free(tmp); - } + slist_clear(list); } int string_list_add(string_list_t* list, const char* string) { if(!list) return -1; + + if(slist_add(list, strdup(string)) == NULL) + return -2; - if(!list->first_) { - list->first_ = malloc(sizeof(string_list_element_t)); - if(!list->first_) - return -2; - - list->first_->next_ = 0; - list->first_->string_ = strdup(string); - if(!list->first_->string_) { - free(list->first_); - list->first_ = NULL; - return -2; - } - } - else { - string_list_element_t* tmp = list->first_; - while(tmp->next_) - tmp = tmp->next_; - - tmp->next_ = malloc(sizeof(string_list_element_t)); - if(!tmp->next_) - return -2; - - tmp->next_->next_ = 0; - tmp->next_->string_ = strdup(string); - if(!tmp->next_->string_) { - free(tmp->next_); - tmp->next_ = NULL; - return -2; - } - } return 0; } @@ -97,9 +58,9 @@ void string_list_print(string_list_t* list, const char* head, const char* tail) if(!list) return; - string_list_element_t* tmp = list->first_; + slist_element_t* tmp = list->first_; while(tmp) { - printf("%s%s%s", head, tmp->string_, tail); + printf("%s%s%s", head, (char*)(tmp->data_), tail); tmp = tmp->next_; } } |