1 /* Copyright  (C) 2010-2020 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (string_list.h).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __LIBRETRO_SDK_STRING_LIST_H
24 #define __LIBRETRO_SDK_STRING_LIST_H
25 
26 #include <retro_common_api.h>
27 
28 #include <boolean.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31 
32 RETRO_BEGIN_DECLS
33 
34 union string_list_elem_attr
35 {
36    bool  b;
37    int   i;
38    void *p;
39 };
40 
41 struct string_list_elem
42 {
43    char *data;
44    void *userdata;
45    union string_list_elem_attr attr;
46 };
47 
48 struct string_list
49 {
50    struct string_list_elem *elems;
51    size_t size;
52    size_t cap;
53 };
54 
55 /**
56  * string_list_find_elem:
57  * @list             : pointer to string list
58  * @elem             : element to find inside the string list.
59  *
60  * Searches for an element (@elem) inside the string list.
61  *
62  * Returns: true (1) if element could be found, otherwise false (0).
63  */
64 int string_list_find_elem(const struct string_list *list, const char *elem);
65 
66 /**
67  * string_list_find_elem_prefix:
68  * @list             : pointer to string list
69  * @prefix           : prefix to append to @elem
70  * @elem             : element to find inside the string list.
71  *
72  * Searches for an element (@elem) inside the string list. Will
73  * also search for the same element prefixed by @prefix.
74  *
75  * Returns: true (1) if element could be found, otherwise false (0).
76  */
77 bool string_list_find_elem_prefix(const struct string_list *list,
78       const char *prefix, const char *elem);
79 
80 /**
81  * string_split:
82  * @str              : string to turn into a string list
83  * @delim            : delimiter character to use for splitting the string.
84  *
85  * Creates a new string list based on string @str, delimited by @delim.
86  *
87  * Returns: new string list if successful, otherwise NULL.
88  */
89 struct string_list *string_split(const char *str, const char *delim);
90 
91 bool string_split_noalloc(struct string_list *list,
92       const char *str, const char *delim);
93 
94 /**
95  * string_separate:
96  * @str              : string to turn into a string list
97  * @delim            : delimiter character to use for separating the string.
98  *
99  * Creates a new string list based on string @str, delimited by @delim.
100  * Includes empty strings - i.e. two adjacent delimiters will resolve
101  * to a string list element of "".
102  *
103  * Returns: new string list if successful, otherwise NULL.
104  */
105 struct string_list *string_separate(char *str, const char *delim);
106 
107 bool string_separate_noalloc(struct string_list *list,
108       char *str, const char *delim);
109 
110 bool string_list_deinitialize(struct string_list *list);
111 
112 bool string_list_initialize(struct string_list *list);
113 
114 /**
115  * string_list_new:
116  *
117  * Creates a new string list. Has to be freed manually.
118  *
119  * Returns: new string list if successful, otherwise NULL.
120  */
121 struct string_list *string_list_new(void);
122 
123 /**
124  * string_list_append:
125  * @list             : pointer to string list
126  * @elem             : element to add to the string list
127  * @attr             : attributes of new element.
128  *
129  * Appends a new element to the string list.
130  *
131  * Returns: true (1) if successful, otherwise false (0).
132  **/
133 bool string_list_append(struct string_list *list, const char *elem,
134       union string_list_elem_attr attr);
135 
136 /**
137  * string_list_append_n:
138  * @list             : pointer to string list
139  * @elem             : element to add to the string list
140  * @length           : read at most this many bytes from elem
141  * @attr             : attributes of new element.
142  *
143  * Appends a new element to the string list.
144  *
145  * Returns: true (1) if successful, otherwise false (0).
146  **/
147 bool string_list_append_n(struct string_list *list, const char *elem,
148       unsigned length, union string_list_elem_attr attr);
149 
150 /**
151  * string_list_free
152  * @list             : pointer to string list object
153  *
154  * Frees a string list.
155  */
156 void string_list_free(struct string_list *list);
157 
158 /**
159  * string_list_join_concat:
160  * @buffer           : buffer that @list will be joined to.
161  * @size             : length of @buffer.
162  * @list             : pointer to string list.
163  * @delim            : delimiter character for @list.
164  *
165  * A string list will be joined/concatenated as a
166  * string to @buffer, delimited by @delim.
167  */
168 void string_list_join_concat(char *buffer, size_t size,
169       const struct string_list *list, const char *sep);
170 
171 /**
172  * string_list_set:
173  * @list             : pointer to string list
174  * @idx              : index of element in string list
175  * @str              : value for the element.
176  *
177  * Set value of element inside string list.
178  **/
179 void string_list_set(struct string_list *list, unsigned idx,
180       const char *str);
181 
182 struct string_list *string_list_clone(const struct string_list *src);
183 
184 RETRO_END_DECLS
185 
186 #endif
187