2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2005
5 Copyright (C) Jelmer Vernooij 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "system/locale.h"
28 * @brief String list manipulation
32 build a null terminated list of strings from a input string and a
33 separator list. The separator list must contain characters less than
34 or equal to 0x2f for this to work correctly on multi-byte strings
36 _PUBLIC_ const char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
39 const char **ret = NULL;
45 ret = talloc_array(mem_ctx, const char *, 1);
50 while (string && *string) {
51 size_t len = strcspn(string, sep);
55 string += strspn(string, sep);
59 ret2 = talloc_realloc(mem_ctx, ret, const char *, num_elements+2);
66 ret[num_elements] = talloc_strndup(ret, string, len);
67 if (ret[num_elements] == NULL) {
76 ret[num_elements] = NULL;
82 * build a null terminated list of strings from an argv-like input string
83 * Entries are seperated by spaces and can be enclosed by quotes.
84 * Does NOT support escaping
86 _PUBLIC_ const char **str_list_make_shell(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
89 const char **ret = NULL;
91 ret = talloc_array(mem_ctx, const char *, 1);
99 while (string && *string) {
100 size_t len = strcspn(string, sep);
105 string += strspn(string, sep);
109 if (*string == '\"') {
111 len = strcspn(string, "\"");
112 element = talloc_strndup(ret, string, len);
115 element = talloc_strndup(ret, string, len);
119 if (element == NULL) {
124 ret2 = talloc_realloc(mem_ctx, ret, const char *, num_elements+2);
131 ret[num_elements] = element;
136 ret[num_elements] = NULL;
143 * join a list back to one string
145 _PUBLIC_ char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char seperator)
151 return talloc_strdup(mem_ctx, "");
153 ret = talloc_strdup(mem_ctx, list[0]);
155 for (i = 1; list[i]; i++) {
156 ret = talloc_asprintf_append(ret, "%c%s", seperator, list[i]);
162 /** join a list back to one (shell-like) string; entries
163 * seperated by spaces, using quotes where necessary */
164 _PUBLIC_ char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char sep)
170 return talloc_strdup(mem_ctx, "");
172 if (strchr(list[0], ' ') || strlen(list[0]) == 0)
173 ret = talloc_asprintf(mem_ctx, "\"%s\"", list[0]);
175 ret = talloc_strdup(mem_ctx, list[0]);
177 for (i = 1; list[i]; i++) {
178 if (strchr(list[i], ' ') || strlen(list[i]) == 0)
179 ret = talloc_asprintf_append(ret, "%c\"%s\"", sep, list[i]);
181 ret = talloc_asprintf_append(ret, "%c%s", sep, list[i]);
188 return the number of elements in a string list
190 _PUBLIC_ size_t str_list_length(const char **list)
193 for (ret=0;list && list[ret];ret++) /* noop */ ;
201 _PUBLIC_ const char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list)
204 const char **ret = talloc_array(mem_ctx, const char *, str_list_length(list)+1);
205 if (ret == NULL) return NULL;
207 for (i=0;list && list[i];i++) {
208 ret[i] = talloc_strdup(ret, list[i]);
209 if (ret[i] == NULL) {
219 Return true if all the elements of the list match exactly.
221 _PUBLIC_ BOOL str_list_equal(const char **list1, const char **list2)
225 if (list1 == NULL || list2 == NULL) {
226 return (list1 == list2);
229 for (i=0;list1[i] && list2[i];i++) {
230 if (strcmp(list1[i], list2[i]) != 0) {
234 if (list1[i] || list2[i]) {
242 add an entry to a string list
244 _PUBLIC_ const char **str_list_add(const char **list, const char *s)
246 size_t len = str_list_length(list);
249 ret = talloc_realloc(NULL, list, const char *, len+2);
250 if (ret == NULL) return NULL;
252 ret[len] = talloc_strdup(ret, s);
253 if (ret[len] == NULL) return NULL;
261 remove an entry from a string list
263 _PUBLIC_ void str_list_remove(const char **list, const char *s)
267 for (i=0;list[i];i++) {
268 if (strcmp(list[i], s) == 0) break;
270 if (!list[i]) return;
279 return True if a string is in a list
281 _PUBLIC_ BOOL str_list_check(const char **list, const char *s)
285 for (i=0;list[i];i++) {
286 if (strcmp(list[i], s) == 0) return True;
292 return True if a string is in a list, case insensitively
294 _PUBLIC_ BOOL str_list_check_ci(const char **list, const char *s)
298 for (i=0;list[i];i++) {
299 if (strcasecmp(list[i], s) == 0) return True;
305 Check if a string is part of a list.
307 _PUBLIC_ BOOL in_list(const char *s, const char *list, BOOL casesensitive)
315 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
317 if (strcmp(tok,s) == 0)
320 if (strcasecmp_m(tok,s) == 0)