mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-05 13:17:51 +03:00
util: xml: Extract implementation of xml property -> enum parsing to a common helper
virXMLPropTristateBool/virXMLPropTristateSwitch/virXMLPropEnum can be implemented using the same internal code. Extract it into a new function called virXMLPropEnumInternal, which will also simplify adding versions of these functions with a custom default value. This way we'll be able to always initialize @result so that unused value bugs can be prevented. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
4ef4476d3a
commit
0420c325ce
@ -557,6 +557,40 @@ virXMLNodeContentString(xmlNodePtr node)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
virXMLPropEnumInternal(xmlNodePtr node,
|
||||
const char* name,
|
||||
int (*strToInt)(const char*),
|
||||
virXMLPropFlags flags,
|
||||
unsigned int *result)
|
||||
|
||||
{
|
||||
g_autofree char *tmp = NULL;
|
||||
int ret;
|
||||
|
||||
if (!(tmp = virXMLPropString(node, name))) {
|
||||
if (!(flags & VIR_XML_PROP_REQUIRED))
|
||||
return 0;
|
||||
|
||||
virReportError(VIR_ERR_XML_ERROR,
|
||||
_("Missing required attribute '%s' in element '%s'"),
|
||||
name, node->name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = strToInt(tmp);
|
||||
if (ret < 0 ||
|
||||
((flags & VIR_XML_PROP_NONZERO) && (ret == 0))) {
|
||||
virReportError(VIR_ERR_XML_ERROR,
|
||||
_("Invalid value for attribute '%s' in element '%s': '%s'."),
|
||||
name, node->name, NULLSTR(tmp));
|
||||
return -1;
|
||||
}
|
||||
|
||||
*result = ret;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* virXMLPropTristateBool:
|
||||
@ -577,28 +611,10 @@ virXMLPropTristateBool(xmlNodePtr node,
|
||||
virXMLPropFlags flags,
|
||||
virTristateBool *result)
|
||||
{
|
||||
g_autofree char *tmp = NULL;
|
||||
int val;
|
||||
flags |= VIR_XML_PROP_NONZERO;
|
||||
|
||||
if (!(tmp = virXMLPropString(node, name))) {
|
||||
if (!(flags & VIR_XML_PROP_REQUIRED))
|
||||
return 0;
|
||||
|
||||
virReportError(VIR_ERR_XML_ERROR,
|
||||
_("Missing required attribute '%s' in element '%s'"),
|
||||
name, node->name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((val = virTristateBoolTypeFromString(tmp)) <= 0) {
|
||||
virReportError(VIR_ERR_XML_ERROR,
|
||||
_("Invalid value for attribute '%s' in element '%s': '%s'. Expected 'yes' or 'no'"),
|
||||
name, node->name, tmp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*result = val;
|
||||
return 1;
|
||||
return virXMLPropEnumInternal(node, name, virTristateBoolTypeFromString,
|
||||
flags, result);
|
||||
}
|
||||
|
||||
|
||||
@ -621,28 +637,10 @@ virXMLPropTristateSwitch(xmlNodePtr node,
|
||||
virXMLPropFlags flags,
|
||||
virTristateSwitch *result)
|
||||
{
|
||||
g_autofree char *tmp = NULL;
|
||||
int val;
|
||||
flags |= VIR_XML_PROP_NONZERO;
|
||||
|
||||
if (!(tmp = virXMLPropString(node, name))) {
|
||||
if (!(flags & VIR_XML_PROP_REQUIRED))
|
||||
return 0;
|
||||
|
||||
virReportError(VIR_ERR_XML_ERROR,
|
||||
_("Missing required attribute '%s' in element '%s'"),
|
||||
name, node->name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((val = virTristateSwitchTypeFromString(tmp)) <= 0) {
|
||||
virReportError(VIR_ERR_XML_ERROR,
|
||||
_("Invalid value for attribute '%s' in element '%s': '%s'. Expected 'on' or 'off'"),
|
||||
name, node->name, tmp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*result = val;
|
||||
return 1;
|
||||
return virXMLPropEnumInternal(node, name, virTristateSwitchTypeFromString,
|
||||
flags, result);
|
||||
}
|
||||
|
||||
|
||||
@ -833,32 +831,10 @@ virXMLPropEnum(xmlNodePtr node,
|
||||
virXMLPropFlags flags,
|
||||
unsigned int *result)
|
||||
{
|
||||
g_autofree char *tmp = NULL;
|
||||
int ret;
|
||||
|
||||
if (!(tmp = virXMLPropString(node, name))) {
|
||||
if (!(flags & VIR_XML_PROP_REQUIRED))
|
||||
return 0;
|
||||
|
||||
virReportError(VIR_ERR_XML_ERROR,
|
||||
_("Missing required attribute '%s' in element '%s'"),
|
||||
name, node->name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = strToInt(tmp);
|
||||
if (ret < 0 ||
|
||||
((flags & VIR_XML_PROP_NONZERO) && (ret == 0))) {
|
||||
virReportError(VIR_ERR_XML_ERROR,
|
||||
_("Invalid value for attribute '%s' in element '%s': '%s'."),
|
||||
name, node->name, NULLSTR(tmp));
|
||||
return -1;
|
||||
}
|
||||
|
||||
*result = ret;
|
||||
return 1;
|
||||
return virXMLPropEnumInternal(node, name, strToInt, flags, result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* virXPathBoolean:
|
||||
* @xpath: the XPath string to evaluate
|
||||
|
Loading…
Reference in New Issue
Block a user