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