]> xenbits.xensource.com Git - libvirt.git/commitdiff
virstring: Introduce virStringListRemove
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 28 Nov 2016 13:38:58 +0000 (14:38 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 6 Dec 2016 12:33:18 +0000 (13:33 +0100)
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
src/libvirt_private.syms
src/util/virstring.c
src/util/virstring.h
tests/virstringtest.c

index bc6588969ad5120984ecb07317e07f84f47274b5..3d4da7356265eb91d8b072364cc668695c1ae397 100644 (file)
@@ -2467,6 +2467,7 @@ virStringListGetFirstWithPrefix;
 virStringListHasString;
 virStringListJoin;
 virStringListLength;
+virStringListRemove;
 virStringReplace;
 virStringSearch;
 virStringSortCompare;
index 629f8ca32e0388b939340747f92185f463aed171..69abc267bfdb50e74db68f63c1a8a64f711855ab 100644 (file)
@@ -202,6 +202,41 @@ virStringListAdd(const char **strings,
 }
 
 
+/**
+ * virStringListRemove:
+ * @strings: a NULL-terminated array of strings
+ * @item: string to remove
+ *
+ * Remove every occurrence of @item in list of @strings.
+ */
+void
+virStringListRemove(char ***strings,
+                    const char *item)
+{
+    size_t r, w = 0;
+
+    if (!strings || !*strings)
+        return;
+
+    for (r = 0; (*strings)[r]; r++) {
+        if (STREQ((*strings)[r], item)) {
+            VIR_FREE((*strings)[r]);
+            continue;
+        }
+        if (r != w)
+            (*strings)[w] = (*strings)[r];
+        w++;
+    }
+
+    if (w == 0) {
+        VIR_FREE(*strings);
+    } else {
+        (*strings)[w] = NULL;
+        ignore_value(VIR_REALLOC_N(*strings, w + 1));
+    }
+}
+
+
 /**
  * virStringListFree:
  * @str_array: a NULL-terminated array of strings to free
index da9d35ccad6490e2464157e4ce653c10c36443dd..a5550e30d2e2d7b8d117daaa5b1a3e5db4a802dd 100644 (file)
@@ -43,6 +43,8 @@ char *virStringListJoin(const char **strings,
 
 char **virStringListAdd(const char **strings,
                         const char *item);
+void virStringListRemove(char ***strings,
+                         const char *item);
 
 void virStringListFree(char **strings);
 void virStringListFreeCount(char **strings,
index 43657c84c5e1366298a8041cd06b5f4424d8ea31..1d660b79848846eb063229f10bb945889f570f6f 100644 (file)
@@ -162,6 +162,40 @@ static int testAdd(const void *args)
 }
 
 
+static int testRemove(const void *args)
+{
+    const struct testSplitData *data = args;
+    char **list = NULL;
+    size_t ntokens;
+    size_t i;
+    int ret = -1;
+
+    if (!(list = virStringSplitCount(data->string, data->delim,
+                                     data->max_tokens, &ntokens))) {
+        VIR_DEBUG("Got no tokens at all");
+        return -1;
+    }
+
+    for (i = 0; data->tokens[i]; i++) {
+        virStringListRemove(&list, data->tokens[i]);
+        if (virStringListHasString((const char **) list, data->tokens[i])) {
+            virFilePrintf(stderr, "Not removed %s", data->tokens[i]);
+            goto cleanup;
+        }
+    }
+
+    if (list && list[0]) {
+        virFilePrintf(stderr, "Not removed all tokens: %s", list[0]);
+        goto cleanup;
+    }
+
+    ret = 0;
+ cleanup:
+    virStringListFree(list);
+    return ret;
+}
+
+
 static bool fail;
 
 static const char *
@@ -636,6 +670,8 @@ mymain(void)
             ret = -1;                                                   \
         if (virTestRun("Add " #str, testAdd, &joinData) < 0)            \
             ret = -1;                                                   \
+        if (virTestRun("Remove " #str, testRemove, &splitData) < 0)     \
+            ret = -1;                                                   \
     } while (0)
 
     const char *tokens1[] = { NULL };