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.
31 * The ec_strvec API provide helpers to manipulate string vectors.
32 * When duplicating vectors, the strings are not duplicated in memory,
33 * a reference counter is used.
42 * Allocate a new empty string vector.
45 * The new strvec object, or NULL on error (errno is set).
47 struct ec_strvec *ec_strvec(void);
50 #define EC_COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
51 ((size_t)(!(sizeof(x) % sizeof(0[x])))))
55 * Allocate a new string vector
57 * The string vector is initialized with the list of const strings
58 * passed as arguments.
61 * The new strvec object, or NULL on error (errno is set).
63 #define EC_STRVEC(args...) ({ \
64 const char *_arr[] = {args}; \
65 ec_strvec_from_array(_arr, EC_COUNT_OF(_arr)); \
68 * Allocate a new string vector
70 * The string vector is initialized with the array of const strings
71 * passed as arguments.
74 * The array of const strings.
76 * The number of strings in the array.
78 * The new strvec object, or NULL on error (errno is set).
80 struct ec_strvec *ec_strvec_from_array(const char * const *strarr,
84 * Add a string in a vector.
87 * The pointer to the string vector.
89 * The string to be added at the end of the vector.
91 * 0 on success or -1 on error (errno is set).
93 int ec_strvec_add(struct ec_strvec *strvec, const char *s);
96 * Delete the last entry in the string vector.
99 * The pointer to the string vector.
101 * The string to be added at the end of the vector.
103 * 0 on success or -1 on error (errno is set).
105 int ec_strvec_del_last(struct ec_strvec *strvec);
108 * Duplicate a string vector.
111 * The pointer to the string vector.
113 * The duplicated strvec object, or NULL on error (errno is set).
115 struct ec_strvec *ec_strvec_dup(const struct ec_strvec *strvec);
118 * Duplicate a part of a string vector.
121 * The pointer to the string vector.
123 * The index of the first string to duplicate.
125 * The number of strings to duplicate.
127 * The duplicated strvec object, or NULL on error (errno is set).
129 struct ec_strvec *ec_strvec_ndup(const struct ec_strvec *strvec,
130 size_t off, size_t len);
133 * Free a string vector.
136 * The pointer to the string vector.
138 void ec_strvec_free(struct ec_strvec *strvec);
141 * Get the length of a string vector.
144 * The pointer to the string vector.
146 * The length of the vector.
148 size_t ec_strvec_len(const struct ec_strvec *strvec);
151 * Get a string element from a vector.
154 * The pointer to the string vector.
156 * The index of the string to get.
158 * The string stored at given index, or NULL on error (strvec is NULL
161 const char *ec_strvec_val(const struct ec_strvec *strvec, size_t idx);
164 * Compare two string vectors
167 * The pointer to the first string vector.
169 * The pointer to the second string vector.
171 * 0 if the string vectors are equal.
173 int ec_strvec_cmp(const struct ec_strvec *strvec1,
174 const struct ec_strvec *strvec2);
177 * Dump a string vector.
180 * The stream where the dump is sent.
182 * The pointer to the string vector.
184 void ec_strvec_dump(FILE *out, const struct ec_strvec *strvec);