]> xenbits.xensource.com Git - libvirt.git/commitdiff
virxml: Add virXMLPropTristateSwitch
authorTim Wiederhake <twiederh@redhat.com>
Fri, 16 Apr 2021 09:41:46 +0000 (11:41 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 16 Apr 2021 11:21:27 +0000 (13:21 +0200)
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>
src/libvirt_private.syms
src/util/virxml.c
src/util/virxml.h

index 331cbb57d5cab550bad8c73970d592ae5b6f311c..ba70e8b4cae19bb67b7c269a636d8fd63223d6fe 100644 (file)
@@ -3551,6 +3551,7 @@ virXMLPickShellSafeComment;
 virXMLPropString;
 virXMLPropStringLimit;
 virXMLPropTristateBool;
+virXMLPropTristateSwitch;
 virXMLSaveFile;
 virXMLValidateAgainstSchema;
 virXMLValidatorFree;
index d0c4a6eff546e3942b13038c0d44e89afe65b9f7..e22fb4ae6feb816cc441597e014619223110767a 100644 (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:
  * @xpath: the XPath string to evaluate
index 9751e27a7f9f5a323fd711224d22c254a8a97f2a..703b98385abb8f2fc51aa40f771de0767bc60a23 100644 (file)
@@ -110,6 +110,13 @@ virXMLPropTristateBool(xmlNodePtr node,
                        virTristateBool *result)
     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.  */
 xmlDocPtr
 virXMLParseHelper(int domcode,