[libvirt PATCH 02/38] virxml: Add virXMLPropYesNo

Tim Wiederhake twiederh at redhat.com
Thu Mar 18 08:00:41 UTC 2021


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

Does not use virTristateBoolTypeFromString to disallow "default".

Signed-off-by: Tim Wiederhake <twiederh at redhat.com>
---
 src/util/virxml.c | 37 +++++++++++++++++++++++++++++++++++++
 src/util/virxml.h |  4 ++++
 2 files changed, 41 insertions(+)

diff --git a/src/util/virxml.c b/src/util/virxml.c
index 060b7530fc..47e8414bd5 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -556,6 +556,43 @@ virXMLNodeContentString(xmlNodePtr node)
 }
 
 
+/**
+ * virXMLPropYesNo:
+ * @node: XML dom node pointer
+ * @name: Name of the property (attribute) to get
+ * @value: 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
+virXMLPropYesNo(xmlNodePtr node, const char* name, virTristateBool *value)
+{
+    g_autofree char *tmp = virXMLPropString(node, name);
+
+    if (!tmp)
+        return 0;
+
+    if (STREQ("yes", tmp)) {
+        *value = VIR_TRISTATE_BOOL_YES;
+        return 1;
+    }
+
+    if (STREQ("no", tmp)) {
+        *value = VIR_TRISTATE_BOOL_NO;
+        return 1;
+    }
+
+    virReportError(VIR_ERR_XML_ERROR,
+                   _("Invalid value for attribute '%s' in node '%s': '%s'. Expected 'yes' or 'no'"),
+                   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 d32f77b867..072948b3e2 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,9 @@ char *     virXMLPropStringLimit(xmlNodePtr node,
                                  const char *name,
                                  size_t maxlen);
 char *   virXMLNodeContentString(xmlNodePtr node);
+int              virXMLPropYesNo(xmlNodePtr node,
+                                 const char *name,
+                                 virTristateBool *value);
 
 /* Internal function; prefer the macros below.  */
 xmlDocPtr      virXMLParseHelper(int domcode,
-- 
2.26.2




More information about the libvir-list mailing list