1c03734748ed4f7697b81eb889d96e0ce0d3be71
[protos/libecoli.git] / lib / ecoli_strvec.h
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 /**
29  * Vectors of strings.
30  *
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.
34  */
35
36 #ifndef ECOLI_STRVEC_
37 #define ECOLI_STRVEC_
38
39 #include <stdio.h>
40
41 /**
42  * Allocate a new empty string vector.
43  *
44  * @return
45  *   The new strvec object, or NULL on error (errno is set).
46  */
47 struct ec_strvec *ec_strvec(void);
48
49 #ifndef EC_COUNT_OF
50 #define EC_COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
51                 ((size_t)(!(sizeof(x) % sizeof(0[x])))))
52 #endif
53
54 /**
55  * Allocate a new string vector
56  *
57  * The string vector is initialized with the list of const strings
58  * passed as arguments.
59  *
60  * @return
61  *   The new strvec object, or NULL on error (errno is set).
62  */
63 #define EC_STRVEC(args...) ({                                           \
64                         const char *_arr[] = {args};                    \
65                         ec_strvec_from_array(_arr, EC_COUNT_OF(_arr));  \
66                 })
67 /**
68  * Allocate a new string vector
69  *
70  * The string vector is initialized with the array of const strings
71  * passed as arguments.
72  *
73  * @param strarr
74  *   The array of const strings.
75  * @param n
76  *   The number of strings in the array.
77  * @return
78  *   The new strvec object, or NULL on error (errno is set).
79  */
80 struct ec_strvec *ec_strvec_from_array(const char * const *strarr,
81         size_t n);
82
83 /**
84  * Add a string in a vector.
85  *
86  * @param strvec
87  *   The pointer to the string vector.
88  * @param s
89  *   The string to be added at the end of the vector.
90  * @return
91  *   0 on success or -1 on error (errno is set).
92  */
93 int ec_strvec_add(struct ec_strvec *strvec, const char *s);
94
95 /**
96  * Delete the last entry in the string vector.
97  *
98  * @param strvec
99  *   The pointer to the string vector.
100  * @param s
101  *   The string to be added at the end of the vector.
102  * @return
103  *   0 on success or -1 on error (errno is set).
104  */
105 int ec_strvec_del_last(struct ec_strvec *strvec);
106
107 /**
108  * Duplicate a string vector.
109  *
110  * @param strvec
111  *   The pointer to the string vector.
112  * @return
113  *   The duplicated strvec object, or NULL on error (errno is set).
114  */
115 struct ec_strvec *ec_strvec_dup(const struct ec_strvec *strvec);
116
117 /**
118  * Duplicate a part of a string vector.
119  *
120  * @param strvec
121  *   The pointer to the string vector.
122  * @param off
123  *   The index of the first string to duplicate.
124  * @param
125  *   The number of strings to duplicate.
126  * @return
127  *   The duplicated strvec object, or NULL on error (errno is set).
128  */
129 struct ec_strvec *ec_strvec_ndup(const struct ec_strvec *strvec,
130         size_t off, size_t len);
131
132 /**
133  * Free a string vector.
134  *
135  * @param strvec
136  *   The pointer to the string vector.
137  */
138 void ec_strvec_free(struct ec_strvec *strvec);
139
140 /**
141  * Get the length of a string vector.
142  *
143  * @param strvec
144  *   The pointer to the string vector.
145  * @return
146  *   The length of the vector.
147  */
148 size_t ec_strvec_len(const struct ec_strvec *strvec);
149
150 /**
151  * Get a string element from a vector.
152  *
153  * @param strvec
154  *   The pointer to the string vector.
155  * @param idx
156  *   The index of the string to get.
157  * @return
158  *   The string stored at given index, or NULL on error (strvec is NULL
159  *   or invalid index).
160  */
161 const char *ec_strvec_val(const struct ec_strvec *strvec, size_t idx);
162
163 /**
164  * Compare two string vectors
165  *
166  * @param strvec
167  *   The pointer to the first string vector.
168  * @param strvec
169  *   The pointer to the second string vector.
170  * @return
171  *   0 if the string vectors are equal.
172  */
173 int ec_strvec_cmp(const struct ec_strvec *strvec1,
174                 const struct ec_strvec *strvec2);
175
176 /**
177  * Dump a string vector.
178  *
179  * @param out
180  *   The stream where the dump is sent.
181  * @param strvec
182  *   The pointer to the string vector.
183  */
184 void ec_strvec_dump(FILE *out, const struct ec_strvec *strvec);
185
186 #endif