]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: string: Return element count from virStringSplit
authorPeter Krempa <pkrempa@redhat.com>
Mon, 12 May 2014 07:46:37 +0000 (09:46 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 3 Jun 2014 07:27:24 +0000 (09:27 +0200)
To allow using the array manipulation macros on the arrays returned by
virStringSplit we need to know the count of the elements in the array.
Modify virStringSplit to return this value, rename it and add a helper
with the old name so that we don't need to update all the code.

src/libvirt_private.syms
src/util/virstring.c
src/util/virstring.h
tests/virstringtest.c

index e1702dbff3118303e57643cc5751bd9f4e7c7e05..4c1f61cefcd36d70bb51c6d8b4824eb4ad35db6d 100644 (file)
@@ -1908,6 +1908,7 @@ virStringSearch;
 virStringSortCompare;
 virStringSortRevCompare;
 virStringSplit;
+virStringSplitCount;
 virStrncpy;
 virStrndup;
 virStrToDouble;
index 7a8430e847da165595cf857817881ed7375d9a3a..6dcc7a8accc35804ba488985fd8054d3447196e2 100644 (file)
@@ -43,13 +43,15 @@ VIR_LOG_INIT("util.string");
  */
 
 /**
- * virStringSplit:
+ * virStringSplitCount:
  * @string: a string to split
  * @delim: a string which specifies the places at which to split
  *     the string. The delimiter is not included in any of the resulting
  *     strings, unless @max_tokens is reached.
  * @max_tokens: the maximum number of pieces to split @string into.
  *     If this is 0, the string is split completely.
+ * @tokcount: If provided, the value is set to the count of pieces the string
+ *            was split to excluding the terminating NULL element.
  *
  * Splits a string into a maximum of @max_tokens pieces, using the given
  * @delim. If @max_tokens is reached, the remainder of @string is
@@ -65,9 +67,11 @@ VIR_LOG_INIT("util.string");
  * Return value: a newly-allocated NULL-terminated array of strings. Use
  *    virStringFreeList() to free it.
  */
-char **virStringSplit(const char *string,
-                      const char *delim,
-                      size_t max_tokens)
+char **
+virStringSplitCount(const char *string,
+                    const char *delim,
+                    size_t max_tokens,
+                    size_t *tokcount)
 {
     char **tokens = NULL;
     size_t ntokens = 0;
@@ -109,6 +113,9 @@ char **virStringSplit(const char *string,
         goto error;
     tokens[ntokens++] = NULL;
 
+    if (tokcount)
+        *tokcount = ntokens - 1;
+
     return tokens;
 
  error:
@@ -119,6 +126,15 @@ char **virStringSplit(const char *string,
 }
 
 
+char **
+virStringSplit(const char *string,
+               const char *delim,
+               size_t max_tokens)
+{
+    return virStringSplitCount(string, delim, max_tokens, NULL);
+}
+
+
 /**
  * virStringJoin:
  * @strings: a NULL-terminated array of strings to join
index 1ed104682c39b413e5750aa03990211b83499c4a..0ab9d9633fcd69e2dc560ebe1e23e95fa945df8f 100644 (file)
 
 # include "internal.h"
 
+char **virStringSplitCount(const char *string,
+                           const char *delim,
+                           size_t max_tokens,
+                           size_t *tokcount)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
 char **virStringSplit(const char *string,
                       const char *delim,
                       size_t max_tokens)
index 96c3784a588b3a0ebe5e86d7615336fd634d9702..5277fc657af457d360dbad980271458ff52bd48b 100644 (file)
@@ -53,11 +53,14 @@ static int testSplit(const void *args)
 {
     const struct testSplitData *data = args;
     char **got;
+    size_t ntokens;
+    size_t exptokens = 0;
     char **tmp1;
     const char **tmp2;
     int ret = -1;
 
-    if (!(got = virStringSplit(data->string, data->delim, data->max_tokens))) {
+    if (!(got = virStringSplitCount(data->string, data->delim,
+                                    data->max_tokens, &ntokens))) {
         VIR_DEBUG("Got no tokens at all");
         return -1;
     }
@@ -71,6 +74,7 @@ static int testSplit(const void *args)
         }
         tmp1++;
         tmp2++;
+        exptokens++;
     }
     if (*tmp1) {
         virFilePrintf(stderr, "Too many pieces returned\n");
@@ -81,6 +85,14 @@ static int testSplit(const void *args)
         goto cleanup;
     }
 
+    if (ntokens != exptokens) {
+        virFilePrintf(stderr,
+                      "Returned token count (%zu) doesn't match "
+                      "expected count (%zu)",
+                      ntokens, exptokens);
+        goto cleanup;
+    }
+
     ret = 0;
  cleanup:
     virStringFreeList(got);