1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
8 * The ec_strvec API provide helpers to manipulate string vectors.
9 * When duplicating vectors, the strings are not duplicated in memory,
10 * a reference counter is used.
19 * Allocate a new empty string vector.
22 * The new strvec object, or NULL on error (errno is set).
24 struct ec_strvec *ec_strvec(void);
27 #define EC_COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
28 ((size_t)(!(sizeof(x) % sizeof(0[x])))))
32 * Allocate a new string vector
34 * The string vector is initialized with the list of const strings
35 * passed as arguments.
38 * The new strvec object, or NULL on error (errno is set).
40 #define EC_STRVEC(args...) ({ \
41 const char *_arr[] = {args}; \
42 ec_strvec_from_array(_arr, EC_COUNT_OF(_arr)); \
45 * Allocate a new string vector
47 * The string vector is initialized with the array of const strings
48 * passed as arguments.
51 * The array of const strings.
53 * The number of strings in the array.
55 * The new strvec object, or NULL on error (errno is set).
57 struct ec_strvec *ec_strvec_from_array(const char * const *strarr,
61 * Set a string in the vector at specified index.
64 * The pointer to the string vector.
66 * The index of the string to set.
68 * The string to be set.
70 * 0 on success or -1 on error (errno is set).
72 int ec_strvec_set(struct ec_strvec *strvec, size_t idx, const char *s);
75 * Add a string in a vector.
78 * The pointer to the string vector.
80 * The string to be added at the end of the vector.
82 * 0 on success or -1 on error (errno is set).
84 int ec_strvec_add(struct ec_strvec *strvec, const char *s);
87 * Delete the last entry in the string vector.
90 * The pointer to the string vector.
92 * The string to be added at the end of the vector.
94 * 0 on success or -1 on error (errno is set).
96 int ec_strvec_del_last(struct ec_strvec *strvec);
99 * Duplicate a string vector.
101 * Attributes are duplicated if any.
104 * The pointer to the string vector.
106 * The duplicated strvec object, or NULL on error (errno is set).
108 struct ec_strvec *ec_strvec_dup(const struct ec_strvec *strvec);
111 * Duplicate a part of a string vector.
113 * Attributes are duplicated if any.
116 * The pointer to the string vector.
118 * The index of the first string to duplicate.
120 * The number of strings to duplicate.
122 * The duplicated strvec object, or NULL on error (errno is set).
124 struct ec_strvec *ec_strvec_ndup(const struct ec_strvec *strvec,
125 size_t off, size_t len);
128 * Free a string vector.
131 * The pointer to the string vector.
133 void ec_strvec_free(struct ec_strvec *strvec);
136 * Get the length of a string vector.
139 * The pointer to the string vector.
141 * The length of the vector.
143 size_t ec_strvec_len(const struct ec_strvec *strvec);
146 * Get a string element from a vector.
149 * The pointer to the string vector.
151 * The index of the string to get.
153 * The string stored at given index, or NULL on error (strvec is NULL
156 const char *ec_strvec_val(const struct ec_strvec *strvec, size_t idx);
159 * Get the attributes of a vector element.
162 * The pointer to the string vector.
164 * The index of the string to get.
166 * The read-only attributes (dictionnary) of the string at specified
167 * index, or NULL if there is no attribute.
169 const struct ec_keyval *ec_strvec_get_attrs(const struct ec_strvec *strvec,
173 * Set the attributes of a vector element.
176 * The pointer to the string vector.
178 * The index of the string to get.
180 * The attributes to be set.
182 * 0 on success, -1 on error (errno is set). On error, attrs
183 * are freed and must not be used by the caller.
185 int ec_strvec_set_attrs(struct ec_strvec *strvec, size_t idx,
186 struct ec_keyval *attrs);
189 * Compare two string vectors
192 * The pointer to the first string vector.
194 * The pointer to the second string vector.
196 * 0 if the string vectors are equal.
198 int ec_strvec_cmp(const struct ec_strvec *strvec1,
199 const struct ec_strvec *strvec2);
202 * Sort the string vector.
204 * Attributes are not compared.
207 * The pointer to the first string vector.
209 * The sort function to use. If NULL, use strcmp.
211 void ec_strvec_sort(struct ec_strvec *strvec,
212 int (*str_cmp)(const char *s1, const char *s2));
215 * Dump a string vector.
218 * The stream where the dump is sent.
220 * The pointer to the string vector.
222 void ec_strvec_dump(FILE *out, const struct ec_strvec *strvec);