*/
/**
- * 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
* 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;
goto error;
tokens[ntokens++] = NULL;
+ if (tokcount)
+ *tokcount = ntokens - 1;
+
return tokens;
error:
}
+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
# 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)
{
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;
}
}
tmp1++;
tmp2++;
+ exptokens++;
}
if (*tmp1) {
virFilePrintf(stderr, "Too many pieces returned\n");
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);