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