mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-22 11:22:23 +00:00
util: Introduce virStringListCopy
The API makes a deep copy of a NULL-terminated string list. Signed-off-by: Jiri Denemark <jdenemar@redhat.com> Reviewed-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
parent
02555bfe5c
commit
ef04596d76
@ -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:
|
* virStringListFree:
|
||||||
* @str_array: a NULL-terminated array of strings to free
|
* @str_array: a NULL-terminated array of strings to free
|
||||||
|
@ -46,6 +46,9 @@ char **virStringListAdd(const char **strings,
|
|||||||
void virStringListRemove(char ***strings,
|
void virStringListRemove(char ***strings,
|
||||||
const char *item);
|
const char *item);
|
||||||
|
|
||||||
|
int virStringListCopy(char ***dst,
|
||||||
|
const char **src);
|
||||||
|
|
||||||
void virStringListFree(char **strings);
|
void virStringListFree(char **strings);
|
||||||
void virStringListFreeCount(char **strings,
|
void virStringListFreeCount(char **strings,
|
||||||
size_t count);
|
size_t count);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user