From 4f69d7067da6a8ee88950ab15aaf5a5849574fdd Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 9 Apr 2009 13:44:27 +1000 Subject: added str_list_unique() and str_list_show() I also undid some of the const changes from Andrew, as they didn't in fact resolve the const warnings. --- lib/util/util.h | 21 +++++++++----- lib/util/util_strlist.c | 77 +++++++++++++++++++++++++++++++------------------ 2 files changed, 62 insertions(+), 36 deletions(-) diff --git a/lib/util/util.h b/lib/util/util.h index bb07180872..78fc87ebe1 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -422,12 +422,12 @@ _PUBLIC_ char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char /** return the number of elements in a string list */ -_PUBLIC_ size_t str_list_length(const char * const *list); +_PUBLIC_ size_t str_list_length(const char **list); /** copy a string list */ -_PUBLIC_ char **str_list_copy(TALLOC_CTX *mem_ctx, const char * const *list); +_PUBLIC_ char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list); /** Return true if all the elements of the list match exactly. @@ -439,11 +439,6 @@ _PUBLIC_ bool str_list_equal(const char **list1, const char **list2); */ _PUBLIC_ char **str_list_add(char **list, const char *s); -/** - add an entry to a string list (if not already in there) -*/ -_PUBLIC_ char **str_list_add_unique(char **list, const char *s); - /** remove an entry from a string list */ @@ -461,7 +456,17 @@ _PUBLIC_ bool str_list_check_ci(const char **list, const char *s); /** append one list to another - expanding list1 */ -_PUBLIC_ char **str_list_append(char **list1, const char * const *list2); +_PUBLIC_ char **str_list_append(char **list1, const char **list2); + +/** + remove duplicate elements from a list +*/ +_PUBLIC_ char **str_list_unique(char **list); + +/* + very useful when debugging complex list related code + */ +_PUBLIC_ void str_list_show(const char **list); /* The following definitions come from lib/util/util_file.c */ diff --git a/lib/util/util_strlist.c b/lib/util/util_strlist.c index 86956289b4..29556b14d0 100644 --- a/lib/util/util_strlist.c +++ b/lib/util/util_strlist.c @@ -187,7 +187,7 @@ _PUBLIC_ char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char /** return the number of elements in a string list */ -_PUBLIC_ size_t str_list_length(const char * const*list) +_PUBLIC_ size_t str_list_length(const char **list) { size_t ret; for (ret=0;list && list[ret];ret++) /* noop */ ; @@ -198,7 +198,7 @@ _PUBLIC_ size_t str_list_length(const char * const*list) /** copy a string list */ -_PUBLIC_ char **str_list_copy(TALLOC_CTX *mem_ctx, const char * const *list) +_PUBLIC_ char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list) { int i; char **ret; @@ -244,31 +244,6 @@ _PUBLIC_ bool str_list_equal(const char **list1, const char **list2) } -/** - add an entry to a string list if not already in there -*/ -_PUBLIC_ char **str_list_add_unique(char **list, const char *s) -{ - int i; - size_t len; - const char **ret; - - for (i=0;list[i];i++) { - if (strcmp(list[i], s) == 0) return list; - } - - len = i; - ret = talloc_realloc(NULL, list, const char *, len+2); - if (ret == NULL) return NULL; - - ret[len] = talloc_strdup(ret, s); - if (ret[len] == NULL) return NULL; - - ret[len+1] = NULL; - - return ret; -} - /** add an entry to a string list */ @@ -336,7 +311,7 @@ _PUBLIC_ bool str_list_check_ci(const char **list, const char *s) /** append one list to another - expanding list1 */ -_PUBLIC_ char **str_list_append(char **list1, const char * const *list2) +_PUBLIC_ char **str_list_append(char **list1, const char **list2) { size_t len1 = str_list_length(list1); size_t len2 = str_list_length(list2); @@ -356,3 +331,49 @@ _PUBLIC_ char **str_list_append(char **list1, const char * const *list2) return ret; } + +static int list_cmp(const char **el1, const char **el2) +{ + return strcmp(*el1, *el2); +} + +/* + return a list that only contains the unique elements of a list, + removing any duplicates + */ +_PUBLIC_ char **str_list_unique(char **list) +{ + size_t len = str_list_length(list); + char **list2; + int i, j; + if (len < 2) { + return list; + } + list2 = (char **)talloc_memdup(list, list, sizeof(list[0])*(len+1)); + qsort(list2, len, sizeof(list2[0]), QSORT_CAST list_cmp); + list[0] = list2[0]; + for (i=j=1;i