提交 ef04596d 编写于 作者: J Jiri Denemark

util: Introduce virStringListCopy

The API makes a deep copy of a NULL-terminated string list.
Signed-off-by: NJiri Denemark <jdenemar@redhat.com>
Reviewed-by: NJohn Ferlan <jferlan@redhat.com>
上级 02555bfe
......@@ -239,6 +239,45 @@ virStringListRemove(char ***strings,
}
/**
* virStringListCopy:
* @dst: where to store the copy of @strings
* @src: a NULL-terminated array of strings
*
* Makes a deep copy of the @src string list and stores it in @dst. Callers
* are responsible for freeing @dst.
*
* Returns 0 on success, -1 on error.
*/
int
virStringListCopy(char ***dst,
const char **src)
{
char **copy = NULL;
size_t i;
*dst = NULL;
if (!src)
return 0;
if (VIR_ALLOC_N(copy, virStringListLength(src) + 1) < 0)
goto error;
for (i = 0; src[i]; i++) {
if (VIR_STRDUP(copy[i], src[i]) < 0)
goto error;
}
*dst = copy;
return 0;
error:
virStringListFree(copy);
return -1;
}
/**
* virStringListFree:
* @str_array: a NULL-terminated array of strings to free
......
......@@ -46,6 +46,9 @@ char **virStringListAdd(const char **strings,
void virStringListRemove(char ***strings,
const char *item);
int virStringListCopy(char ***dst,
const char **src);
void virStringListFree(char **strings);
void virStringListFreeCount(char **strings,
size_t count);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册