r10423: minor changes to the ldb test suite to allow it to work correctly with
[Samba/aatanasov.git] / source4 / lib / util_strlist.c
blob8efa1f92d2012dc421d2565d5a574fb132bf11ff
1 /*
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.
21 #include "includes.h"
24 build a null terminated list of strings from a input string and a
25 separator list. The separator 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)
30 int num_elements = 0;
31 const char **ret = NULL;
33 if (sep == NULL) {
34 sep = LIST_SEP;
37 ret = talloc_array(mem_ctx, const char *, 1);
38 if (ret == NULL) {
39 return NULL;
42 while (string && *string) {
43 size_t len = strcspn(string, sep);
44 const char **ret2;
46 if (len == 0) {
47 string += strspn(string, sep);
48 continue;
51 ret2 = talloc_realloc(mem_ctx, ret, const char *, num_elements+2);
52 if (ret2 == NULL) {
53 talloc_free(ret);
54 return NULL;
56 ret = ret2;
58 ret[num_elements] = talloc_strndup(ret, string, len);
59 if (ret[num_elements] == NULL) {
60 talloc_free(ret);
61 return NULL;
64 num_elements++;
65 string += len;
68 ret[num_elements] = NULL;
70 return ret;
73 /* join a list back to one string */
74 char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char seperator)
76 char *ret = NULL;
77 int i;
79 if (list[0] == NULL)
80 return talloc_strdup(mem_ctx, "");
82 ret = talloc_strdup(mem_ctx, list[0]);
84 for (i = 1; list[i]; i++) {
85 ret = talloc_asprintf_append(ret, "%c%s", seperator, list[i]);
88 return ret;
93 return the number of elements in a string list
95 size_t str_list_length(const char **list)
97 size_t ret;
98 for (ret=0;list && list[ret];ret++) /* noop */ ;
99 return ret;
104 copy a string list
106 const char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list)
108 int i;
109 const char **ret = talloc_array(mem_ctx, const char *, str_list_length(list)+1);
110 if (ret == NULL) return NULL;
112 for (i=0;list && list[i];i++) {
113 ret[i] = talloc_strdup(ret, list[i]);
114 if (ret[i] == NULL) {
115 talloc_free(ret);
116 return NULL;
119 ret[i] = NULL;
120 return ret;
124 Return true if all the elements of the list match exactly.
126 BOOL str_list_equal(const char **list1, const char **list2)
128 int i;
130 if (list1 == NULL || list2 == NULL) {
131 return (list1 == list2);
134 for (i=0;list1[i] && list2[i];i++) {
135 if (strcmp(list1[i], list2[i]) != 0) {
136 return False;
139 if (list1[i] || list2[i]) {
140 return False;
142 return True;
147 add an entry to a string list
149 const char **str_list_add(const char **list, const char *s)
151 size_t len = str_list_length(list);
152 const char **ret;
154 ret = talloc_realloc(NULL, list, const char *, len+2);
155 if (ret == NULL) return NULL;
157 ret[len] = talloc_strdup(ret, s);
158 if (ret[len] == NULL) return NULL;
160 ret[len+1] = NULL;
162 return ret;
166 remove an entry from a string list
168 void str_list_remove(const char **list, const char *s)
170 int i;
172 for (i=0;list[i];i++) {
173 if (strcmp(list[i], s) == 0) break;
175 if (!list[i]) return;
177 for (;list[i];i++) {
178 list[i] = list[i+1];
184 return True if a string is in a list
186 BOOL str_list_check(const char **list, const char *s)
188 int i;
190 for (i=0;list[i];i++) {
191 if (strcmp(list[i], s) == 0) return True;
193 return False;
197 return True if a string is in a list, case insensitively
199 BOOL str_list_check_ci(const char **list, const char *s)
201 int i;
203 for (i=0;list[i];i++) {
204 if (strcasecmp(list[i], s) == 0) return True;
206 return False;