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