Convenience function to return value of an on / off attribute. Does not use virTristateSwitchTypeFromString to disallow "default". Signed-off-by: Tim Wiederhake <twiederh@xxxxxxxxxx> --- src/util/virxml.c | 37 +++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 3 +++ 2 files changed, 40 insertions(+) diff --git a/src/util/virxml.c b/src/util/virxml.c index 47e8414bd5..7c06530d25 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -593,6 +593,43 @@ virXMLPropYesNo(xmlNodePtr node, const char* name, virTristateBool *value) } +/** + * virXMLPropOnOff: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * @value: 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 +virXMLPropOnOff(xmlNodePtr node, const char* name, virTristateSwitch *value) +{ + g_autofree char *tmp = virXMLPropString(node, name); + + if (!tmp) + return 0; + + if (STREQ("on", tmp)) { + *value = VIR_TRISTATE_SWITCH_ON; + return 1; + } + + if (STREQ("off", tmp)) { + *value = VIR_TRISTATE_SWITCH_OFF; + return 1; + } + + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in node '%s': '%s'. Expected 'on' or 'off'"), + name, node->name, tmp); + return -1; +} + + /** * virXPathBoolean: * @xpath: the XPath string to evaluate diff --git a/src/util/virxml.h b/src/util/virxml.h index 072948b3e2..d0577723ef 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -81,6 +81,9 @@ char * virXMLNodeContentString(xmlNodePtr node); int virXMLPropYesNo(xmlNodePtr node, const char *name, virTristateBool *value); +int virXMLPropOnOff(xmlNodePtr node, + const char *name, + virTristateSwitch *value); /* Internal function; prefer the macros below. */ xmlDocPtr virXMLParseHelper(int domcode, -- 2.26.2