summaryrefslogtreecommitdiff
path: root/strv.c
blob: 7e0810bffe48ddb8b71e82b36d8498d7ec8fbf14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*-*- Mode: C; c-basic-offset: 8 -*-*/

#include <assert.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#include "util.h"
#include "strv.h"

char *strv_find(char **l, const char *name) {
        assert(l);
        assert(name);

        for (; *l; l++)
                if (streq(*l, name))
                        return *l;

        return NULL;
}

void strv_free(char **l) {
        char **k;

        if (!l)
                return;

        for (k = l; *k; k++)
                free(*k);

        free(l);
}

char **strv_copy(char **l) {
        char **r, **k;

        if (!(r = new(char*, strv_length(l)+1)))
                return NULL;

        for (k = r; *l; k++, l++)
                if (!(*k = strdup(*l)))
                        goto fail;

        *k = NULL;
        return r;

fail:
        for (k--, l--; k >= r; k--, l--)
                free(*k);

        return NULL;
}

unsigned strv_length(char **l) {
        unsigned n = 0;

        if (!l)
                return 0;

        for (; *l; l++)
                n++;

        return n;
}

char **strv_new(const char *x, ...) {
        const char *s;
        char **a;
        unsigned n = 0, i = 0;
        va_list ap;

        if (x) {
                n = 1;

                va_start(ap, x);

                while (va_arg(ap, const char*))
                        n++;

                va_end(ap);
        }

        if (!(a = new(char*, n+1)))
                return NULL;

        if (x) {
                if (!(a[i] = strdup(x))) {
                        free(a);
                        return NULL;
                }

                i++;

                va_start(ap, x);

                while ((s = va_arg(ap, const char*))) {
                        if (!(a[i] = strdup(s)))
                                goto fail;

                        i++;
                }

                va_end(ap);
        }

        a[i] = NULL;
        return a;

fail:

        for (; i > 0; i--)
                if (a[i-1])
                        free(a[i-1]);

        free(a);
        return NULL;
}

char **strv_merge(char **a, char **b) {
        char **r, **k;

        if (!a)
                return strv_copy(b);

        if (!b)
                return strv_copy(a);

        if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
                return NULL;

        for (k = r; *a; k++, a++)
                if (!(*k = strdup(*a)))
                        goto fail;
        for (; *b; k++, b++)
                if (!(*k = strdup(*b)))
                        goto fail;

        *k = NULL;
        return r;

fail:
        for (k--; k >= r; k--)
                free(*k);

        return NULL;

}

bool strv_contains(char **l, const char *s) {
        char **i;

        STRV_FOREACH(i, l)
                if (streq(*i, s))
                        return true;

        return false;
}