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 a null terminated list of strings from a input string and a
33 separator list. The separator list must contain characters less than
34 or equal to 0x2f for this to work correctly on multi-byte strings
36 _PUBLIC_
char **str_list_make(TALLOC_CTX
*mem_ctx
, const char *string
, const char *sep
)
45 ret
= talloc_array(mem_ctx
, char *, 1);
50 while (string
&& *string
) {
51 size_t len
= strcspn(string
, sep
);
55 string
+= strspn(string
, sep
);
59 ret2
= talloc_realloc(mem_ctx
, ret
, char *, num_elements
+2);
66 ret
[num_elements
] = talloc_strndup(ret
, string
, len
);
67 if (ret
[num_elements
] == NULL
) {
76 ret
[num_elements
] = NULL
;
82 * build a null terminated list of strings from an argv-like input string
83 * Entries are seperated by spaces and can be enclosed by quotes.
84 * Does NOT support escaping
86 _PUBLIC_
const char **str_list_make_shell(TALLOC_CTX
*mem_ctx
, const char *string
, const char *sep
)
89 const char **ret
= NULL
;
91 ret
= talloc_array(mem_ctx
, const char *, 1);
99 while (string
&& *string
) {
100 size_t len
= strcspn(string
, sep
);
105 string
+= strspn(string
, sep
);
109 if (*string
== '\"') {
111 len
= strcspn(string
, "\"");
112 element
= talloc_strndup(ret
, string
, len
);
115 element
= talloc_strndup(ret
, string
, len
);
119 if (element
== NULL
) {
124 ret2
= talloc_realloc(mem_ctx
, ret
, const char *, num_elements
+2);
131 ret
[num_elements
] = element
;
136 ret
[num_elements
] = NULL
;
143 * join a list back to one string
145 _PUBLIC_
char *str_list_join(TALLOC_CTX
*mem_ctx
, const char **list
, char seperator
)
151 return talloc_strdup(mem_ctx
, "");
153 ret
= talloc_strdup(mem_ctx
, list
[0]);
155 for (i
= 1; list
[i
]; i
++) {
156 ret
= talloc_asprintf_append_buffer(ret
, "%c%s", seperator
, list
[i
]);
162 /** join a list back to one (shell-like) string; entries
163 * seperated by spaces, using quotes where necessary */
164 _PUBLIC_
char *str_list_join_shell(TALLOC_CTX
*mem_ctx
, const char **list
, char sep
)
170 return talloc_strdup(mem_ctx
, "");
172 if (strchr(list
[0], ' ') || strlen(list
[0]) == 0)
173 ret
= talloc_asprintf(mem_ctx
, "\"%s\"", list
[0]);
175 ret
= talloc_strdup(mem_ctx
, list
[0]);
177 for (i
= 1; list
[i
]; i
++) {
178 if (strchr(list
[i
], ' ') || strlen(list
[i
]) == 0)
179 ret
= talloc_asprintf_append_buffer(ret
, "%c\"%s\"", sep
, list
[i
]);
181 ret
= talloc_asprintf_append_buffer(ret
, "%c%s", sep
, list
[i
]);
188 return the number of elements in a string list
190 _PUBLIC_
size_t str_list_length(const char **list
)
193 for (ret
=0;list
&& list
[ret
];ret
++) /* noop */ ;
201 _PUBLIC_
char **str_list_copy(TALLOC_CTX
*mem_ctx
, const char **list
)
209 ret
= talloc_array(mem_ctx
, char *, str_list_length(list
)+1);
213 for (i
=0;list
&& list
[i
];i
++) {
214 ret
[i
] = talloc_strdup(ret
, list
[i
]);
215 if (ret
[i
] == NULL
) {
225 Return true if all the elements of the list match exactly.
227 _PUBLIC_
bool str_list_equal(const char **list1
, const char **list2
)
231 if (list1
== NULL
|| list2
== NULL
) {
232 return (list1
== list2
);
235 for (i
=0;list1
[i
] && list2
[i
];i
++) {
236 if (strcmp(list1
[i
], list2
[i
]) != 0) {
240 if (list1
[i
] || list2
[i
]) {
248 add an entry to a string list
250 _PUBLIC_
char **str_list_add(char **list
, const char *s
)
252 size_t len
= str_list_length(list
);
255 ret
= talloc_realloc(NULL
, list
, char *, len
+2);
256 if (ret
== NULL
) return NULL
;
258 ret
[len
] = talloc_strdup(ret
, s
);
259 if (ret
[len
] == NULL
) return NULL
;
267 remove an entry from a string list
269 _PUBLIC_
void str_list_remove(const char **list
, const char *s
)
273 for (i
=0;list
[i
];i
++) {
274 if (strcmp(list
[i
], s
) == 0) break;
276 if (!list
[i
]) return;
285 return true if a string is in a list
287 _PUBLIC_
bool str_list_check(const char **list
, const char *s
)
291 for (i
=0;list
[i
];i
++) {
292 if (strcmp(list
[i
], s
) == 0) return true;
298 return true if a string is in a list, case insensitively
300 _PUBLIC_
bool str_list_check_ci(const char **list
, const char *s
)
304 for (i
=0;list
[i
];i
++) {
305 if (strcasecmp(list
[i
], s
) == 0) return true;
312 append one list to another - expanding list1
314 _PUBLIC_
char **str_list_append(char **list1
, const char **list2
)
316 size_t len1
= str_list_length(list1
);
317 size_t len2
= str_list_length(list2
);
321 ret
= talloc_realloc(NULL
, list1
, char *, len1
+len2
+1);
322 if (ret
== NULL
) return NULL
;
324 for (i
=len1
;i
<len1
+len2
;i
++) {
325 ret
[i
] = talloc_strdup(ret
, list2
[i
-len1
]);
326 if (ret
[i
] == NULL
) {
335 static int list_cmp(const char **el1
, const char **el2
)
337 return strcmp(*el1
, *el2
);
341 return a list that only contains the unique elements of a list,
342 removing any duplicates
344 _PUBLIC_
char **str_list_unique(char **list
)
346 size_t len
= str_list_length(list
);
352 list2
= (char **)talloc_memdup(list
, list
, sizeof(list
[0])*(len
+1));
353 qsort(list2
, len
, sizeof(list2
[0]), QSORT_CAST list_cmp
);
355 for (i
=j
=1;i
<len
;i
++) {
356 if (strcmp(list2
[i
], list
[j
-1]) != 0) {
362 list
= talloc_realloc(NULL
, list
, char *, j
);
368 very useful when debugging complex list related code
370 _PUBLIC_
void str_list_show(const char **list
)
374 for (i
=0;list
&& list
[i
];i
++) {
375 DEBUG(0,("\"%s\", ", list
[i
]));
383 append one list to another - expanding list1
384 this assumes the elements of list2 are const pointers, so we can re-use them
386 _PUBLIC_
char **str_list_append_const(char **list1
, const char **list2
)
388 size_t len1
= str_list_length(list1
);
389 size_t len2
= str_list_length(list2
);
393 ret
= talloc_realloc(NULL
, list1
, char *, len1
+len2
+1);
394 if (ret
== NULL
) return NULL
;
396 for (i
=len1
;i
<len1
+len2
;i
++) {
397 ret
[i
] = list2
[i
-len1
];
405 add an entry to a string list
406 this assumes s will not change
408 _PUBLIC_
char **str_list_add_const(char **list
, const char *s
)
410 size_t len
= str_list_length(list
);
413 ret
= talloc_realloc(NULL
, list
, char *, len
+2);
414 if (ret
== NULL
) return NULL
;
424 this assumes list will not change
426 _PUBLIC_
char **str_list_copy_const(TALLOC_CTX
*mem_ctx
, const char **list
)
434 ret
= talloc_array(mem_ctx
, char *, str_list_length(list
)+1);
438 for (i
=0;list
&& list
[i
];i
++) {