[libvirt PATCH v2 15/51] virxml: Add virXMLPropTristateBool

Tim Wiederhake twiederh at redhat.com
Fri Mar 19 13:40:29 UTC 2021


Convenience function to return value of a yes / no attribute.

Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virxml.c        | 41 ++++++++++++++++++++++++++++++++++++++++
 src/util/virxml.h        |  5 +++++
 3 files changed, 47 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 526dcee11a..70525cef8c 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3545,6 +3545,7 @@ virXMLParseHelper;
 virXMLPickShellSafeComment;
 virXMLPropString;
 virXMLPropStringLimit;
+virXMLPropTristateBool;
 virXMLSaveFile;
 virXMLValidateAgainstSchema;
 virXMLValidatorFree;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 4a6fe09468..81b7bb1386 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -558,6 +558,47 @@ virXMLNodeContentString(xmlNodePtr node)
 }
 
 
+/**
+ * virXMLPropTristateBool:
+ * @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 virTristateBool value
+ *
+ * Convenience function to return value of a yes / no 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
+virXMLPropTristateBool(xmlNodePtr node, const char* name, bool required,
+                       virTristateBool *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 = 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;
+}
+
+
 /**
  * virXPathBoolean:
  * @xpath: the XPath string to evaluate
diff --git a/src/util/virxml.h b/src/util/virxml.h
index d32f77b867..3041c37df3 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -28,6 +28,7 @@
 #include <libxml/relaxng.h>
 
 #include "virbuffer.h"
+#include "virenum.h"
 
 xmlXPathContextPtr virXMLXPathContextNew(xmlDocPtr xml)
     G_GNUC_WARN_UNUSED_RESULT;
@@ -77,6 +78,10 @@ char *     virXMLPropStringLimit(xmlNodePtr node,
                                  const char *name,
                                  size_t maxlen);
 char *   virXMLNodeContentString(xmlNodePtr node);
+int       virXMLPropTristateBool(xmlNodePtr node,
+                                 const char *name,
+                                 bool mandatory,
+                                 virTristateBool *result);
 
 /* Internal function; prefer the macros below.  */
 xmlDocPtr      virXMLParseHelper(int domcode,
-- 
2.26.2




More information about the libvir-list mailing list