virstring: Introduce virStringListRemove

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Michal Privoznik 2016-11-28 14:38:58 +01:00
parent ec38d6f741
commit b379c44c35
4 changed files with 74 additions and 0 deletions

View File

@ -2467,6 +2467,7 @@ virStringListGetFirstWithPrefix;
virStringListHasString;
virStringListJoin;
virStringListLength;
virStringListRemove;
virStringReplace;
virStringSearch;
virStringSortCompare;

View File

@ -202,6 +202,41 @@ virStringListAdd(const char **strings,
}
/**
* virStringListRemove:
* @strings: a NULL-terminated array of strings
* @item: string to remove
*
* Remove every occurrence of @item in list of @strings.
*/
void
virStringListRemove(char ***strings,
const char *item)
{
size_t r, w = 0;
if (!strings || !*strings)
return;
for (r = 0; (*strings)[r]; r++) {
if (STREQ((*strings)[r], item)) {
VIR_FREE((*strings)[r]);
continue;
}
if (r != w)
(*strings)[w] = (*strings)[r];
w++;
}
if (w == 0) {
VIR_FREE(*strings);
} else {
(*strings)[w] = NULL;
ignore_value(VIR_REALLOC_N(*strings, w + 1));
}
}
/**
* virStringListFree:
* @str_array: a NULL-terminated array of strings to free

View File

@ -43,6 +43,8 @@ char *virStringListJoin(const char **strings,
char **virStringListAdd(const char **strings,
const char *item);
void virStringListRemove(char ***strings,
const char *item);
void virStringListFree(char **strings);
void virStringListFreeCount(char **strings,

View File

@ -162,6 +162,40 @@ static int testAdd(const void *args)
}
static int testRemove(const void *args)
{
const struct testSplitData *data = args;
char **list = NULL;
size_t ntokens;
size_t i;
int ret = -1;
if (!(list = virStringSplitCount(data->string, data->delim,
data->max_tokens, &ntokens))) {
VIR_DEBUG("Got no tokens at all");
return -1;
}
for (i = 0; data->tokens[i]; i++) {
virStringListRemove(&list, data->tokens[i]);
if (virStringListHasString((const char **) list, data->tokens[i])) {
virFilePrintf(stderr, "Not removed %s", data->tokens[i]);
goto cleanup;
}
}
if (list && list[0]) {
virFilePrintf(stderr, "Not removed all tokens: %s", list[0]);
goto cleanup;
}
ret = 0;
cleanup:
virStringListFree(list);
return ret;
}
static bool fail;
static const char *
@ -636,6 +670,8 @@ mymain(void)
ret = -1; \
if (virTestRun("Add " #str, testAdd, &joinData) < 0) \
ret = -1; \
if (virTestRun("Remove " #str, testRemove, &splitData) < 0) \
ret = -1; \
} while (0)
const char *tokens1[] = { NULL };