strvec
[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 /**
50  * Add a string in a vector.
51  *
52  * @param strvec
53  *   The pointer to the string vector.
54  * @param s
55  *   The string to be added at the end of the vector.
56  * @return
57  *   0 on success or -1 on error (errno is set).
58  */
59 int ec_strvec_add(struct ec_strvec *strvec, const char *s);
60
61 /**
62  * Duplicate a string vector.
63  *
64  * @param strvec
65  *   The pointer to the string vector.
66  * @return
67  *   The duplicated strvec object, or NULL on error (errno is set).
68  */
69 struct ec_strvec *ec_strvec_dup(const struct ec_strvec *strvec);
70
71 /**
72  * Duplicate a part of a string vector.
73  *
74  * @param strvec
75  *   The pointer to the string vector.
76  * @param off
77  *   The index of the first string to duplicate.
78  * @param
79  *   The number of strings to duplicate.
80  * @return
81  *   The duplicated strvec object, or NULL on error (errno is set).
82  */
83 struct ec_strvec *ec_strvec_ndup(const struct ec_strvec *strvec,
84         size_t off, size_t len);
85
86 /**
87  * Free a string vector.
88  *
89  * @param strvec
90  *   The pointer to the string vector.
91  */
92 void ec_strvec_free(struct ec_strvec *strvec);
93
94 /**
95  * Get the length of a string vector.
96  *
97  * @param strvec
98  *   The pointer to the string vector.
99  * @return
100  *   The length of the vector.
101  */
102 size_t ec_strvec_len(const struct ec_strvec *strvec);
103
104 /**
105  * Get a string element from a vector.
106  *
107  * @param strvec
108  *   The pointer to the string vector.
109  * @param idx
110  *   The index of the string to get.
111  * @return
112  *   The string stored at given index, or NULL on error (strvec is NULL
113  *   or invalid index).
114  */
115 const char *ec_strvec_val(const struct ec_strvec *strvec, size_t idx);
116
117 /**
118  * Dump a string vector.
119  *
120  * @param out
121  *   The stream where the dump is sent.
122  * @param strvec
123  *   The pointer to the string vector.
124  */
125 void ec_strvec_dump(FILE *out, const struct ec_strvec *strvec);
126
127 #endif