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.
This commit is contained in:
Peter Krempa 2012-09-06 14:47:40 +02:00
parent 972e914f59
commit 245cef9f97
3 changed files with 104 additions and 0 deletions

View File

@ -1733,6 +1733,7 @@ virTimeStringThenRaw;
virTypedParameterArrayClear;
virTypedParameterArrayValidate;
virTypedParameterAssign;
virTypedParameterAssignFromStr;
# viruri.h

View File

@ -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, &param->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, &param->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, &param->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, &param->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, &param->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;
}

View File

@ -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 */