virstring: Introduce virStringListAdd

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Michal Privoznik 2016-11-25 15:43:53 +01:00
parent 1ef3ac82a3
commit ec38d6f741
4 changed files with 80 additions and 0 deletions

View File

@ -2460,6 +2460,7 @@ virStringEncodeBase64;
virStringHasControlChars; virStringHasControlChars;
virStringIsEmpty; virStringIsEmpty;
virStringIsPrintable; virStringIsPrintable;
virStringListAdd;
virStringListFree; virStringListFree;
virStringListFreeCount; virStringListFreeCount;
virStringListGetFirstWithPrefix; virStringListGetFirstWithPrefix;

View File

@ -168,6 +168,40 @@ char *virStringListJoin(const char **strings,
} }
/**
* virStringListAdd:
* @strings: a NULL-terminated array of strings
* @item: string to add
*
* Creates new strings list with all strings duplicated and @item
* at the end of the list. Callers is responsible for freeing
* both @strings and returned list.
*/
char **
virStringListAdd(const char **strings,
const char *item)
{
char **ret = NULL;
size_t i = virStringListLength(strings);
if (VIR_ALLOC_N(ret, i + 2) < 0)
goto error;
for (i = 0; strings && strings[i]; i++) {
if (VIR_STRDUP(ret[i], strings[i]) < 0)
goto error;
}
if (VIR_STRDUP(ret[i], item) < 0)
goto error;
return ret;
error:
virStringListFree(ret);
return NULL;
}
/** /**
* virStringListFree: * virStringListFree:
* @str_array: a NULL-terminated array of strings to free * @str_array: a NULL-terminated array of strings to free

View File

@ -41,6 +41,9 @@ char *virStringListJoin(const char **strings,
const char *delim) const char *delim)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
char **virStringListAdd(const char **strings,
const char *item);
void virStringListFree(char **strings); void virStringListFree(char **strings);
void virStringListFreeCount(char **strings, void virStringListFreeCount(char **strings,
size_t count); size_t count);

View File

@ -122,6 +122,46 @@ static int testJoin(const void *args)
return ret; return ret;
} }
static int testAdd(const void *args)
{
const struct testJoinData *data = args;
char **list = NULL;
char *got = NULL;
int ret = -1;
size_t i;
for (i = 0; data->tokens[i]; i++) {
char **tmp = virStringListAdd((const char **)list, data->tokens[i]);
if (!tmp)
goto cleanup;
virStringListFree(list);
list = tmp;
tmp = NULL;
}
if (!list &&
VIR_ALLOC(list) < 0)
goto cleanup;
if (!(got = virStringListJoin((const char **)list, data->delim))) {
VIR_DEBUG("Got no result");
goto cleanup;
}
if (STRNEQ(got, data->string)) {
virFilePrintf(stderr, "Mismatch '%s' vs '%s'\n", got, data->string);
goto cleanup;
}
ret = 0;
cleanup:
virStringListFree(list);
VIR_FREE(got);
return ret;
}
static bool fail; static bool fail;
static const char * static const char *
@ -594,6 +634,8 @@ mymain(void)
ret = -1; \ ret = -1; \
if (virTestRun("Join " #str, testJoin, &joinData) < 0) \ if (virTestRun("Join " #str, testJoin, &joinData) < 0) \
ret = -1; \ ret = -1; \
if (virTestRun("Add " #str, testAdd, &joinData) < 0) \
ret = -1; \
} while (0) } while (0)
const char *tokens1[] = { NULL }; const char *tokens1[] = { NULL };