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 * Allocate a new string vector
32 * The string vector is initialized with the list of const strings
33 * passed as arguments.
36 * The new strvec object, or NULL on error (errno is set).
38 #define EC_STRVEC(args...) ({ \
39 const char *_arr[] = {args}; \
40 ec_strvec_from_array(_arr, EC_COUNT_OF(_arr)); \
43 * Allocate a new string vector
45 * The string vector is initialized with the array of const strings
46 * passed as arguments.
49 * The array of const strings.
51 * The number of strings in the array.
53 * The new strvec object, or NULL on error (errno is set).
55 struct ec_strvec *ec_strvec_from_array(const char * const *strarr,
59 * Set a string in the vector at specified index.
62 * The pointer to the string vector.
64 * The index of the string to set.
66 * The string to be set.
68 * 0 on success or -1 on error (errno is set).
70 int ec_strvec_set(struct ec_strvec *strvec, size_t idx, const char *s);
73 * Add a string in a vector.
76 * The pointer to the string vector.
78 * The string to be added at the end of the vector.
80 * 0 on success or -1 on error (errno is set).
82 int ec_strvec_add(struct ec_strvec *strvec, const char *s);
85 * Delete the last entry in the string vector.
88 * The pointer to the string vector.
90 * The string to be added at the end of the vector.
92 * 0 on success or -1 on error (errno is set).
94 int ec_strvec_del_last(struct ec_strvec *strvec);
97 * Duplicate a string vector.
99 * Attributes are duplicated if any.
102 * The pointer to the string vector.
104 * The duplicated strvec object, or NULL on error (errno is set).
106 struct ec_strvec *ec_strvec_dup(const struct ec_strvec *strvec);
109 * Duplicate a part of a string vector.
111 * Attributes are duplicated if any.
114 * The pointer to the string vector.
116 * The index of the first string to duplicate.
118 * The number of strings to duplicate.
120 * The duplicated strvec object, or NULL on error (errno is set).
122 struct ec_strvec *ec_strvec_ndup(const struct ec_strvec *strvec,
123 size_t off, size_t len);
126 * Free a string vector.
129 * The pointer to the string vector.
131 void ec_strvec_free(struct ec_strvec *strvec);
134 * Get the length of a string vector.
137 * The pointer to the string vector.
139 * The length of the vector.
141 size_t ec_strvec_len(const struct ec_strvec *strvec);
144 * Get a string element from a vector.
147 * The pointer to the string vector.
149 * The index of the string to get.
151 * The string stored at given index, or NULL on error (strvec is NULL
154 const char *ec_strvec_val(const struct ec_strvec *strvec, size_t idx);
157 * Get the attributes of a vector element.
160 * The pointer to the string vector.
162 * The index of the string to get.
164 * The read-only attributes (dictionnary) of the string at specified
165 * index, or NULL if there is no attribute.
167 const struct ec_dict *ec_strvec_get_attrs(const struct ec_strvec *strvec,
171 * Set the attributes of a vector element.
174 * The pointer to the string vector.
176 * The index of the string to get.
178 * The attributes to be set.
180 * 0 on success, -1 on error (errno is set). On error, attrs
181 * are freed and must not be used by the caller.
183 int ec_strvec_set_attrs(struct ec_strvec *strvec, size_t idx,
184 struct ec_dict *attrs);
187 * Compare two string vectors
190 * The pointer to the first string vector.
192 * The pointer to the second string vector.
194 * 0 if the string vectors are equal.
196 int ec_strvec_cmp(const struct ec_strvec *strvec1,
197 const struct ec_strvec *strvec2);
200 * Sort the string vector.
202 * Attributes are not compared.
205 * The pointer to the first string vector.
207 * The sort function to use. If NULL, use strcmp.
209 void ec_strvec_sort(struct ec_strvec *strvec,
210 int (*str_cmp)(const char *s1, const char *s2));
213 * Dump a string vector.
216 * The stream where the dump is sent.
218 * The pointer to the string vector.
220 void ec_strvec_dump(FILE *out, const struct ec_strvec *strvec);