join.py: Add Replica-Locations for DomainDNS and ForestDNS
[Samba.git] / lib / util / util_strlist.c
blob203a643b09baf19cee2adbaa3427a059b77265d6
1 /*
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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "replace.h"
22 #include "debug.h"
23 #include "tsort.h"
25 #include "util_strlist.h"
27 #undef strcasecmp
29 /**
30 * @file
31 * @brief String list manipulation
34 /**
35 build an empty (only NULL terminated) list of strings (for expansion with str_list_add() etc)
37 _PUBLIC_ char **str_list_make_empty(TALLOC_CTX *mem_ctx)
39 char **ret = NULL;
41 ret = talloc_array(mem_ctx, char *, 1);
42 if (ret == NULL) {
43 return NULL;
46 ret[0] = NULL;
48 return ret;
51 /**
52 place the only element 'entry' into a new, NULL terminated string list
54 _PUBLIC_ char **str_list_make_single(TALLOC_CTX *mem_ctx, const char *entry)
56 char **ret = NULL;
58 ret = talloc_array(mem_ctx, char *, 2);
59 if (ret == NULL) {
60 return NULL;
63 ret[0] = talloc_strdup(ret, entry);
64 if (!ret[0]) {
65 talloc_free(ret);
66 return NULL;
68 ret[1] = NULL;
70 return ret;
73 /**
74 build a null terminated list of strings from a input string and a
75 separator list. The separator list must contain characters less than
76 or equal to 0x2f for this to work correctly on multi-byte strings
78 _PUBLIC_ char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
80 int num_elements = 0;
81 char **ret = NULL;
83 if (sep == NULL) {
84 sep = LIST_SEP;
87 ret = talloc_array(mem_ctx, char *, 1);
88 if (ret == NULL) {
89 return NULL;
92 while (string && *string) {
93 size_t len = strcspn(string, sep);
94 char **ret2;
96 if (len == 0) {
97 string += strspn(string, sep);
98 continue;
101 ret2 = talloc_realloc(mem_ctx, ret, char *,
102 num_elements+2);
103 if (ret2 == NULL) {
104 talloc_free(ret);
105 return NULL;
107 ret = ret2;
109 ret[num_elements] = talloc_strndup(ret, string, len);
110 if (ret[num_elements] == NULL) {
111 talloc_free(ret);
112 return NULL;
115 num_elements++;
116 string += len;
119 ret[num_elements] = NULL;
121 return ret;
125 * build a null terminated list of strings from an argv-like input string
126 * Entries are separated by spaces and can be enclosed by quotes.
127 * Does NOT support escaping
129 _PUBLIC_ char **str_list_make_shell(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
131 int num_elements = 0;
132 char **ret = NULL;
134 ret = talloc_array(mem_ctx, char *, 1);
135 if (ret == NULL) {
136 return NULL;
139 if (sep == NULL)
140 sep = " \t\n\r";
142 while (string && *string) {
143 size_t len = strcspn(string, sep);
144 char *element;
145 char **ret2;
147 if (len == 0) {
148 string += strspn(string, sep);
149 continue;
152 if (*string == '\"') {
153 string++;
154 len = strcspn(string, "\"");
155 element = talloc_strndup(ret, string, len);
156 string += len + 1;
157 } else {
158 element = talloc_strndup(ret, string, len);
159 string += len;
162 if (element == NULL) {
163 talloc_free(ret);
164 return NULL;
167 ret2 = talloc_realloc(mem_ctx, ret, char *, num_elements+2);
168 if (ret2 == NULL) {
169 talloc_free(ret);
170 return NULL;
172 ret = ret2;
174 ret[num_elements] = element;
176 num_elements++;
179 ret[num_elements] = NULL;
181 return ret;
186 * join a list back to one string
188 _PUBLIC_ char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char separator)
190 char *ret = NULL;
191 int i;
193 if (list[0] == NULL)
194 return talloc_strdup(mem_ctx, "");
196 ret = talloc_strdup(mem_ctx, list[0]);
198 for (i = 1; list[i]; i++) {
199 ret = talloc_asprintf_append_buffer(ret, "%c%s", separator, list[i]);
202 return ret;
205 /** join a list back to one (shell-like) string; entries
206 * separated by spaces, using quotes where necessary */
207 _PUBLIC_ char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char sep)
209 char *ret = NULL;
210 int i;
212 if (list[0] == NULL)
213 return talloc_strdup(mem_ctx, "");
215 if (strchr(list[0], ' ') || strlen(list[0]) == 0)
216 ret = talloc_asprintf(mem_ctx, "\"%s\"", list[0]);
217 else
218 ret = talloc_strdup(mem_ctx, list[0]);
220 for (i = 1; list[i]; i++) {
221 if (strchr(list[i], ' ') || strlen(list[i]) == 0)
222 ret = talloc_asprintf_append_buffer(ret, "%c\"%s\"", sep, list[i]);
223 else
224 ret = talloc_asprintf_append_buffer(ret, "%c%s", sep, list[i]);
227 return ret;
231 return the number of elements in a string list
233 _PUBLIC_ size_t str_list_length(const char * const *list)
235 size_t ret;
236 for (ret=0;list && list[ret];ret++) /* noop */ ;
237 return ret;
242 copy a string list
244 _PUBLIC_ char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list)
246 int i;
247 char **ret;
249 if (list == NULL)
250 return NULL;
252 ret = talloc_array(mem_ctx, char *, str_list_length(list)+1);
253 if (ret == NULL)
254 return NULL;
256 for (i=0;list && list[i];i++) {
257 ret[i] = talloc_strdup(ret, list[i]);
258 if (ret[i] == NULL) {
259 talloc_free(ret);
260 return NULL;
263 ret[i] = NULL;
264 return ret;
268 Return true if all the elements of the list match exactly.
270 _PUBLIC_ bool str_list_equal(const char * const *list1,
271 const char * const *list2)
273 int i;
275 if (list1 == NULL || list2 == NULL) {
276 return (list1 == list2);
279 for (i=0;list1[i] && list2[i];i++) {
280 if (strcmp(list1[i], list2[i]) != 0) {
281 return false;
284 if (list1[i] || list2[i]) {
285 return false;
287 return true;
292 add an entry to a string list
294 _PUBLIC_ const char **str_list_add(const char **list, const char *s)
296 size_t len = str_list_length(list);
297 const char **ret;
299 ret = talloc_realloc(NULL, list, const char *, len+2);
300 if (ret == NULL) return NULL;
302 ret[len] = talloc_strdup(ret, s);
303 if (ret[len] == NULL) return NULL;
305 ret[len+1] = NULL;
307 return ret;
311 remove an entry from a string list
313 _PUBLIC_ void str_list_remove(const char **list, const char *s)
315 int i;
317 for (i=0;list[i];i++) {
318 if (strcmp(list[i], s) == 0) break;
320 if (!list[i]) return;
322 for (;list[i];i++) {
323 list[i] = list[i+1];
329 return true if a string is in a list
331 _PUBLIC_ bool str_list_check(const char **list, const char *s)
333 int i;
335 for (i=0; list != NULL && list[i] != NULL; i++) {
336 if (strcmp(list[i], s) == 0) return true;
338 return false;
342 return true if a string is in a list, case insensitively
344 _PUBLIC_ bool str_list_check_ci(const char **list, const char *s)
346 int i;
348 for (i=0; list != NULL && list[i] != NULL; i++) {
349 if (strcasecmp(list[i], s) == 0) return true;
351 return false;
356 append one list to another - expanding list1
358 _PUBLIC_ const char **str_list_append(const char **list1,
359 const char * const *list2)
361 size_t len1 = str_list_length(list1);
362 size_t len2 = str_list_length(list2);
363 const char **ret;
364 int i;
366 ret = talloc_realloc(NULL, list1, const char *, len1+len2+1);
367 if (ret == NULL) return NULL;
369 for (i=len1;i<len1+len2;i++) {
370 ret[i] = talloc_strdup(ret, list2[i-len1]);
371 if (ret[i] == NULL) {
372 return NULL;
375 ret[i] = NULL;
377 return ret;
380 static int list_cmp(const char **el1, const char **el2)
382 return strcmp(*el1, *el2);
386 return a list that only contains the unique elements of a list,
387 removing any duplicates
389 _PUBLIC_ const char **str_list_unique(const char **list)
391 size_t len = str_list_length(list);
392 const char **list2;
393 int i, j;
394 if (len < 2) {
395 return list;
397 list2 = (const char **)talloc_memdup(list, list,
398 sizeof(list[0])*(len+1));
399 TYPESAFE_QSORT(list2, len, list_cmp);
400 list[0] = list2[0];
401 for (i=j=1;i<len;i++) {
402 if (strcmp(list2[i], list[j-1]) != 0) {
403 list[j] = list2[i];
404 j++;
407 list[j] = NULL;
408 list = talloc_realloc(NULL, list, const char *, j + 1);
409 talloc_free(list2);
410 return list;
414 very useful when debugging complex list related code
416 _PUBLIC_ void str_list_show(const char **list)
418 int i;
419 DEBUG(0,("{ "));
420 for (i=0;list && list[i];i++) {
421 DEBUG(0,("\"%s\", ", list[i]));
423 DEBUG(0,("}\n"));
429 append one list to another - expanding list1
430 this assumes the elements of list2 are const pointers, so we can re-use them
432 _PUBLIC_ const char **str_list_append_const(const char **list1,
433 const char **list2)
435 size_t len1 = str_list_length(list1);
436 size_t len2 = str_list_length(list2);
437 const char **ret;
438 int i;
440 ret = talloc_realloc(NULL, list1, const char *, len1+len2+1);
441 if (ret == NULL) return NULL;
443 for (i=len1;i<len1+len2;i++) {
444 ret[i] = list2[i-len1];
446 ret[i] = NULL;
448 return ret;
452 * Add a string to an array of strings.
454 * num should be a pointer to an integer that holds the current
455 * number of elements in strings. It will be updated by this function.
457 _PUBLIC_ bool add_string_to_array(TALLOC_CTX *mem_ctx,
458 const char *str, const char ***strings, size_t *num)
460 char *dup_str = talloc_strdup(mem_ctx, str);
462 *strings = talloc_realloc(mem_ctx,
463 *strings,
464 const char *, ((*num)+1));
466 if ((*strings == NULL) || (dup_str == NULL)) {
467 *num = 0;
468 return false;
471 (*strings)[*num] = dup_str;
472 *num += 1;
474 return true;
478 add an entry to a string list
479 this assumes s will not change
481 _PUBLIC_ const char **str_list_add_const(const char **list, const char *s)
483 size_t len = str_list_length(list);
484 const char **ret;
486 ret = talloc_realloc(NULL, list, const char *, len+2);
487 if (ret == NULL) return NULL;
489 ret[len] = s;
490 ret[len+1] = NULL;
492 return ret;
496 copy a string list
497 this assumes list will not change
499 _PUBLIC_ const char **str_list_copy_const(TALLOC_CTX *mem_ctx,
500 const char **list)
502 int i;
503 const char **ret;
505 if (list == NULL)
506 return NULL;
508 ret = talloc_array(mem_ctx, const char *, str_list_length(list)+1);
509 if (ret == NULL)
510 return NULL;
512 for (i=0;list && list[i];i++) {
513 ret[i] = list[i];
515 ret[i] = NULL;
516 return ret;