2 * Copyright (c) 2008 Hyogeol Lee <hyogeollee@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/types.h>
34 #include "_libelftc.h"
36 ELFTC_VCSID("$Id: libelftc_vstr.c 2065 2011-10-26 15:24:47Z jkoshy $");
40 * @brief Dynamic vector data for string implementation.
42 * Resemble to std::vector<std::string> in C++.
45 static size_t get_strlen_sum(const struct vector_str *v);
46 static bool vector_str_grow(struct vector_str *v);
49 get_strlen_sum(const struct vector_str *v)
58 for (i = 0; i < v->size; ++i)
59 len += strlen(v->container[i]);
65 * @brief Deallocate resource in vector_str.
68 vector_str_dest(struct vector_str *v)
75 for (i = 0; i < v->size; ++i)
76 free(v->container[i]);
82 * @brief Find string in vector_str.
83 * @param v Destination vector.
84 * @param o String to find.
85 * @param l Length of the string.
86 * @return -1 at failed, 0 at not found, 1 at found.
89 vector_str_find(const struct vector_str *v, const char *o, size_t l)
93 if (v == NULL || o == NULL)
96 for (i = 0; i < v->size; ++i)
97 if (strncmp(v->container[i], o, l) == 0)
104 * @brief Get new allocated flat string from vector.
106 * If l is not NULL, return length of the string.
107 * @param v Destination vector.
108 * @param l Length of the string.
109 * @return NULL at failed or NUL terminated new allocated string.
112 vector_str_get_flat(const struct vector_str *v, size_t *l)
114 ssize_t elem_pos, elem_size, rtn_size;
118 if (v == NULL || v->size == 0)
121 if ((rtn_size = get_strlen_sum(v)) == 0)
124 if ((rtn = malloc(sizeof(char) * (rtn_size + 1))) == NULL)
128 for (i = 0; i < v->size; ++i) {
129 elem_size = strlen(v->container[i]);
131 memcpy(rtn + elem_pos, v->container[i], elem_size);
133 elem_pos += elem_size;
136 rtn[rtn_size] = '\0';
145 vector_str_grow(struct vector_str *v)
153 assert(v->capacity > 0);
155 tmp_cap = v->capacity * BUFFER_GROWFACTOR;
157 assert(tmp_cap > v->capacity);
159 if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL)
162 for (i = 0; i < v->size; ++i)
163 tmp_ctn[i] = v->container[i];
167 v->container = tmp_ctn;
168 v->capacity = tmp_cap;
174 * @brief Initialize vector_str.
175 * @return false at failed, true at success.
178 vector_str_init(struct vector_str *v)
185 v->capacity = VECTOR_DEF_CAPACITY;
187 assert(v->capacity > 0);
189 if ((v->container = malloc(sizeof(char *) * v->capacity)) == NULL)
192 assert(v->container != NULL);
198 * @brief Remove last element in vector_str.
199 * @return false at failed, true at success.
202 vector_str_pop(struct vector_str *v)
213 free(v->container[v->size]);
214 v->container[v->size] = NULL;
220 * @brief Push back string to vector.
221 * @return false at failed, true at success.
224 vector_str_push(struct vector_str *v, const char *str, size_t len)
227 if (v == NULL || str == NULL)
230 if (v->size == v->capacity && vector_str_grow(v) == false)
233 if ((v->container[v->size] = malloc(sizeof(char) * (len + 1))) == NULL)
236 snprintf(v->container[v->size], len + 1, "%s", str);
244 * @brief Push front org vector to det vector.
245 * @return false at failed, true at success.
248 vector_str_push_vector_head(struct vector_str *dst, struct vector_str *org)
250 size_t i, j, tmp_cap;
253 if (dst == NULL || org == NULL)
256 tmp_cap = (dst->size + org->size) * BUFFER_GROWFACTOR;
258 if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL)
261 for (i = 0; i < org->size; ++i)
262 if ((tmp_ctn[i] = strdup(org->container[i])) == NULL) {
263 for (j = 0; j < i; ++j)
271 for (i = 0; i < dst->size; ++i)
272 tmp_ctn[i + org->size] = dst->container[i];
274 free(dst->container);
276 dst->container = tmp_ctn;
277 dst->capacity = tmp_cap;
278 dst->size += org->size;
284 * @brief Get new allocated flat string from vector between begin and end.
286 * If r_len is not NULL, string length will be returned.
287 * @return NULL at failed or NUL terminated new allocated string.
290 vector_str_substr(const struct vector_str *v, size_t begin, size_t end,
296 if (v == NULL || begin > end)
300 for (i = begin; i < end + 1; ++i)
301 len += strlen(v->container[i]);
303 if ((rtn = malloc(sizeof(char) * (len + 1))) == NULL)
310 for (i = begin; i < end + 1; ++i) {
311 len = strlen(v->container[i]);
312 memcpy(rtn + cur, v->container[i], len);