src/stringlist.c
author Volker Birk <vb@pep-project.org>
Thu, 31 Mar 2016 14:01:45 +0200
changeset 523 afaf34a57cc6
parent 301 d8b44dc4b0c8
child 825 c5b5500a978d
permissions -rw-r--r--
delete from stringlist
     1 #include "pEp_internal.h"
     2 
     3 #include <stdlib.h>
     4 #include <string.h>
     5 #include <assert.h>
     6 
     7 #include "stringlist.h"
     8 
     9 
    10 DYNAMIC_API stringlist_t *new_stringlist(const char *value)
    11 {
    12     stringlist_t *result = calloc(1, sizeof(stringlist_t));
    13     assert(result);
    14 
    15     if (result && value) {
    16         result->value = strdup(value);
    17         assert(result->value);
    18         if (result->value == 0) {
    19             free(result);
    20             return NULL;
    21         }
    22     }
    23 
    24     return result;
    25 }
    26 
    27 DYNAMIC_API stringlist_t *stringlist_dup(const stringlist_t *src)
    28 {
    29     assert(src);
    30     if (src == NULL)
    31         return NULL;
    32 
    33     stringlist_t *dst = new_stringlist(src->value);
    34     if (dst == NULL)
    35         return NULL;
    36 
    37     if (src->next) {
    38         dst->next = stringlist_dup(src->next);
    39         if (dst->next == NULL) {
    40             free_stringlist(dst);
    41             return NULL;
    42         }
    43     }
    44 
    45     return dst;
    46 }
    47 
    48 DYNAMIC_API stringlist_t *stringlist_add(
    49         stringlist_t *stringlist,
    50         const char *value
    51     )
    52 {
    53     assert(value);
    54 
    55     if (stringlist == NULL)
    56         return new_stringlist(value);
    57 
    58     if (stringlist->next != NULL)
    59         return stringlist_add(stringlist->next, value);
    60     if (stringlist->value == NULL) {
    61         stringlist->value = strdup(value);
    62         assert(stringlist->value);
    63         if (stringlist->value == NULL)
    64             return NULL;
    65         return stringlist;
    66     }
    67 
    68     stringlist->next = new_stringlist(value);
    69     assert(stringlist->next);
    70     if (stringlist->next == NULL)
    71         return NULL;
    72 
    73     return stringlist->next;
    74 }
    75 
    76 DYNAMIC_API stringlist_t *stringlist_append(
    77         stringlist_t *stringlist,
    78         stringlist_t *second
    79     )
    80 {
    81     assert(stringlist);
    82 
    83     if (second == NULL || second->value == NULL)
    84         return stringlist;
    85 
    86     stringlist_t *_s = stringlist;
    87     stringlist_t *_s2;
    88     for (_s2 = second; _s2 != NULL; _s2 = _s2->next) {
    89         _s = stringlist_add(_s, _s2->value);
    90         if (_s == NULL)
    91             return NULL;
    92     }
    93     return _s;
    94 }
    95 
    96 DYNAMIC_API int stringlist_length(const stringlist_t *stringlist)
    97 {
    98     int len = 0;
    99 
   100     const stringlist_t *_sl;
   101     for (_sl = stringlist; _sl && _sl->value; _sl = _sl->next)
   102         len++;
   103 
   104     return len;
   105 }
   106 
   107 DYNAMIC_API stringlist_t *stringlist_delete(
   108         stringlist_t *stringlist,
   109         const char *value
   110     )
   111 {
   112     assert(stringlist);
   113     assert(value);
   114 
   115     if (stringlist->value == NULL) {
   116         free_stringlist(stringlist);
   117         return NULL;
   118     }
   119 
   120     if (value == NULL)
   121         return stringlist;
   122 
   123     stringlist_t *_sl;
   124     stringlist_t *last = NULL;
   125     for (_sl = stringlist; _sl && _sl->value; _sl = _sl->next) {
   126         if (strcmp(_sl->value, value) == 0) {
   127             if (last == NULL)
   128                 stringlist = stringlist->next;
   129             else
   130                 last->next = _sl->next;
   131             _sl->next = NULL;
   132             free_stringlist(_sl);
   133             break;
   134         }
   135         last = _sl;
   136     }
   137     return stringlist;
   138 }
   139 
   140 DYNAMIC_API void free_stringlist(stringlist_t *stringlist)
   141 {
   142     if (stringlist) {
   143         free_stringlist(stringlist->next);
   144         free(stringlist->value);
   145         free(stringlist);
   146     }
   147 }
   148