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>
31 #include <ecoli_malloc.h>
32 #include <ecoli_strvec.h>
34 struct ec_strvec *ec_strvec_new(void)
36 struct ec_strvec *strvec;
38 strvec = ec_calloc(1, sizeof(*strvec));
45 int ec_strvec_add(struct ec_strvec *strvec, const char *s)
49 new_vec = ec_realloc(strvec->vec,
50 sizeof(*strvec->vec) * (strvec->len + 1));
54 strvec->vec = new_vec;
55 strvec->vec[strvec->len] = ec_strdup(s);
56 if (strvec->vec[strvec->len] == NULL)
63 struct ec_strvec *ec_strvec_ndup(const struct ec_strvec *strvec, size_t len)
65 struct ec_strvec *copy = NULL;
68 copy = ec_strvec_new();
75 veclen = ec_strvec_len(strvec);
78 copy->vec = ec_calloc(len, sizeof(*copy->vec));
79 if (copy->vec == NULL)
82 for (i = 0; i < len; i++) {
83 copy->vec[i] = ec_strdup(strvec->vec[i]);
84 if (copy->vec[i] == NULL)
96 struct ec_strvec *ec_strvec_dup(const struct ec_strvec *strvec)
98 return ec_strvec_ndup(strvec, ec_strvec_len(strvec));
101 void ec_strvec_free(struct ec_strvec *strvec)
108 for (i = 0; i < ec_strvec_len(strvec); i++)
109 ec_free(ec_strvec_val(strvec, i));
111 ec_free(strvec->vec);
115 size_t ec_strvec_len(const struct ec_strvec *strvec)
120 char *ec_strvec_val(const struct ec_strvec *strvec, size_t idx)
122 if (strvec == NULL || idx >= strvec->len)
125 return strvec->vec[idx];
128 int ec_strvec_slice(struct ec_strvec *strvec, const struct ec_strvec *from,
134 strvec->len = from->len - off;
135 strvec->vec = &from->vec[off];
140 void ec_strvec_dump(const struct ec_strvec *strvec, FILE *out)
144 if (strvec == NULL) {
145 fprintf(out, "empty strvec\n");
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]);