Convenience function to return value of an on / off attribute. Signed-off-by: Tim Wiederhake <twiederh@xxxxxxxxxx> --- src/libvirt_private.syms | 1 + src/util/virxml.c | 41 ++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 6 +++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 70525cef8c..9aed890c58 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3546,6 +3546,7 @@ virXMLPickShellSafeComment; virXMLPropString; virXMLPropStringLimit; virXMLPropTristateBool; +virXMLPropTristateSwitch; virXMLSaveFile; virXMLValidateAgainstSchema; virXMLValidatorFree; diff --git a/src/util/virxml.c b/src/util/virxml.c index 81b7bb1386..aaad6453d2 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -599,6 +599,47 @@ virXMLPropTristateBool(xmlNodePtr node, const char* name, bool required, } +/** + * virXMLPropTristateSwitch: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * @required: Change the return value to -1 if the attribute is not present + * @result: The returned virTristateSwitch value + * + * Convenience function to return value of an on / off attribute. + * + * Returns 1 in case of success in which case @value 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, bool required, + virTristateSwitch *result) +{ + g_autofree char *tmp = virXMLPropString(node, name); + int val; + + if (!tmp) { + if (!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: * @xpath: the XPath string to evaluate diff --git a/src/util/virxml.h b/src/util/virxml.h index 3041c37df3..e844cb0713 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -80,8 +80,12 @@ char * virXMLPropStringLimit(xmlNodePtr node, char * virXMLNodeContentString(xmlNodePtr node); int virXMLPropTristateBool(xmlNodePtr node, const char *name, - bool mandatory, + bool required, virTristateBool *result); +int virXMLPropTristateSwitch(xmlNodePtr node, + const char *name, + bool required, + virTristateSwitch *result); /* Internal function; prefer the macros below. */ xmlDocPtr virXMLParseHelper(int domcode, -- 2.26.2