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.
25 build a null terminated list of strings from a input string and a
26 separator list. The separator list must contain characters less than
27 or equal to 0x2f for this to work correctly on multi-byte strings
29 const char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
32 const char **ret = NULL;
38 ret = talloc_array(mem_ctx, const char *, 1);
43 while (string && *string) {
44 size_t len = strcspn(string, sep);
48 string += strspn(string, sep);
52 ret2 = talloc_realloc(mem_ctx, ret, const char *, num_elements+2);
59 ret[num_elements] = talloc_strndup(ret, string, len);
60 if (ret[num_elements] == NULL) {
69 ret[num_elements] = NULL;
74 /* build a null terminated list of strings from an argv-like input string
75 Entries are seperated by spaces and can be enclosed by quotes.
76 Does NOT support escaping
78 const char **str_list_make_shell(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
81 const char **ret = NULL;
83 ret = talloc_array(mem_ctx, const char *, 1);
91 while (string && *string) {
92 size_t len = strcspn(string, sep);
97 string += strspn(string, sep);
101 if (*string == '\"') {
103 len = strcspn(string, "\"");
104 element = talloc_strndup(ret, string, len);
107 element = talloc_strndup(ret, string, len);
111 if (element == NULL) {
116 ret2 = talloc_realloc(mem_ctx, ret, const char *, num_elements+2);
123 ret[num_elements] = element;
128 ret[num_elements] = NULL;
134 /* join a list back to one string */
135 char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char seperator)
141 return talloc_strdup(mem_ctx, "");
143 ret = talloc_strdup(mem_ctx, list[0]);
145 for (i = 1; list[i]; i++) {
146 ret = talloc_asprintf_append(ret, "%c%s", seperator, list[i]);
152 /* join a list back to one (shell-like) string; entries
153 * seperated by spaces, using quotes where necessary */
154 char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char sep)
160 return talloc_strdup(mem_ctx, "");
162 if (strchr(list[0], ' ') || strlen(list[0]) == 0)
163 ret = talloc_asprintf(mem_ctx, "\"%s\"", list[0]);
165 ret = talloc_strdup(mem_ctx, list[0]);
167 for (i = 1; list[i]; i++) {
168 if (strchr(list[i], ' ') || strlen(list[i]) == 0)
169 ret = talloc_asprintf_append(ret, "%c\"%s\"", sep, list[i]);
171 ret = talloc_asprintf_append(ret, "%c%s", sep, list[i]);
178 return the number of elements in a string list
180 size_t str_list_length(const char **list)
183 for (ret=0;list && list[ret];ret++) /* noop */ ;
191 const char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list)
194 const char **ret = talloc_array(mem_ctx, const char *, str_list_length(list)+1);
195 if (ret == NULL) return NULL;
197 for (i=0;list && list[i];i++) {
198 ret[i] = talloc_strdup(ret, list[i]);
199 if (ret[i] == NULL) {
209 Return true if all the elements of the list match exactly.
211 BOOL str_list_equal(const char **list1, const char **list2)
215 if (list1 == NULL || list2 == NULL) {
216 return (list1 == list2);
219 for (i=0;list1[i] && list2[i];i++) {
220 if (strcmp(list1[i], list2[i]) != 0) {
224 if (list1[i] || list2[i]) {
232 add an entry to a string list
234 const char **str_list_add(const char **list, const char *s)
236 size_t len = str_list_length(list);
239 ret = talloc_realloc(NULL, list, const char *, len+2);
240 if (ret == NULL) return NULL;
242 ret[len] = talloc_strdup(ret, s);
243 if (ret[len] == NULL) return NULL;
251 remove an entry from a string list
253 void str_list_remove(const char **list, const char *s)
257 for (i=0;list[i];i++) {
258 if (strcmp(list[i], s) == 0) break;
260 if (!list[i]) return;
269 return True if a string is in a list
271 BOOL str_list_check(const char **list, const char *s)
275 for (i=0;list[i];i++) {
276 if (strcmp(list[i], s) == 0) return True;
282 return True if a string is in a list, case insensitively
284 BOOL str_list_check_ci(const char **list, const char *s)
288 for (i=0;list[i];i++) {
289 if (strcasecmp(list[i], s) == 0) return True;