diff options
author | Andrew Tridgell <tridge@samba.org> | 2009-04-09 13:44:27 +1000 |
---|---|---|
committer | Andrew Tridgell <tridge@samba.org> | 2009-04-09 13:44:27 +1000 |
commit | 4f69d7067da6a8ee88950ab15aaf5a5849574fdd (patch) | |
tree | 6ef5f318b242f935393cc212e66662d5f14d20ad /lib | |
parent | db29383797e10496dab16fc4729272dcfe090858 (diff) | |
download | samba-4f69d7067da6a8ee88950ab15aaf5a5849574fdd.tar.gz samba-4f69d7067da6a8ee88950ab15aaf5a5849574fdd.tar.bz2 samba-4f69d7067da6a8ee88950ab15aaf5a5849574fdd.zip |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/util/util.h | 21 | ||||
-rw-r--r-- | 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. @@ -440,11 +440,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 */ _PUBLIC_ void str_list_remove(const char **list, const char *s); @@ -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; @@ -245,31 +245,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 */ _PUBLIC_ char **str_list_add(char **list, const char *s) @@ -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<len;i++) { + if (strcmp(list2[i], list[j-1]) != 0) { + list[j] = list2[i]; + j++; + } + } + list[j] = NULL; + list = talloc_realloc(NULL, list, char *, j); + talloc_free(list2); + return list; +} + +/* + very useful when debugging complex list related code + */ +_PUBLIC_ void str_list_show(const char **list) +{ + int i; + DEBUG(0,("{ ")); + for (i=0;list && list[i];i++) { + DEBUG(0,("\"%s\", ", list[i])); + } + DEBUG(0,("}\n")); +} + |