1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
6 * @defgroup strvec String vectors
9 * @brief Helpers for strings vectors manipulation.
11 * The ec_strvec API provide helpers to manipulate string vectors.
12 * When duplicating vectors, the strings are not duplicated in memory,
13 * a reference counter is used.
22 * Allocate a new empty string vector.
25 * The new strvec object, or NULL on error (errno is set).
27 struct ec_strvec *ec_strvec(void);
30 #define EC_COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
31 ((size_t)(!(sizeof(x) % sizeof(0[x])))))
35 * Allocate a new string vector
37 * The string vector is initialized with the list of const strings
38 * passed as arguments.
41 * The new strvec object, or NULL on error (errno is set).
43 #define EC_STRVEC(args...) ({ \
44 const char *_arr[] = {args}; \
45 ec_strvec_from_array(_arr, EC_COUNT_OF(_arr)); \
48 * Allocate a new string vector
50 * The string vector is initialized with the array of const strings
51 * passed as arguments.
54 * The array of const strings.
56 * The number of strings in the array.
58 * The new strvec object, or NULL on error (errno is set).
60 struct ec_strvec *ec_strvec_from_array(const char * const *strarr,
64 * Set a string in the vector at specified index.
67 * The pointer to the string vector.
69 * The index of the string to set.
71 * The string to be set.
73 * 0 on success or -1 on error (errno is set).
75 int ec_strvec_set(struct ec_strvec *strvec, size_t idx, const char *s);
78 * Add a string in a vector.
81 * The pointer to the string vector.
83 * The string to be added at the end of the vector.
85 * 0 on success or -1 on error (errno is set).
87 int ec_strvec_add(struct ec_strvec *strvec, const char *s);
90 * Delete the last entry in the string vector.
93 * The pointer to the string vector.
95 * The string to be added at the end of the vector.
97 * 0 on success or -1 on error (errno is set).
99 int ec_strvec_del_last(struct ec_strvec *strvec);
102 * Duplicate a string vector.
104 * Attributes are duplicated if any.
107 * The pointer to the string vector.
109 * The duplicated strvec object, or NULL on error (errno is set).
111 struct ec_strvec *ec_strvec_dup(const struct ec_strvec *strvec);
114 * Duplicate a part of a string vector.
116 * Attributes are duplicated if any.
119 * The pointer to the string vector.
121 * The index of the first string to duplicate.
123 * The number of strings to duplicate.
125 * The duplicated strvec object, or NULL on error (errno is set).
127 struct ec_strvec *ec_strvec_ndup(const struct ec_strvec *strvec,
128 size_t off, size_t len);
131 * Free a string vector.
134 * The pointer to the string vector.
136 void ec_strvec_free(struct ec_strvec *strvec);
139 * Get the length of a string vector.
142 * The pointer to the string vector.
144 * The length of the vector.
146 size_t ec_strvec_len(const struct ec_strvec *strvec);
149 * Get a string element from a vector.
152 * The pointer to the string vector.
154 * The index of the string to get.
156 * The string stored at given index, or NULL on error (strvec is NULL
159 const char *ec_strvec_val(const struct ec_strvec *strvec, size_t idx);
162 * Get the attributes of a vector element.
165 * The pointer to the string vector.
167 * The index of the string to get.
169 * The read-only attributes (dictionnary) of the string at specified
170 * index, or NULL if there is no attribute.
172 const struct ec_dict *ec_strvec_get_attrs(const struct ec_strvec *strvec,
176 * Set the attributes of a vector element.
179 * The pointer to the string vector.
181 * The index of the string to get.
183 * The attributes to be set.
185 * 0 on success, -1 on error (errno is set). On error, attrs
186 * are freed and must not be used by the caller.
188 int ec_strvec_set_attrs(struct ec_strvec *strvec, size_t idx,
189 struct ec_dict *attrs);
192 * Compare two string vectors
195 * The pointer to the first string vector.
197 * The pointer to the second string vector.
199 * 0 if the string vectors are equal.
201 int ec_strvec_cmp(const struct ec_strvec *strvec1,
202 const struct ec_strvec *strvec2);
205 * Sort the string vector.
207 * Attributes are not compared.
210 * The pointer to the first string vector.
212 * The sort function to use. If NULL, use strcmp.
214 void ec_strvec_sort(struct ec_strvec *strvec,
215 int (*str_cmp)(const char *s1, const char *s2));
218 * Dump a string vector.
221 * The stream where the dump is sent.
223 * The pointer to the string vector.
225 void ec_strvec_dump(FILE *out, const struct ec_strvec *strvec);