2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 build a null terminated list of strings from a input string and a
25 separator list. The sepatator list must contain characters less than
26 or equal to 0x2f for this to work correctly on multi-byte strings
28 const char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
31 const char **ret = NULL;
37 ret = talloc_array(mem_ctx, const char *, 1);
42 while (string && *string) {
43 size_t len = strcspn(string, sep);
47 string += strspn(string, sep);
51 ret2 = talloc_realloc(mem_ctx, ret, const char *, num_elements+2);
58 ret[num_elements] = talloc_strndup(ret, string, len);
59 if (ret[num_elements] == NULL) {
68 ret[num_elements] = NULL;
74 return the number of elements in a string list
76 size_t str_list_length(const char **list)
79 for (ret=0;list && list[ret];ret++) /* noop */ ;
87 const char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list)
90 const char **ret = talloc_array(mem_ctx, const char *, str_list_length(list)+1);
91 if (ret == NULL) return NULL;
93 for (i=0;list && list[i];i++) {
94 ret[i] = talloc_strdup(ret, list[i]);
105 Return true if all the elements of the list match exactly.
107 BOOL str_list_equal(const char **list1, const char **list2)
111 if (list1 == NULL || list2 == NULL) {
112 return (list1 == list2);
115 for (i=0;list1[i] && list2[i];i++) {
116 if (strcmp(list1[i], list2[i]) != 0) {
120 if (list1[i] || list2[i]) {
128 add an entry to a string list
130 const char **str_list_add(const char **list, const char *s)
132 size_t len = str_list_length(list);
135 ret = talloc_realloc(NULL, list, const char *, len+2);
136 if (ret == NULL) return NULL;
138 ret[len] = talloc_strdup(ret, s);
139 if (ret[len] == NULL) return NULL;
147 remove an entry from a string list
149 void str_list_remove(const char **list, const char *s)
153 for (i=0;list[i];i++) {
154 if (strcmp(list[i], s) == 0) break;
156 if (!list[i]) return;
165 return True if a string is in a list
167 BOOL str_list_check(const char **list, const char *s)
171 for (i=0;list[i];i++) {
172 if (strcmp(list[i], s) == 0) return True;