author | vb |
Wed, 11 Mar 2015 15:54:19 +0100 | |
changeset 107 | fbef1c59da96 |
parent 98 | 9e3d28932e7b |
child 109 | 297efc53ba70 |
permissions | -rw-r--r-- |
vb@98 | 1 |
#include <stdlib.h> |
vb@98 | 2 |
#include <string.h> |
vb@98 | 3 |
#include <assert.h> |
vb@98 | 4 |
|
vb@98 | 5 |
#include "stringlist.h" |
vb@98 | 6 |
|
vb@98 | 7 |
|
vb@98 | 8 |
DYNAMIC_API stringlist_t *new_stringlist(const char *value) |
vb@98 | 9 |
{ |
vb@107 | 10 |
stringlist_t *result = calloc(1, sizeof(stringlist_t)); |
vb@98 | 11 |
if (result && value) { |
vb@98 | 12 |
result->value = strdup(value); |
vb@98 | 13 |
assert(result->value); |
vb@98 | 14 |
if (result->value == 0) { |
vb@98 | 15 |
free(result); |
vb@98 | 16 |
return NULL; |
vb@98 | 17 |
} |
vb@98 | 18 |
} |
vb@98 | 19 |
return result; |
vb@98 | 20 |
} |
vb@98 | 21 |
|
vb@98 | 22 |
DYNAMIC_API stringlist_t *stringlist_dup(const stringlist_t *src) |
vb@98 | 23 |
{ |
vb@98 | 24 |
assert(src); |
vb@98 | 25 |
if (src == NULL) |
vb@98 | 26 |
return NULL; |
vb@98 | 27 |
|
vb@98 | 28 |
stringlist_t *dst = new_stringlist(src->value); |
vb@98 | 29 |
if (dst == NULL) |
vb@98 | 30 |
return NULL; |
vb@98 | 31 |
|
vb@98 | 32 |
if (src->next) { |
vb@98 | 33 |
dst->next = stringlist_dup(src->next); |
vb@98 | 34 |
if (dst->next == NULL) { |
vb@98 | 35 |
free_stringlist(dst); |
vb@98 | 36 |
return NULL; |
vb@98 | 37 |
} |
vb@98 | 38 |
} |
vb@98 | 39 |
|
vb@98 | 40 |
return dst; |
vb@98 | 41 |
} |
vb@98 | 42 |
|
vb@98 | 43 |
DYNAMIC_API stringlist_t *stringlist_add(stringlist_t *stringlist, const char *value) |
vb@98 | 44 |
{ |
vb@98 | 45 |
assert(value); |
vb@98 | 46 |
|
vb@98 | 47 |
if (stringlist == NULL) |
vb@98 | 48 |
return new_stringlist(value); |
vb@98 | 49 |
|
vb@98 | 50 |
if (stringlist->next != NULL) |
vb@98 | 51 |
return stringlist_add(stringlist->next, value); |
vb@98 | 52 |
if (stringlist->value == NULL) { |
vb@98 | 53 |
stringlist->value = strdup(value); |
vb@98 | 54 |
assert(stringlist->value); |
vb@98 | 55 |
if (stringlist->value == NULL) |
vb@98 | 56 |
return NULL; |
vb@98 | 57 |
return stringlist; |
vb@98 | 58 |
} |
vb@98 | 59 |
|
vb@98 | 60 |
stringlist->next = new_stringlist(value); |
vb@98 | 61 |
assert(stringlist->next); |
vb@98 | 62 |
if (stringlist->next == NULL) |
vb@98 | 63 |
return NULL; |
vb@98 | 64 |
|
vb@98 | 65 |
return stringlist->next; |
vb@98 | 66 |
} |
vb@98 | 67 |
|
vb@98 | 68 |
DYNAMIC_API stringlist_t *stringlist_append(stringlist_t *stringlist, |
vb@98 | 69 |
stringlist_t *second) |
vb@98 | 70 |
{ |
vb@98 | 71 |
assert(stringlist); |
vb@98 | 72 |
|
vb@98 | 73 |
if (second == NULL || second->value == NULL) |
vb@98 | 74 |
return stringlist; |
vb@98 | 75 |
|
vb@98 | 76 |
stringlist_t *_s = stringlist; |
vb@98 | 77 |
stringlist_t *_s2; |
vb@98 | 78 |
for (_s2 = second; _s2 != NULL; _s2 = _s2->next) { |
vb@98 | 79 |
_s = stringlist_add(_s, _s2->value); |
vb@98 | 80 |
if (_s == NULL) |
vb@98 | 81 |
return NULL; |
vb@98 | 82 |
} |
vb@98 | 83 |
return _s; |
vb@98 | 84 |
} |
vb@98 | 85 |
|
vb@98 | 86 |
DYNAMIC_API int stringlist_length(const stringlist_t *stringlist) |
vb@98 | 87 |
{ |
vb@98 | 88 |
int len = 1; |
vb@98 | 89 |
stringlist_t *_stringlist; |
vb@98 | 90 |
|
vb@98 | 91 |
assert(stringlist); |
vb@98 | 92 |
|
vb@98 | 93 |
if (stringlist->value == NULL) |
vb@98 | 94 |
return 0; |
vb@98 | 95 |
|
vb@98 | 96 |
for (_stringlist=stringlist->next; _stringlist!=NULL; _stringlist=_stringlist->next) |
vb@98 | 97 |
len += 1; |
vb@98 | 98 |
|
vb@98 | 99 |
return len; |
vb@98 | 100 |
} |
vb@98 | 101 |
|
vb@98 | 102 |
DYNAMIC_API void free_stringlist(stringlist_t *stringlist) |
vb@98 | 103 |
{ |
vb@98 | 104 |
if (stringlist) { |
vb@98 | 105 |
free_stringlist(stringlist->next); |
vb@98 | 106 |
free(stringlist->value); |
vb@98 | 107 |
free(stringlist); |
vb@98 | 108 |
} |
vb@98 | 109 |
} |
vb@98 | 110 |