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 * Add a string in a vector.
64 * The pointer to the string vector.
66 * The string to be added at the end of the vector.
68 * 0 on success or -1 on error (errno is set).
70 int ec_strvec_add(struct ec_strvec *strvec, const char *s);
73 * Delete the last entry in the string 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_del_last(struct ec_strvec *strvec);
85 * Duplicate a string vector.
88 * The pointer to the string vector.
90 * The duplicated strvec object, or NULL on error (errno is set).
92 struct ec_strvec *ec_strvec_dup(const struct ec_strvec *strvec);
95 * Duplicate a part of a string vector.
98 * The pointer to the string vector.
100 * The index of the first string to duplicate.
102 * The number of strings to duplicate.
104 * The duplicated strvec object, or NULL on error (errno is set).
106 struct ec_strvec *ec_strvec_ndup(const struct ec_strvec *strvec,
107 size_t off, size_t len);
110 * Free a string vector.
113 * The pointer to the string vector.
115 void ec_strvec_free(struct ec_strvec *strvec);
118 * Get the length of a string vector.
121 * The pointer to the string vector.
123 * The length of the vector.
125 size_t ec_strvec_len(const struct ec_strvec *strvec);
128 * Get a string element from a vector.
131 * The pointer to the string vector.
133 * The index of the string to get.
135 * The string stored at given index, or NULL on error (strvec is NULL
138 const char *ec_strvec_val(const struct ec_strvec *strvec, size_t idx);
141 * Compare two string vectors
144 * The pointer to the first string vector.
146 * The pointer to the second string vector.
148 * 0 if the string vectors are equal.
150 int ec_strvec_cmp(const struct ec_strvec *strvec1,
151 const struct ec_strvec *strvec2);
154 * Sort the string vector.
157 * The pointer to the first string vector.
159 * The sort function to use. If NULL, use strcmp.
161 void ec_strvec_sort(struct ec_strvec *strvec,
162 int (*str_cmp)(const char *s1, const char *s2));
165 * Dump a string vector.
168 * The stream where the dump is sent.
170 * The pointer to the string vector.
172 void ec_strvec_dump(FILE *out, const struct ec_strvec *strvec);