cont
[protos/libecoli.git] / lib / ecoli_strvec.c
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <sys/types.h>
29 #include <stdlib.h>
30
31 #include <ecoli_malloc.h>
32 #include <ecoli_strvec.h>
33
34 struct ec_strvec *ec_strvec_new(void)
35 {
36         struct ec_strvec *strvec;
37
38         strvec = ec_calloc(1, sizeof(*strvec));
39         if (strvec == NULL)
40                 return NULL;
41
42         return strvec;
43 }
44
45 int ec_strvec_add(struct ec_strvec *strvec, const char *s)
46 {
47         char **new_vec;
48
49         new_vec = ec_realloc(strvec->vec,
50                 sizeof(*strvec->vec) * (strvec->len + 1));
51         if (new_vec == NULL)
52                 return -1;
53
54         strvec->vec = new_vec;
55         strvec->vec[strvec->len] = ec_strdup(s);
56         if (strvec->vec[strvec->len] == NULL)
57                 return -1;
58
59         strvec->len++;
60         return 0;
61 }
62
63 struct ec_strvec *ec_strvec_ndup(const struct ec_strvec *strvec, size_t len)
64 {
65         struct ec_strvec *copy = NULL;
66         size_t i, veclen;
67
68         copy = ec_strvec_new();
69         if (copy == NULL)
70                 goto fail;
71
72         if (len == 0)
73                 return copy;
74
75         veclen = ec_strvec_len(strvec);
76         if (len > veclen)
77                 len = veclen;
78         copy->vec = ec_calloc(len, sizeof(*copy->vec));
79         if (copy->vec == NULL)
80                 goto fail;
81
82         for (i = 0; i < len; i++) {
83                 copy->vec[i] = ec_strdup(strvec->vec[i]);
84                 if (copy->vec[i] == NULL)
85                         goto fail;
86                 copy->len++;
87         }
88
89         return copy;
90
91 fail:
92         ec_strvec_free(copy);
93         return NULL;
94 }
95
96 struct ec_strvec *ec_strvec_dup(const struct ec_strvec *strvec)
97 {
98         return ec_strvec_ndup(strvec, ec_strvec_len(strvec));
99 }
100
101 void ec_strvec_free(struct ec_strvec *strvec)
102 {
103         size_t i;
104
105         if (strvec == NULL)
106                 return;
107
108         for (i = 0; i < ec_strvec_len(strvec); i++)
109                 ec_free(ec_strvec_val(strvec, i));
110
111         ec_free(strvec->vec);
112         ec_free(strvec);
113 }
114
115 size_t ec_strvec_len(const struct ec_strvec *strvec)
116 {
117         return strvec->len;
118 }
119
120 char *ec_strvec_val(const struct ec_strvec *strvec, size_t idx)
121 {
122         if (strvec == NULL || idx >= strvec->len)
123                 return NULL;
124
125         return strvec->vec[idx];
126 }
127
128 int ec_strvec_slice(struct ec_strvec *strvec, const struct ec_strvec *from,
129         size_t off)
130 {
131         if (off > from->len)
132                 return -1;
133
134         strvec->len = from->len - off;
135         strvec->vec = &from->vec[off];
136
137         return 0;
138 }
139
140 void ec_strvec_dump(const struct ec_strvec *strvec, FILE *out)
141 {
142         size_t i;
143
144         if (strvec == NULL) {
145                 fprintf(out, "empty strvec\n");
146                 return;
147         }
148
149         fprintf(out, "strvec:\n");
150         for (i = 0; i < ec_strvec_len(strvec); i++)
151                 fprintf(out, "  %zd: %s\n", i, strvec->vec[i]);
152 }