[libvirt] [PATCH v4 6/9] util: virTypedParams{Filter, GetAllStrings}
Add multikey API: * virTypedParamsFilter that filters all the parameters with specified name. * virTypedParamsGetAllStrings that returns a list with all the values for specified name and string type. Signed-off-by: Pavel Boldin Signed-off-by: Michal Privoznik --- include/libvirt/libvirt-host.h | 5 ++ src/libvirt_public.syms| 5 ++ src/util/virtypedparam.c | 102 + src/util/virtypedparam.h | 9 tests/Makefile.am | 2 +- tests/virtypedparamtest.c | 100 6 files changed, 222 insertions(+), 1 deletion(-) diff --git a/include/libvirt/libvirt-host.h b/include/libvirt/libvirt-host.h index 070550b..8222cfb 100644 --- a/include/libvirt/libvirt-host.h +++ b/include/libvirt/libvirt-host.h @@ -284,6 +284,11 @@ virTypedParamsGetString (virTypedParameterPtr params, const char *name, const char **value); int +virTypedParamsGetAllStrings(virTypedParameterPtr params, + int nparams, + const char *name, + const char ***values); +int virTypedParamsAddInt(virTypedParameterPtr *params, int *nparams, int *maxparams, diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms index 716dd2f..0a1feea 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -715,4 +715,9 @@ LIBVIRT_1.2.16 { virDomainSetUserPassword; } LIBVIRT_1.2.15; +LIBVIRT_1.3.0 { +global: +virTypedParamsGetAllStrings; +} LIBVIRT_1.2.16; + # define new API here using predicted next version number diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c index 6f608d6..a12006c 100644 --- a/src/util/virtypedparam.c +++ b/src/util/virtypedparam.c @@ -482,6 +482,51 @@ virTypedParamsGet(virTypedParameterPtr params, } +/** + * virTypedParamsFilter: + * @params: array of typed parameters + * @nparams: number of parameters in the @params array + * @name: name of the parameter to find + * @ret: pointer to the returned array + * + * Filters @params retaining only the parameters named @name in the + * resulting array @ret. Caller should free the @ret array but not + * the items since they are pointing to the @params elements. + * + * Returns amount of elements in @ret on success, -1 on error. + */ +int +virTypedParamsFilter(virTypedParameterPtr params, + int nparams, + const char *name, + virTypedParameterPtr **ret) +{ +size_t i, alloc = 0, n = 0; + +virCheckNonNullArgGoto(params, error); +virCheckNonNullArgGoto(name, error); +virCheckNonNullArgGoto(ret, error); + +*ret = NULL; + +for (i = 0; i < nparams; i++) { +if (STREQ(params[i].field, name)) { +if (VIR_RESIZE_N(*ret, alloc, n, 1) < 0) +goto error; + +(*ret)[n] = ¶ms[i]; + +n++; +} +} + +return n; + + error: +return -1; +} + + #define VIR_TYPED_PARAM_CHECK_TYPE(check_type) \ do { if (param->type != check_type) { \ virReportError(VIR_ERR_INVALID_ARG, \ @@ -750,6 +795,63 @@ virTypedParamsGetString(virTypedParameterPtr params, /** + * virTypedParamsGetAllStrings: + * @params: array of typed parameters + * @nparams: number of parameters in the @params array + * @name: name of the parameter to find + * @values: array of returned values + * + * Finds all parameters with desired @name within @params and + * store their values into @values. The @values array is self + * allocated and its length is stored into @picked. When no + * longer needed, caller should free the returned array, but not + * the items since they are taken from @params array. + * + * Returns amount of strings in @values array on success, + * -1 otherwise. + */ +int +virTypedParamsGetAllStrings(virTypedParameterPtr params, +int nparams, +const char *name, +const char ***values) +{ +size_t i, n; +int nfiltered; +virTypedParameterPtr *filtered = NULL; + +virResetLastError(); + +virCheckNonNullArgGoto(values, error); +*values = NULL; + +nfiltered = virTypedParamsFilter(params, nparams, name, &filtered); + +if (nfiltered < 0) +goto error; + +if (nfiltered && +VIR_ALLOC_N(*values, nfiltered) < 0) +goto error; + +for (n = 0, i = 0; i < nfiltered; i++) { +if (filtered[i]->type == VIR_TYPED_PARAM_STRING) +(*values)[n++] = filtered[i]->value.s; +} + +VIR_FREE(filtered); +return n; + + error: +if (values) +VIR_FREE(*values); +VIR_FREE(filtered); +virDispatchError(NULL); +return -1; +} + + +/** * virTypedParamsAdd
Re: [libvirt] [PATCH v4 6/9] util: virTypedParams{Filter, GetAllStrings}
On 16.06.2015 00:42, Pavel Boldin wrote: > Add multikey API: > > * virTypedParamsFilter that filters all the parameters with specified name. > * virTypedParamsGetAllStrings that returns a list with all the values for >specified name and string type. > > Signed-off-by: Pavel Boldin > Signed-off-by: Michal Privoznik > --- > include/libvirt/libvirt-host.h | 5 ++ > src/libvirt_public.syms| 5 ++ > src/util/virtypedparam.c | 102 > + > src/util/virtypedparam.h | 9 > tests/Makefile.am | 2 +- > tests/virtypedparamtest.c | 100 > 6 files changed, 222 insertions(+), 1 deletion(-) > > diff --git a/include/libvirt/libvirt-host.h b/include/libvirt/libvirt-host.h > index 070550b..8222cfb 100644 > --- a/include/libvirt/libvirt-host.h > +++ b/include/libvirt/libvirt-host.h > @@ -284,6 +284,11 @@ virTypedParamsGetString (virTypedParameterPtr params, > const char *name, > const char **value); > int > +virTypedParamsGetAllStrings(virTypedParameterPtr params, > + int nparams, > + const char *name, > + const char ***values); > +int > virTypedParamsAddInt(virTypedParameterPtr *params, > int *nparams, > int *maxparams, > diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms > index 716dd2f..0a1feea 100644 > --- a/src/libvirt_public.syms > +++ b/src/libvirt_public.syms > @@ -715,4 +715,9 @@ LIBVIRT_1.2.16 { > virDomainSetUserPassword; > } LIBVIRT_1.2.15; > > +LIBVIRT_1.3.0 { > +global: > +virTypedParamsGetAllStrings; > +} LIBVIRT_1.2.16; > + I don't think this symbol needs to be exported. > # define new API here using predicted next version number > diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c > index 6f608d6..a12006c 100644 > --- a/src/util/virtypedparam.c > +++ b/src/util/virtypedparam.c > @@ -482,6 +482,51 @@ virTypedParamsGet(virTypedParameterPtr params, > } > > > +/** > + * virTypedParamsFilter: > + * @params: array of typed parameters > + * @nparams: number of parameters in the @params array > + * @name: name of the parameter to find > + * @ret: pointer to the returned array > + * > + * Filters @params retaining only the parameters named @name in the > + * resulting array @ret. Caller should free the @ret array but not > + * the items since they are pointing to the @params elements. > + * > + * Returns amount of elements in @ret on success, -1 on error. > + */ > +int > +virTypedParamsFilter(virTypedParameterPtr params, > + int nparams, > + const char *name, > + virTypedParameterPtr **ret) > +{ > +size_t i, alloc = 0, n = 0; > + > +virCheckNonNullArgGoto(params, error); > +virCheckNonNullArgGoto(name, error); > +virCheckNonNullArgGoto(ret, error); > + > +*ret = NULL; > + > +for (i = 0; i < nparams; i++) { > +if (STREQ(params[i].field, name)) { > +if (VIR_RESIZE_N(*ret, alloc, n, 1) < 0) > +goto error; > + > +(*ret)[n] = ¶ms[i]; > + > +n++; > +} > +} > + > +return n; > + > + error: > +return -1; > +} > + > + > #define VIR_TYPED_PARAM_CHECK_TYPE(check_type) \ > do { if (param->type != check_type) { \ > virReportError(VIR_ERR_INVALID_ARG, \ > @@ -750,6 +795,63 @@ virTypedParamsGetString(virTypedParameterPtr params, > > > /** > + * virTypedParamsGetAllStrings: > + * @params: array of typed parameters > + * @nparams: number of parameters in the @params array > + * @name: name of the parameter to find > + * @values: array of returned values > + * > + * Finds all parameters with desired @name within @params and > + * store their values into @values. The @values array is self > + * allocated and its length is stored into @picked. When no > + * longer needed, caller should free the returned array, but not > + * the items since they are taken from @params array. > + * > + * Returns amount of strings in @values array on success, > + * -1 otherwise. > + */ > +int > +virTypedParamsGetAllStrings(virTypedParameterPtr params, > +int nparams, > +const char *name, > +const char ***values) > +{ > +size_t i, n; > +int nfiltered; > +virTypedParameterPtr *filtered = NULL; > + > +virResetLastError(); > + > +virCheckNonNullArgGoto(values, error); > +*values = NULL; > + > +nfiltered = virTypedParamsFilter(params, nparams, name, &filtered); > + > +if (nfiltered < 0) > +goto error; > + > +if (nfiltered && > +VIR_ALLOC_N(*values, nfiltered) < 0) > +
Re: [libvirt] [PATCH v4 6/9] util: virTypedParams{Filter, GetAllStrings}
On Tue, Jun 16, 2015 at 17:44:11 +0200, Michal Privoznik wrote: > On 16.06.2015 00:42, Pavel Boldin wrote: > > Add multikey API: > > > > * virTypedParamsFilter that filters all the parameters with specified name. > > * virTypedParamsGetAllStrings that returns a list with all the values for > >specified name and string type. > > > > Signed-off-by: Pavel Boldin > > Signed-off-by: Michal Privoznik > > --- > > include/libvirt/libvirt-host.h | 5 ++ > > src/libvirt_public.syms| 5 ++ > > src/util/virtypedparam.c | 102 > > + > > src/util/virtypedparam.h | 9 > > tests/Makefile.am | 2 +- > > tests/virtypedparamtest.c | 100 > > > > 6 files changed, 222 insertions(+), 1 deletion(-) > > > > diff --git a/include/libvirt/libvirt-host.h b/include/libvirt/libvirt-host.h > > index 070550b..8222cfb 100644 > > --- a/include/libvirt/libvirt-host.h > > +++ b/include/libvirt/libvirt-host.h > > @@ -284,6 +284,11 @@ virTypedParamsGetString (virTypedParameterPtr params, > > const char *name, > > const char **value); > > int > > +virTypedParamsGetAllStrings(virTypedParameterPtr params, Hmm, I apologize for not noticing it in my earlier comment, but since the corresponding API for adding multiple strings is called virTypedParamsAddStringList, we should call this virTypedParamsGetStringList. > > + int nparams, > > + const char *name, > > + const char ***values); Wrong indentation. > > +int > > virTypedParamsAddInt(virTypedParameterPtr *params, > > int *nparams, > > int *maxparams, > > diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms > > index 716dd2f..0a1feea 100644 > > --- a/src/libvirt_public.syms > > +++ b/src/libvirt_public.syms > > @@ -715,4 +715,9 @@ LIBVIRT_1.2.16 { > > virDomainSetUserPassword; > > } LIBVIRT_1.2.15; > > > > +LIBVIRT_1.3.0 { > > +global: > > +virTypedParamsGetAllStrings; > > +} LIBVIRT_1.2.16; > > + > > I don't think this symbol needs to be exported. Yeah, we have no API which would return multiple values for a single parameter so there's no reason to export this now. We can export it later when introducing such API. Jirka -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list