Remove two unused variables
[ira/wip.git] / lib / util / util_strlist.c
index 86956289b446a683a4aeb5ccbb17c2b11ce8ac35..844e8f25dca1e878822266a81656c6e01e1321f2 100644 (file)
  * @brief String list manipulation
  */
 
+/**
+  build an empty (only NULL terminated) list of strings (for expansion with str_list_add() etc)
+*/
+_PUBLIC_ char **str_list_make_empty(TALLOC_CTX *mem_ctx)
+{
+       char **ret = NULL;
+
+       ret = talloc_array(mem_ctx, char *, 1);
+       if (ret == NULL) {
+               return NULL;
+       }
+
+       ret[0] = NULL;
+
+       return ret;
+}
+
+/**
+  place the only element 'entry' into a new, NULL terminated string list
+*/
+_PUBLIC_ char **str_list_make_single(TALLOC_CTX *mem_ctx, const char *entry)
+{
+       char **ret = NULL;
+
+       ret = talloc_array(mem_ctx, char *, 2);
+       if (ret == NULL) {
+               return NULL;
+       }
+
+       ret[0] = talloc_strdup(ret, entry);
+       if (!ret[0]) {
+               talloc_free(ret);
+               return NULL;
+       }
+       ret[1] = NULL;
+
+       return ret;
+}
+
 /**
   build a null terminated list of strings from a input string and a
   separator list. The separator list must contain characters less than
@@ -187,7 +226,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 * const *list)
 {
        size_t ret;
        for (ret=0;list && list[ret];ret++) /* noop */ ;
@@ -198,7 +237,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,40 +283,15 @@ _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)
+_PUBLIC_ const char **str_list_add(const char **list, const char *s)
 {
        size_t len = str_list_length(list);
-       char **ret;
+       const char **ret;
 
-       ret = talloc_realloc(NULL, list, char *, len+2);
+       ret = talloc_realloc(NULL, list, const char *, len+2);
        if (ret == NULL) return NULL;
 
        ret[len] = talloc_strdup(ret, s);
@@ -336,7 +350,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(const char **list1, const char * const *list2)
 {
        size_t len1 = str_list_length(list1);
        size_t len2 = str_list_length(list2);
@@ -356,3 +370,116 @@ _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_ const char **str_list_unique(const char **list)
+{
+       size_t len = str_list_length(list);
+       const char **list2;
+       int i, j;
+       if (len < 2) {
+               return list;
+       }
+       list2 = (const 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, const 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"));
+}
+
+
+
+/**
+  append one list to another - expanding list1
+  this assumes the elements of list2 are const pointers, so we can re-use them
+*/
+_PUBLIC_ const char **str_list_append_const(const char **list1,
+                                           const char **list2)
+{
+       size_t len1 = str_list_length(list1);
+       size_t len2 = str_list_length(list2);
+       const char **ret;
+       int i;
+
+       ret = talloc_realloc(NULL, list1, const char *, len1+len2+1);
+       if (ret == NULL) return NULL;
+
+       for (i=len1;i<len1+len2;i++) {
+               ret[i] = list2[i-len1];
+       }
+       ret[i] = NULL;
+
+       return ret;
+}
+
+/**
+  add an entry to a string list
+  this assumes s will not change
+*/
+_PUBLIC_ const char **str_list_add_const(const char **list, const char *s)
+{
+       size_t len = str_list_length(list);
+       const char **ret;
+
+       ret = talloc_realloc(NULL, list, const char *, len+2);
+       if (ret == NULL) return NULL;
+
+       ret[len] = s;
+       ret[len+1] = NULL;
+
+       return ret;
+}
+
+/**
+  copy a string list
+  this assumes list will not change
+*/
+_PUBLIC_ const char **str_list_copy_const(TALLOC_CTX *mem_ctx,
+                                         const char **list)
+{
+       int i;
+       const char **ret;
+
+       if (list == NULL)
+               return NULL;
+       
+       ret = talloc_array(mem_ctx, const char *, str_list_length(list)+1);
+       if (ret == NULL) 
+               return NULL;
+
+       for (i=0;list && list[i];i++) {
+               ret[i] = list[i];
+       }
+       ret[i] = NULL;
+       return ret;
+}