]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
list: Add helper to convert strings separated by ', ' to array
authorOsier Yang <jyang@redhat.com>
Tue, 4 Sep 2012 15:16:29 +0000 (23:16 +0800)
committerOsier Yang <jyang@redhat.com>
Thu, 6 Sep 2012 14:04:43 +0000 (22:04 +0800)
tools/virsh.c: New helper function vshStringToArray.
tools/virsh.h: Declare vshStringToArray.
tools/virsh-domain.c: use the helper in cmdUndefine.

tools/virsh-domain.c
tools/virsh.c
tools/virsh.h

index 4684466a4ae125579ae0140b0dfcb6d9bb1a60a9..63eb6c95b629ac53c4053d88f9894ed202ec433d 100644 (file)
@@ -2443,23 +2443,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,
index e3f9e4ec8dea518a5aab19433977bf6609cc9934..242f7892c16c4c958037e9d5291304e4423a5fef 100644 (file)
@@ -166,6 +166,50 @@ vshPrettyCapacity(unsigned long long val, const char **unit)
     }
 }
 
+/*
+ * 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.
+ */
+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;
+}
 
 virErrorPtr last_error;
 
index 8923f946bb74b7efc8483fc953cbcc3ea53f857a..2a9c6a21a0a29f474b60b9a7a2e2e05669c89a47 100644 (file)
@@ -328,6 +328,7 @@ int vshAskReedit(vshControl *ctl, const char *msg);
 int vshStreamSink(virStreamPtr st, const char *bytes, size_t nbytes,
                   void *opaque);
 double vshPrettyCapacity(unsigned long long val, const char **unit);
+int vshStringToArray(char *str, char ***array);
 
 /* Typedefs, function prototypes for job progress reporting.
  * There are used by some long lingering commands like