2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include <sys/types.h>
33 #include <ecoli_malloc.h>
34 #include <ecoli_strvec.h>
36 struct ec_strvec_elt {
43 struct ec_strvec_elt **vec;
46 struct ec_strvec *ec_strvec(void)
48 struct ec_strvec *strvec;
50 strvec = ec_calloc(1, sizeof(*strvec));
57 int ec_strvec_add(struct ec_strvec *strvec, const char *s)
59 struct ec_strvec_elt *elt, **new_vec;
61 new_vec = ec_realloc(strvec->vec,
62 sizeof(*strvec->vec) * (strvec->len + 1));
66 strvec->vec = new_vec;
68 elt = ec_malloc(sizeof(*elt));
72 elt->str = ec_strdup(s);
73 if (elt->str == NULL) {
79 new_vec[strvec->len] = elt;
84 struct ec_strvec *ec_strvec_ndup(const struct ec_strvec *strvec, size_t off,
87 struct ec_strvec *copy = NULL;
94 veclen = ec_strvec_len(strvec);
97 else if (off + len > veclen)
98 len -= (veclen - off);
103 copy->vec = ec_calloc(len, sizeof(*copy->vec));
104 if (copy->vec == NULL)
107 for (i = 0; i < len; i++) {
108 copy->vec[i] = strvec->vec[i + off];
109 copy->vec[i]->refcnt++;
116 ec_strvec_free(copy);
120 struct ec_strvec *ec_strvec_dup(const struct ec_strvec *strvec)
122 return ec_strvec_ndup(strvec, 0, ec_strvec_len(strvec));
125 void ec_strvec_free(struct ec_strvec *strvec)
127 struct ec_strvec_elt *elt;
133 for (i = 0; i < ec_strvec_len(strvec); i++) {
134 elt = strvec->vec[i];
136 if (elt->refcnt == 0) {
142 ec_free(strvec->vec);
146 size_t ec_strvec_len(const struct ec_strvec *strvec)
151 char *ec_strvec_val(const struct ec_strvec *strvec, size_t idx)
153 if (strvec == NULL || idx >= strvec->len)
156 return strvec->vec[idx]->str;
159 void ec_strvec_dump(FILE *out, const struct ec_strvec *strvec)
163 if (strvec == NULL) {
164 fprintf(out, "empty strvec\n");
168 fprintf(out, "strvec:\n");
169 for (i = 0; i < ec_strvec_len(strvec); i++)
170 fprintf(out, " %zd: %s (refcnt=%d)\n", i,
171 strvec->vec[i]->str, strvec->vec[i]->refcnt);