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/>.
22 #include "system/locale.h"
23 #include "lib/util/tsort.h"
29 * @brief String list manipulation
33 build an empty (only NULL terminated) list of strings (for expansion with str_list_add() etc)
35 _PUBLIC_
char **str_list_make_empty(TALLOC_CTX
*mem_ctx
)
39 ret
= talloc_array(mem_ctx
, char *, 1);
50 place the only element 'entry' into a new, NULL terminated string list
52 _PUBLIC_
char **str_list_make_single(TALLOC_CTX
*mem_ctx
, const char *entry
)
56 ret
= talloc_array(mem_ctx
, char *, 2);
61 ret
[0] = talloc_strdup(ret
, entry
);
72 build a null terminated list of strings from a input string and a
73 separator list. The separator list must contain characters less than
74 or equal to 0x2f for this to work correctly on multi-byte strings
76 _PUBLIC_
char **str_list_make(TALLOC_CTX
*mem_ctx
, const char *string
, const char *sep
)
85 ret
= talloc_array(mem_ctx
, char *, 1);
90 while (string
&& *string
) {
91 size_t len
= strcspn(string
, sep
);
95 string
+= strspn(string
, sep
);
99 ret2
= talloc_realloc(mem_ctx
, ret
, char *,
107 ret
[num_elements
] = talloc_strndup(ret
, string
, len
);
108 if (ret
[num_elements
] == NULL
) {
117 ret
[num_elements
] = NULL
;
123 * build a null terminated list of strings from an argv-like input string
124 * Entries are separated by spaces and can be enclosed by quotes.
125 * Does NOT support escaping
127 _PUBLIC_
char **str_list_make_shell(TALLOC_CTX
*mem_ctx
, const char *string
, const char *sep
)
129 int num_elements
= 0;
132 ret
= talloc_array(mem_ctx
, char *, 1);
140 while (string
&& *string
) {
141 size_t len
= strcspn(string
, sep
);
146 string
+= strspn(string
, sep
);
150 if (*string
== '\"') {
152 len
= strcspn(string
, "\"");
153 element
= talloc_strndup(ret
, string
, len
);
156 element
= talloc_strndup(ret
, string
, len
);
160 if (element
== NULL
) {
165 ret2
= talloc_realloc(mem_ctx
, ret
, char *, num_elements
+2);
172 ret
[num_elements
] = element
;
177 ret
[num_elements
] = NULL
;
184 * join a list back to one string
186 _PUBLIC_
char *str_list_join(TALLOC_CTX
*mem_ctx
, const char **list
, char separator
)
192 return talloc_strdup(mem_ctx
, "");
194 ret
= talloc_strdup(mem_ctx
, list
[0]);
196 for (i
= 1; list
[i
]; i
++) {
197 ret
= talloc_asprintf_append_buffer(ret
, "%c%s", separator
, list
[i
]);
203 /** join a list back to one (shell-like) string; entries
204 * separated by spaces, using quotes where necessary */
205 _PUBLIC_
char *str_list_join_shell(TALLOC_CTX
*mem_ctx
, const char **list
, char sep
)
211 return talloc_strdup(mem_ctx
, "");
213 if (strchr(list
[0], ' ') || strlen(list
[0]) == 0)
214 ret
= talloc_asprintf(mem_ctx
, "\"%s\"", list
[0]);
216 ret
= talloc_strdup(mem_ctx
, list
[0]);
218 for (i
= 1; list
[i
]; i
++) {
219 if (strchr(list
[i
], ' ') || strlen(list
[i
]) == 0)
220 ret
= talloc_asprintf_append_buffer(ret
, "%c\"%s\"", sep
, list
[i
]);
222 ret
= talloc_asprintf_append_buffer(ret
, "%c%s", sep
, list
[i
]);
229 return the number of elements in a string list
231 _PUBLIC_
size_t str_list_length(const char * const *list
)
234 for (ret
=0;list
&& list
[ret
];ret
++) /* noop */ ;
242 _PUBLIC_
char **str_list_copy(TALLOC_CTX
*mem_ctx
, const char **list
)
250 ret
= talloc_array(mem_ctx
, char *, str_list_length(list
)+1);
254 for (i
=0;list
&& list
[i
];i
++) {
255 ret
[i
] = talloc_strdup(ret
, list
[i
]);
256 if (ret
[i
] == NULL
) {
266 Return true if all the elements of the list match exactly.
268 _PUBLIC_
bool str_list_equal(const char * const *list1
,
269 const char * const *list2
)
273 if (list1
== NULL
|| list2
== NULL
) {
274 return (list1
== list2
);
277 for (i
=0;list1
[i
] && list2
[i
];i
++) {
278 if (strcmp(list1
[i
], list2
[i
]) != 0) {
282 if (list1
[i
] || list2
[i
]) {
290 add an entry to a string list
292 _PUBLIC_
const char **str_list_add(const char **list
, const char *s
)
294 size_t len
= str_list_length(list
);
297 ret
= talloc_realloc(NULL
, list
, const char *, len
+2);
298 if (ret
== NULL
) return NULL
;
300 ret
[len
] = talloc_strdup(ret
, s
);
301 if (ret
[len
] == NULL
) return NULL
;
309 remove an entry from a string list
311 _PUBLIC_
void str_list_remove(const char **list
, const char *s
)
315 for (i
=0;list
[i
];i
++) {
316 if (strcmp(list
[i
], s
) == 0) break;
318 if (!list
[i
]) return;
327 return true if a string is in a list
329 _PUBLIC_
bool str_list_check(const char **list
, const char *s
)
333 for (i
=0;list
[i
];i
++) {
334 if (strcmp(list
[i
], s
) == 0) return true;
340 return true if a string is in a list, case insensitively
342 _PUBLIC_
bool str_list_check_ci(const char **list
, const char *s
)
346 for (i
=0;list
[i
];i
++) {
347 if (strcasecmp(list
[i
], s
) == 0) return true;
354 append one list to another - expanding list1
356 _PUBLIC_
const char **str_list_append(const char **list1
,
357 const char * const *list2
)
359 size_t len1
= str_list_length(list1
);
360 size_t len2
= str_list_length(list2
);
364 ret
= talloc_realloc(NULL
, list1
, const char *, len1
+len2
+1);
365 if (ret
== NULL
) return NULL
;
367 for (i
=len1
;i
<len1
+len2
;i
++) {
368 ret
[i
] = talloc_strdup(ret
, list2
[i
-len1
]);
369 if (ret
[i
] == NULL
) {
378 static int list_cmp(const char **el1
, const char **el2
)
380 return strcmp(*el1
, *el2
);
384 return a list that only contains the unique elements of a list,
385 removing any duplicates
387 _PUBLIC_
const char **str_list_unique(const char **list
)
389 size_t len
= str_list_length(list
);
395 list2
= (const char **)talloc_memdup(list
, list
,
396 sizeof(list
[0])*(len
+1));
397 TYPESAFE_QSORT(list2
, len
, list_cmp
);
399 for (i
=j
=1;i
<len
;i
++) {
400 if (strcmp(list2
[i
], list
[j
-1]) != 0) {
406 list
= talloc_realloc(NULL
, list
, const char *, j
+ 1);
412 very useful when debugging complex list related code
414 _PUBLIC_
void str_list_show(const char **list
)
418 for (i
=0;list
&& list
[i
];i
++) {
419 DEBUG(0,("\"%s\", ", list
[i
]));
427 append one list to another - expanding list1
428 this assumes the elements of list2 are const pointers, so we can re-use them
430 _PUBLIC_
const char **str_list_append_const(const char **list1
,
433 size_t len1
= str_list_length(list1
);
434 size_t len2
= str_list_length(list2
);
438 ret
= talloc_realloc(NULL
, list1
, const char *, len1
+len2
+1);
439 if (ret
== NULL
) return NULL
;
441 for (i
=len1
;i
<len1
+len2
;i
++) {
442 ret
[i
] = list2
[i
-len1
];
450 * Add a string to an array of strings.
452 * num should be a pointer to an integer that holds the current
453 * number of elements in strings. It will be updated by this function.
455 _PUBLIC_
bool add_string_to_array(TALLOC_CTX
*mem_ctx
,
456 const char *str
, const char ***strings
, int *num
)
458 char *dup_str
= talloc_strdup(mem_ctx
, str
);
460 *strings
= talloc_realloc(mem_ctx
,
462 const char *, ((*num
)+1));
464 if ((*strings
== NULL
) || (dup_str
== NULL
)) {
469 (*strings
)[*num
] = dup_str
;
476 add an entry to a string list
477 this assumes s will not change
479 _PUBLIC_
const char **str_list_add_const(const char **list
, const char *s
)
481 size_t len
= str_list_length(list
);
484 ret
= talloc_realloc(NULL
, list
, const char *, len
+2);
485 if (ret
== NULL
) return NULL
;
495 this assumes list will not change
497 _PUBLIC_
const char **str_list_copy_const(TALLOC_CTX
*mem_ctx
,
506 ret
= talloc_array(mem_ctx
, const char *, str_list_length(list
)+1);
510 for (i
=0;list
&& list
[i
];i
++) {
518 * Needed for making an "unconst" list "const"
520 _PUBLIC_
const char **const_str_list(char **list
)
522 return discard_const_p(const char *, list
);
526 * str_list_make, v3 version. The v4 version does not
527 * look at quoted strings with embedded blanks, so
528 * do NOT merge this function please!
530 #define S_LIST_ABS 16 /* List Allocation Block Size */
532 char **str_list_make_v3(TALLOC_CTX
*mem_ctx
, const char *string
,
540 if (!string
|| !*string
)
543 list
= talloc_array(mem_ctx
, char *, S_LIST_ABS
+1);
549 s
= talloc_strdup(list
, string
);
551 DEBUG(0,("str_list_make: Unable to allocate memory"));
555 if (!sep
) sep
= LIST_SEP
;
560 while (next_token_talloc(list
, &str
, &tok
, sep
)) {
567 tmp
= talloc_realloc(mem_ctx
, list
, char *,
570 DEBUG(0,("str_list_make: "
571 "Unable to allocate memory"));
578 memset (&list
[num
], 0,
579 ((sizeof(char*)) * (S_LIST_ABS
+1)));