From: Pavel Boldin <pboldin@xxxxxxxxxxxx> The `virTypedParamsAddStringList' function provides interface to add a NULL-terminated array of string values as a multi-value to the params. Signed-off-by: Pavel Boldin <pboldin@xxxxxxxxxxxx> Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- include/libvirt/libvirt-host.h | 6 ++++++ src/libvirt_public.syms | 1 + src/util/virtypedparam.c | 36 ++++++++++++++++++++++++++++++++++++ tests/virtypedparamtest.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) diff --git a/include/libvirt/libvirt-host.h b/include/libvirt/libvirt-host.h index 6767527..40a2768 100644 --- a/include/libvirt/libvirt-host.h +++ b/include/libvirt/libvirt-host.h @@ -337,6 +337,12 @@ virTypedParamsAddString (virTypedParameterPtr *params, const char *name, const char *value); int +virTypedParamsAddStringList(virTypedParameterPtr *params, + int *nparams, + int *maxparams, + const char *name, + const char **values); +int virTypedParamsAddFromString(virTypedParameterPtr *params, int *nparams, int *maxparams, diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms index 845a0b8..35e6853 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -715,6 +715,7 @@ LIBVIRT_1.2.16 { virDomainSetUserPassword; virTypedParamsFilter; virTypedParamsPickStrings; + virTypedParamsAddStringList; } LIBVIRT_1.2.15; # .... define new API here using predicted next version number .... diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c index d88d4a7..1916f1f 100644 --- a/src/util/virtypedparam.c +++ b/src/util/virtypedparam.c @@ -1197,6 +1197,42 @@ virTypedParamsAddString(virTypedParameterPtr *params, return -1; } +/** + * virTypedParamsAddStringList: + * @params: array of typed parameters + * @nparams: number of parameters in the @params array + * @maxparams: maximum number of parameters that can be stored in @params + * array without allocating more memory + * @name: name of the parameter to store values to + * @values: the values to store into the new parameters + * + * Packs NULL-terminated list of strings @values into @params under the + * key @name. + * + * Returns 0 on success, -1 on error. + */ +int +virTypedParamsAddStringList(virTypedParameterPtr *params, + int *nparams, + int *maxparams, + const char *name, + const char **values) +{ + size_t i; + int rv = -1; + + if (!values) + return 0; + + for (i = 0; values[i]; i++) { + if ((rv = virTypedParamsAddString(params, nparams, maxparams, + name, values[i])) < 0) + break; + } + + return rv; +} + /** * virTypedParamsAddFromString: diff --git a/tests/virtypedparamtest.c b/tests/virtypedparamtest.c index abdddd5..82a3951 100644 --- a/tests/virtypedparamtest.c +++ b/tests/virtypedparamtest.c @@ -126,6 +126,31 @@ testTypedParamsFilter(const void *opaque ATTRIBUTE_UNUSED) } static int +testTypedParamsAddStringList(const void *opaque ATTRIBUTE_UNUSED) +{ + int rv = 0; + virTypedParameterPtr params = NULL; + int nparams = 0, maxparams = 0, i; + + const char *values[] = { + "foo", "bar", "foobar", NULL + }; + + rv = virTypedParamsAddStringList(¶ms, &nparams, &maxparams, "param", + values); + + for (i = 0; i < nparams; i++) { + if (STRNEQ(params[i].field, "param") || + STRNEQ(params[i].value.s, values[i]) || + params[i].type != VIR_TYPED_PARAM_STRING) + rv = -1; + } + + virTypedParamsFree(params, nparams); + return rv; +} + +static int testTypedParamsPickStrings(const void *opaque ATTRIBUTE_UNUSED) { size_t i, picked; @@ -258,6 +283,9 @@ mymain(void) if (virtTestRun("Picking Strings", testTypedParamsPickStrings, NULL) < 0) rv = -1; + if (virtTestRun("Add string list", testTypedParamsAddStringList, NULL) < 0) + rv = -1; + if (rv < 0) return EXIT_FAILURE; return EXIT_SUCCESS; -- 2.3.6 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list