[libvirt] [PATCH 08/48] list: Add helper to convert strings separated by ', ' to array

Osier Yang jyang at redhat.com
Fri Aug 3 15:48:11 UTC 2012


tools/virsh.c: New helper function vshStringToArray.
tools/virsh-domain.c: use the helper in cmdUndefine.
---
 tools/virsh-domain.c |   19 ++-----------------
 tools/virsh.c        |   45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 17 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 33b1727..bf3abe1 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -2393,23 +2393,8 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
 
         /* tokenize the string from user and save it's parts into an array */
         if (volumes) {
-            /* count the delimiters */
-            volume_tok = volumes;
-            nvolume_tokens = 1; /* we need at least one member */
-            while (*volume_tok) {
-                if (*(volume_tok++) == ',')
-                    nvolume_tokens++;
-            }
-
-            volume_tokens = vshCalloc(ctl, nvolume_tokens, sizeof(char *));
-
-            /* tokenize the input string */
-            nvolume_tokens = 0;
-            volume_tok = volumes;
-            do {
-                volume_tokens[nvolume_tokens] = strsep(&volume_tok, ",");
-                nvolume_tokens++;
-            } while (volume_tok);
+            if ((nvolume_tokens = vshStringToArray(volumes, &volume_tokens)) < 0)
+                goto cleanup;
         }
 
         if ((nvolumes = virXPathNodeSet("./devices/disk", ctxt,
diff --git a/tools/virsh.c b/tools/virsh.c
index c57b229..a77b7ba 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -3170,6 +3170,51 @@ vshParseArgv(vshControl *ctl, int argc, char **argv)
     return true;
 }
 
+/*
+ * Convert the strings separated by ',' into array. The caller
+ * must free the returned array after use.
+ *
+ * Returns the length of the filled array on success, or -1
+ * on error.
+ */
+static int
+vshStringToArray(char *str,
+                 char ***array)
+{
+    char *str_tok = NULL;
+    unsigned int nstr_tokens = 0;
+    char **arr = NULL;
+
+    /* tokenize the string from user and save it's parts into an array */
+    if (str) {
+        nstr_tokens = 1;
+
+        /* count the delimiters */
+        str_tok = str;
+        while (*str_tok) {
+            if (*str_tok == ',')
+                nstr_tokens++;
+            str_tok++;
+        }
+
+        if (VIR_ALLOC_N(arr, nstr_tokens) < 0) {
+            virReportOOMError();
+            return -1;
+        }
+
+        /* tokenize the input string */
+        nstr_tokens = 0;
+        str_tok = str;
+        do {
+            arr[nstr_tokens] = strsep(&str_tok, ",");
+            nstr_tokens++;
+        } while (str_tok);
+    }
+
+    *array = arr;
+    return nstr_tokens;
+}
+
 #include "virsh-domain.c"
 #include "virsh-domain-monitor.c"
 #include "virsh-pool.c"
-- 
1.7.7.3




More information about the libvir-list mailing list