From 245cef9f9796691fcba5313bea43af94842c7f23 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 6 Sep 2012 14:47:40 +0200 Subject: [PATCH] util: Add helper to assign typed params from string This patch adds a helper to deal with assigning values to virTypedParameter structures from strings. The helper parses the value from the string and assigns it to the corresponding union value. --- src/libvirt_private.syms | 1 + src/util/virtypedparam.c | 97 ++++++++++++++++++++++++++++++++++++++++ src/util/virtypedparam.h | 6 +++ 3 files changed, 104 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 9971befdd2..10af063a50 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1733,6 +1733,7 @@ virTimeStringThenRaw; virTypedParameterArrayClear; virTypedParameterArrayValidate; virTypedParameterAssign; +virTypedParameterAssignFromStr; # viruri.h diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c index d68d79f2fb..3d920a23aa 100644 --- a/src/util/virtypedparam.c +++ b/src/util/virtypedparam.c @@ -181,3 +181,100 @@ cleanup: va_end(ap); return ret; } + +/* Assign name, type, and convert the argument from a const string. + * In case of a string, the string is copied. + * Return 0 on success, -1 after an error message on failure. */ +int +virTypedParameterAssignFromStr(virTypedParameterPtr param, const char *name, + int type, const char *val) +{ + int ret = -1; + + if (!val) { + virReportError(VIR_ERR_INVALID_ARG, _("NULL value for field '%s'"), + name); + goto cleanup; + } + + if (virStrcpyStatic(param->field, name) == NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, _("Field name '%s' too long"), + name); + goto cleanup; + } + + param->type = type; + switch (type) { + case VIR_TYPED_PARAM_INT: + if (virStrToLong_i(val, NULL, 10, ¶m->value.i) < 0) { + virReportError(VIR_ERR_INVALID_ARG, + _("Invalid value for field '%s': expected int"), + name); + goto cleanup; + } + break; + case VIR_TYPED_PARAM_UINT: + if (virStrToLong_ui(val, NULL, 10, ¶m->value.ui) < 0) { + virReportError(VIR_ERR_INVALID_ARG, + _("Invalid value for field '%s': " + "expected unsigned int"), + name); + goto cleanup; + } + break; + case VIR_TYPED_PARAM_LLONG: + if (virStrToLong_ll(val, NULL, 10, ¶m->value.l) < 0) { + virReportError(VIR_ERR_INVALID_ARG, + _("Invalid value for field '%s': " + "expected long long"), + name); + goto cleanup; + } + break; + case VIR_TYPED_PARAM_ULLONG: + if (virStrToLong_ull(val, NULL, 10, ¶m->value.ul) < 0) { + virReportError(VIR_ERR_INVALID_ARG, + _("Invalid value for field '%s': " + "expected unsigned long long"), + name); + goto cleanup; + } + break; + case VIR_TYPED_PARAM_DOUBLE: + if (virStrToDouble(val, NULL, ¶m->value.d) < 0) { + virReportError(VIR_ERR_INVALID_ARG, + _("Invalid value for field '%s': " + "expected double"), + name); + goto cleanup; + } + break; + case VIR_TYPED_PARAM_BOOLEAN: + if (STRCASEEQ(val, "true") || + STREQ(val, "1")) { + param->value.b = true; + } else if (STRCASEEQ(val, "false") || + STREQ(val, "0")) { + param->value.b = false; + } else { + virReportError(VIR_ERR_INVALID_ARG, + _("Invalid boolean value for field '%s'"), name); + goto cleanup; + } + break; + case VIR_TYPED_PARAM_STRING: + if (!(param->value.s = strdup(val))) { + virReportOOMError(); + goto cleanup; + } + break; + default: + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unexpected type %d for field %s"), type, name); + goto cleanup; + } + + ret = 0; +cleanup: + return ret; +} diff --git a/src/util/virtypedparam.h b/src/util/virtypedparam.h index f117b84fff..581201cbb3 100644 --- a/src/util/virtypedparam.h +++ b/src/util/virtypedparam.h @@ -35,4 +35,10 @@ int virTypedParameterAssign(virTypedParameterPtr param, const char *name, int type, /* TYPE arg */ ...) ATTRIBUTE_RETURN_CHECK; +int virTypedParameterAssignFromStr(virTypedParameterPtr param, + const char *name, + int type, + const char *val) + ATTRIBUTE_RETURN_CHECK; + #endif /* __VIR_TYPED_PARAM_H */ -- GitLab