virxml: Add virXMLPropTristateSwitch

Convenience function to return the value of an on / off XML attribute.

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Tim Wiederhake 2021-04-16 11:41:46 +02:00 committed by Peter Krempa
parent c8726ede83
commit 8861d96c88
3 changed files with 52 additions and 0 deletions

View File

@ -3551,6 +3551,7 @@ virXMLPickShellSafeComment;
virXMLPropString; virXMLPropString;
virXMLPropStringLimit; virXMLPropStringLimit;
virXMLPropTristateBool; virXMLPropTristateBool;
virXMLPropTristateSwitch;
virXMLSaveFile; virXMLSaveFile;
virXMLValidateAgainstSchema; virXMLValidateAgainstSchema;
virXMLValidatorFree; virXMLValidatorFree;

View File

@ -602,6 +602,50 @@ virXMLPropTristateBool(xmlNodePtr node,
} }
/**
* virXMLPropTristateSwitch:
* @node: XML dom node pointer
* @name: Name of the property (attribute) to get
* @flags: Bitwise or of virXMLPropFlags
* @result: The returned value
*
* Convenience function to return value of an on / off attribute.
*
* Returns 1 in case of success in which case @result is set,
* or 0 if the attribute is not present,
* or -1 and reports an error on failure.
*/
int
virXMLPropTristateSwitch(xmlNodePtr node,
const char* name,
virXMLPropFlags flags,
virTristateSwitch *result)
{
g_autofree char *tmp = NULL;
int val;
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;
}
/** /**
* virXPathBoolean: * virXPathBoolean:
* @xpath: the XPath string to evaluate * @xpath: the XPath string to evaluate

View File

@ -110,6 +110,13 @@ virXMLPropTristateBool(xmlNodePtr node,
virTristateBool *result) virTristateBool *result)
ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3); ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
int
virXMLPropTristateSwitch(xmlNodePtr node,
const char *name,
virXMLPropFlags flags,
virTristateSwitch *result)
ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
/* Internal function; prefer the macros below. */ /* Internal function; prefer the macros below. */
xmlDocPtr xmlDocPtr
virXMLParseHelper(int domcode, virXMLParseHelper(int domcode,