util: virtypedparam: Introduce virTypedParamsGetUnsigned

Add an internal helper for fetching a typed parameter which can be
either of the '_UINT' or '_ULONG' type and store it in a unsigned long
long variable.

Since this is an internal helper it offers less protections against
invalid use compared to those we expose as public API.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2023-04-18 13:43:56 +02:00
parent e280e83bff
commit 111eeba7a7
3 changed files with 57 additions and 0 deletions

View File

@ -3567,6 +3567,7 @@ virTypedParamsCopy;
virTypedParamsDeserialize;
virTypedParamsFilter;
virTypedParamsGetStringList;
virTypedParamsGetUnsigned;
virTypedParamsRemoteFree;
virTypedParamsReplaceString;
virTypedParamsSerialize;

View File

@ -456,6 +456,57 @@ virTypedParamsGetStringList(virTypedParameterPtr params,
}
/**
* virTypedParamsGetUnsigned:
* @params: array of typed parameters
* @nparams: number of parameters in the @params array
* @name: name of the parameter to find
* @value: where to store the parameter's value
*
* Finds typed parameter called @name and store its 'unsigned long long' or
* 'unsigned int' value in @value.
*
* This is an internal variand which expects that the typed parameters were
* already validated by calling virTypedParamsValidate and the appropriate
* parameter has the expected type.
*
* Returns 1 on success, 0 when the parameter does not exist in @params, or
* -1 on invalid usage.
*/
int
virTypedParamsGetUnsigned(virTypedParameterPtr params,
int nparams,
const char *name,
unsigned long long *value)
{
virTypedParameterPtr param;
if (!(param = virTypedParamsGet(params, nparams, name)))
return 0;
switch ((virTypedParameterType) param->type) {
case VIR_TYPED_PARAM_UINT:
*value = param->value.ui;
break;
case VIR_TYPED_PARAM_ULLONG:
*value = param->value.ul;
break;
case VIR_TYPED_PARAM_INT:
case VIR_TYPED_PARAM_LLONG:
case VIR_TYPED_PARAM_DOUBLE:
case VIR_TYPED_PARAM_BOOLEAN:
case VIR_TYPED_PARAM_STRING:
case VIR_TYPED_PARAM_LAST:
default:
return -1;
}
return 1;
}
/**
* virTypedParamsRemoteFree:
* @remote_params_val: array of typed parameters as specified by

View File

@ -79,6 +79,11 @@ virTypedParamsFilter(virTypedParameterPtr params,
virTypedParameterPtr **ret)
G_GNUC_WARN_UNUSED_RESULT;
int
virTypedParamsGetUnsigned(virTypedParameterPtr params,
int nparams,
const char *name,
unsigned long long *value);
int
virTypedParameterAssign(virTypedParameterPtr param,