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"
28 * @brief String list manipulation
32 build an empty (only NULL terminated) list of strings (for expansion with str_list_add() etc)
34 _PUBLIC_
char **str_list_make_empty(TALLOC_CTX
*mem_ctx
)
38 ret
= talloc_array(mem_ctx
, char *, 1);
49 place the only element 'entry' into a new, NULL terminated string list
51 _PUBLIC_
char **str_list_make_single(TALLOC_CTX
*mem_ctx
, const char *entry
)
55 ret
= talloc_array(mem_ctx
, char *, 2);
60 ret
[0] = talloc_strdup(ret
, entry
);
71 build a null terminated list of strings from a input string and a
72 separator list. The separator list must contain characters less than
73 or equal to 0x2f for this to work correctly on multi-byte strings
75 _PUBLIC_
char **str_list_make(TALLOC_CTX
*mem_ctx
, const char *string
, const char *sep
)
84 ret
= talloc_array(mem_ctx
, char *, 1);
89 while (string
&& *string
) {
90 size_t len
= strcspn(string
, sep
);
94 string
+= strspn(string
, sep
);
98 ret2
= talloc_realloc(mem_ctx
, ret
, char *,
106 ret
[num_elements
] = talloc_strndup(ret
, string
, len
);
107 if (ret
[num_elements
] == NULL
) {
116 ret
[num_elements
] = NULL
;
122 * build a null terminated list of strings from an argv-like input string
123 * Entries are seperated by spaces and can be enclosed by quotes.
124 * Does NOT support escaping
126 _PUBLIC_
char **str_list_make_shell(TALLOC_CTX
*mem_ctx
, const char *string
, const char *sep
)
128 int num_elements
= 0;
131 ret
= talloc_array(mem_ctx
, char *, 1);
139 while (string
&& *string
) {
140 size_t len
= strcspn(string
, sep
);
145 string
+= strspn(string
, sep
);
149 if (*string
== '\"') {
151 len
= strcspn(string
, "\"");
152 element
= talloc_strndup(ret
, string
, len
);
155 element
= talloc_strndup(ret
, string
, len
);
159 if (element
== NULL
) {
164 ret2
= talloc_realloc(mem_ctx
, ret
, char *, num_elements
+2);
171 ret
[num_elements
] = element
;
176 ret
[num_elements
] = NULL
;
183 * join a list back to one string
185 _PUBLIC_
char *str_list_join(TALLOC_CTX
*mem_ctx
, const char **list
, char seperator
)
191 return talloc_strdup(mem_ctx
, "");
193 ret
= talloc_strdup(mem_ctx
, list
[0]);
195 for (i
= 1; list
[i
]; i
++) {
196 ret
= talloc_asprintf_append_buffer(ret
, "%c%s", seperator
, list
[i
]);
202 /** join a list back to one (shell-like) string; entries
203 * seperated by spaces, using quotes where necessary */
204 _PUBLIC_
char *str_list_join_shell(TALLOC_CTX
*mem_ctx
, const char **list
, char sep
)
210 return talloc_strdup(mem_ctx
, "");
212 if (strchr(list
[0], ' ') || strlen(list
[0]) == 0)
213 ret
= talloc_asprintf(mem_ctx
, "\"%s\"", list
[0]);
215 ret
= talloc_strdup(mem_ctx
, list
[0]);
217 for (i
= 1; list
[i
]; i
++) {
218 if (strchr(list
[i
], ' ') || strlen(list
[i
]) == 0)
219 ret
= talloc_asprintf_append_buffer(ret
, "%c\"%s\"", sep
, list
[i
]);
221 ret
= talloc_asprintf_append_buffer(ret
, "%c%s", sep
, list
[i
]);
228 return the number of elements in a string list
230 _PUBLIC_
size_t str_list_length(const char * const *list
)
233 for (ret
=0;list
&& list
[ret
];ret
++) /* noop */ ;
241 _PUBLIC_
char **str_list_copy(TALLOC_CTX
*mem_ctx
, const char **list
)
249 ret
= talloc_array(mem_ctx
, char *, str_list_length(list
)+1);
253 for (i
=0;list
&& list
[i
];i
++) {
254 ret
[i
] = talloc_strdup(ret
, list
[i
]);
255 if (ret
[i
] == NULL
) {
265 Return true if all the elements of the list match exactly.
267 _PUBLIC_
bool str_list_equal(const char * const *list1
,
268 const char * const *list2
)
272 if (list1
== NULL
|| list2
== NULL
) {
273 return (list1
== list2
);
276 for (i
=0;list1
[i
] && list2
[i
];i
++) {
277 if (strcmp(list1
[i
], list2
[i
]) != 0) {
281 if (list1
[i
] || list2
[i
]) {
289 add an entry to a string list
291 _PUBLIC_
const char **str_list_add(const char **list
, const char *s
)
293 size_t len
= str_list_length(list
);
296 ret
= talloc_realloc(NULL
, list
, const char *, len
+2);
297 if (ret
== NULL
) return NULL
;
299 ret
[len
] = talloc_strdup(ret
, s
);
300 if (ret
[len
] == NULL
) return NULL
;
308 remove an entry from a string list
310 _PUBLIC_
void str_list_remove(const char **list
, const char *s
)
314 for (i
=0;list
[i
];i
++) {
315 if (strcmp(list
[i
], s
) == 0) break;
317 if (!list
[i
]) return;
326 return true if a string is in a list
328 _PUBLIC_
bool str_list_check(const char **list
, const char *s
)
332 for (i
=0;list
[i
];i
++) {
333 if (strcmp(list
[i
], s
) == 0) return true;
339 return true if a string is in a list, case insensitively
341 _PUBLIC_
bool str_list_check_ci(const char **list
, const char *s
)
345 for (i
=0;list
[i
];i
++) {
346 if (strcasecmp(list
[i
], s
) == 0) return true;
353 append one list to another - expanding list1
355 _PUBLIC_
const char **str_list_append(const char **list1
,
356 const char * const *list2
)
358 size_t len1
= str_list_length(list1
);
359 size_t len2
= str_list_length(list2
);
363 ret
= talloc_realloc(NULL
, list1
, const char *, len1
+len2
+1);
364 if (ret
== NULL
) return NULL
;
366 for (i
=len1
;i
<len1
+len2
;i
++) {
367 ret
[i
] = talloc_strdup(ret
, list2
[i
-len1
]);
368 if (ret
[i
] == NULL
) {
377 static int list_cmp(const char **el1
, const char **el2
)
379 return strcmp(*el1
, *el2
);
383 return a list that only contains the unique elements of a list,
384 removing any duplicates
386 _PUBLIC_
const char **str_list_unique(const char **list
)
388 size_t len
= str_list_length(list
);
394 list2
= (const char **)talloc_memdup(list
, list
,
395 sizeof(list
[0])*(len
+1));
396 qsort(list2
, len
, sizeof(list2
[0]), QSORT_CAST list_cmp
);
398 for (i
=j
=1;i
<len
;i
++) {
399 if (strcmp(list2
[i
], list
[j
-1]) != 0) {
405 list
= talloc_realloc(NULL
, list
, const char *, j
+ 1);
411 very useful when debugging complex list related code
413 _PUBLIC_
void str_list_show(const char **list
)
417 for (i
=0;list
&& list
[i
];i
++) {
418 DEBUG(0,("\"%s\", ", list
[i
]));
426 append one list to another - expanding list1
427 this assumes the elements of list2 are const pointers, so we can re-use them
429 _PUBLIC_
const char **str_list_append_const(const char **list1
,
432 size_t len1
= str_list_length(list1
);
433 size_t len2
= str_list_length(list2
);
437 ret
= talloc_realloc(NULL
, list1
, const char *, len1
+len2
+1);
438 if (ret
== NULL
) return NULL
;
440 for (i
=len1
;i
<len1
+len2
;i
++) {
441 ret
[i
] = list2
[i
-len1
];
449 add an entry to a string list
450 this assumes s will not change
452 _PUBLIC_
const char **str_list_add_const(const char **list
, const char *s
)
454 size_t len
= str_list_length(list
);
457 ret
= talloc_realloc(NULL
, list
, const char *, len
+2);
458 if (ret
== NULL
) return NULL
;
468 this assumes list will not change
470 _PUBLIC_
const char **str_list_copy_const(TALLOC_CTX
*mem_ctx
,
479 ret
= talloc_array(mem_ctx
, const char *, str_list_length(list
)+1);
483 for (i
=0;list
&& list
[i
];i
++) {