From 72bf14d3456373130cd47157c0062e9125318716 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Sat, 25 Nov 2017 09:29:54 +0100 Subject: [PATCH] util: Introduce virStringListMerge For two string lists merge one into the other one. Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/virstring.c | 36 ++++++++++++++++++++++++++++++++++++ src/util/virstring.h | 3 +++ 3 files changed, 40 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index a705fa846d..bc8cc1fba9 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2769,6 +2769,7 @@ virStringListGetFirstWithPrefix; virStringListHasString; virStringListJoin; virStringListLength; +virStringListMerge; virStringListRemove; virStringMatch; virStringParsePort; diff --git a/src/util/virstring.c b/src/util/virstring.c index 2ec9f3544b..15f367af7c 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -239,6 +239,42 @@ virStringListRemove(char ***strings, } +/** + * virStringListMerge: + * @dst: a NULL-terminated array of strings to expand + * @src: a NULL-terminated array of strings + * + * Merges @src into @dst. Upon successful return from this + * function, @dst is resized to $(dst + src) elements and @src is + * freed. + * + * Returns 0 on success, -1 otherwise. + */ +int +virStringListMerge(char ***dst, + char ***src) +{ + size_t dst_len, src_len, i; + + if (!src || !*src) + return 0; + + dst_len = virStringListLength((const char **) *dst); + src_len = virStringListLength((const char **) *src); + + if (VIR_REALLOC_N(*dst, dst_len + src_len + 1) < 0) + return -1; + + for (i = 0; i <= src_len; i++) + (*dst)[i + dst_len] = (*src)[i]; + + /* Don't call virStringListFree() as it would free strings in + * @src. */ + VIR_FREE(*src); + return 0; +} + + /** * virStringListCopy: * @dst: where to store the copy of @strings diff --git a/src/util/virstring.h b/src/util/virstring.h index 8af054bce7..fa2ec1df4d 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -46,6 +46,9 @@ char **virStringListAdd(const char **strings, void virStringListRemove(char ***strings, const char *item); +int virStringListMerge(char ***dst, + char ***src); + int virStringListCopy(char ***dst, const char **src); -- GitLab