]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Introduce virStringListCopy
authorJiri Denemark <jdenemar@redhat.com>
Wed, 20 Sep 2017 08:41:35 +0000 (10:41 +0200)
committerJiri Denemark <jdenemar@redhat.com>
Mon, 16 Oct 2017 07:21:51 +0000 (09:21 +0200)
The API makes a deep copy of a NULL-terminated string list.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
src/util/virstring.c
src/util/virstring.h

index 0288d1e6772fb772afd3eb5e369eb8958f384ecc..eac4774b533ea20c9ffd122cf2ecbd6369335b83 100644 (file)
@@ -239,6 +239,45 @@ virStringListRemove(char ***strings,
 }
 
 
+/**
+ * virStringListCopy:
+ * @dst: where to store the copy of @strings
+ * @src: a NULL-terminated array of strings
+ *
+ * Makes a deep copy of the @src string list and stores it in @dst. Callers
+ * are responsible for freeing @dst.
+ *
+ * Returns 0 on success, -1 on error.
+ */
+int
+virStringListCopy(char ***dst,
+                  const char **src)
+{
+    char **copy = NULL;
+    size_t i;
+
+    *dst = NULL;
+
+    if (!src)
+        return 0;
+
+    if (VIR_ALLOC_N(copy, virStringListLength(src) + 1) < 0)
+        goto error;
+
+    for (i = 0; src[i]; i++) {
+        if (VIR_STRDUP(copy[i], src[i]) < 0)
+            goto error;
+    }
+
+    *dst = copy;
+    return 0;
+
+ error:
+    virStringListFree(copy);
+    return -1;
+}
+
+
 /**
  * virStringListFree:
  * @str_array: a NULL-terminated array of strings to free
index 1290fcce15c8123b2cda3670f7c73152dcda14dd..cfd91be314f18ef4dd7f37d741abcccb48bfb829 100644 (file)
@@ -46,6 +46,9 @@ char **virStringListAdd(const char **strings,
 void virStringListRemove(char ***strings,
                          const char *item);
 
+int virStringListCopy(char ***dst,
+                      const char **src);
+
 void virStringListFree(char **strings);
 void virStringListFreeCount(char **strings,
                             size_t count);