/** * virTypedParamsGetStringList: * @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 virTypedParamsGetStringList(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; }
testTypedParamsFilter(const void *opaque ATTRIBUTE_UNUSED) { size_t i, nfiltered; int rv = -1; virTypedParameter params[] = { { .field = "bar", .type = VIR_TYPED_PARAM_UINT }, { .field = "foo", .type = VIR_TYPED_PARAM_INT }, { .field = "bar", .type = VIR_TYPED_PARAM_UINT }, { .field = "foo", .type = VIR_TYPED_PARAM_INT }, { .field = "foobar", .type = VIR_TYPED_PARAM_STRING }, { .field = "foo", .type = VIR_TYPED_PARAM_INT } }; virTypedParameterPtr *filtered = NULL; nfiltered = virTypedParamsFilter(params, ARRAY_CARDINALITY(params), "foo", &filtered); if (nfiltered != 3) goto cleanup; for (i = 0; i < nfiltered; i++) { if (filtered[i] != ¶ms[1 + i * 2]) goto cleanup; } VIR_FREE(filtered); filtered = NULL; nfiltered = virTypedParamsFilter(params, ARRAY_CARDINALITY(params), "bar", &filtered); if (nfiltered != 2) goto cleanup;