[PATCHv2 21/33] util: string: Return element count from virStringSplit

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 |  1 +
 src/util/virstring.c     | 24 ++++++++++++++++++++----
 src/util/virstring.h     |  6 ++++++
 3 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 2b2bc10..ee745ea 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1908,6 +1908,7 @@ virStringSearch;
 virStringSortCompare;
 virStringSortRevCompare;
 virStringSplit;
+virStringSplitCount;
 virStrncpy;
 virStrndup;
 virStrToDouble;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 7a8430e..6dcc7a8 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -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
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 1ed1046..0ab9d96 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -26,6 +26,12 @@

 # 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)
-- 
1.9.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]