[libvirt PATCH v4 5/8] virxml: Add virXMLPropEnum

Tim Wiederhake twiederh at redhat.com
Thu Apr 8 08:57:07 UTC 2021


Convenience function to return the value of an enum XML attribute.

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

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index dc6c9191c8..79eb4dba50 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3544,6 +3544,7 @@ virXMLNodeSanitizeNamespaces;
 virXMLNodeToString;
 virXMLParseHelper;
 virXMLPickShellSafeComment;
+virXMLPropEnum;
 virXMLPropInt;
 virXMLPropString;
 virXMLPropStringLimit;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 4d8cda3985..a994a404d4 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -762,6 +762,57 @@ virXMLPropUInt(xmlNodePtr node, const char* name, int base,
 }
 
 
+/**
+ * virXMLPropEnum:
+ * @node: XML dom node pointer
+ * @name: Name of the property (attribute) to get
+ * @strToInt: Conversion function to turn enum name to value. Expected to
+ *            return negative value on failure.
+ * @flags: Bitwise or of virXMLPropFlags
+ * @result: The returned value
+ *
+ * Convenience function to return value of an enum 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
+virXMLPropEnum(xmlNodePtr node, const char* name, int (*strToInt)(const char*),
+               virXMLPropFlags flags, unsigned int *result)
+{
+    g_autofree char *tmp = NULL;
+    int ret;
+
+    if (!node || !name || !strToInt || !result) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Invalid argument to %s"),
+                       __FUNCTION__);
+        return -1;
+    }
+
+    if (!(tmp = virXMLPropString(node, name))) {
+        if ((flags & VIR_XML_PROP_REQUIRED) != VIR_XML_PROP_REQUIRED)
+            return 0;
+
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Missing required attribute '%s' in element '%s'"),
+                       name, node->name);
+        return -1;
+    }
+
+    ret = strToInt(tmp);
+    if ((ret < 0) || ((flags & VIR_XML_PROP_NONZERO) && (ret == 0))) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Invalid value for attribute '%s' in element '%s': '%s'."),
+                       name, node->name, NULLSTR(tmp));
+        return -1;
+    }
+
+    *result = ret;
+    return 0;
+}
+
 /**
  * virXPathBoolean:
  * @xpath: the XPath string to evaluate
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 82d5d16375..cbe4cc32dc 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -103,6 +103,11 @@ int               virXMLPropUInt(xmlNodePtr node,
                                  int base,
                                  virXMLPropFlags flags,
                                  unsigned int *result);
+int               virXMLPropEnum(xmlNodePtr node,
+                                 const char* name,
+                                 int (*strToInt)(const char*),
+                                 virXMLPropFlags flags,
+                                 unsigned int *result);
 
 /* Internal function; prefer the macros below.  */
 xmlDocPtr      virXMLParseHelper(int domcode,
-- 
2.26.2




More information about the libvir-list mailing list