[libvirt PATCH v4 3/8] virxml: Add virXMLPropInt

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

Signed-off-by: Tim Wiederhake <twiederh@xxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/util/virxml.c        | 51 ++++++++++++++++++++++++++++++++++++++++
 src/util/virxml.h        |  6 +++++
 3 files changed, 58 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 776387e6b3..bdb63349ee 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3544,6 +3544,7 @@ virXMLNodeSanitizeNamespaces;
 virXMLNodeToString;
 virXMLParseHelper;
 virXMLPickShellSafeComment;
+virXMLPropInt;
 virXMLPropString;
 virXMLPropStringLimit;
 virXMLPropTristateBool;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index f62c5c39c4..807cb7dae9 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -656,6 +656,57 @@ virXMLPropTristateSwitch(xmlNodePtr node, const char* name,
 }
 
 
+/**
+ * virXMLPropInt:
+ * @node: XML dom node pointer
+ * @name: Name of the property (attribute) to get
+ * @base: Number base, see strtol
+ * @flags: Bitwise or of virXMLPropFlags
+ * @result: The returned value
+ *
+ * Convenience function to return value of an integer 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
+virXMLPropInt(xmlNodePtr node, const char *name, int base,
+              virXMLPropFlags flags, int *result)
+{
+    g_autofree char *tmp = NULL;
+    int ret;
+
+    if (!node || !name || !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 = virStrToLong_i(tmp, NULL, base, result);
+
+    if ((ret < 0) || ((flags & VIR_XML_PROP_NONZERO) && (*result == 0))) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Invalid value for attribute '%s' in element '%s': '%s'. Expected integer value"),
+                       name, node->name, tmp);
+        return -1;
+    }
+
+    return 1;
+}
+
+
 /**
  * virXPathBoolean:
  * @xpath: the XPath string to evaluate
diff --git a/src/util/virxml.h b/src/util/virxml.h
index a5ecfbb01a..aba863ae11 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -36,6 +36,7 @@ xmlXPathContextPtr virXMLXPathContextNew(xmlDocPtr xml)
 typedef enum {
     VIR_XML_PROP_OPTIONAL       = 0,        /* Attribute may be absent */
     VIR_XML_PROP_REQUIRED       = 1 << 0,   /* Attribute may not be absent */
+    VIR_XML_PROP_NONZERO        = 1 << 1,   /* Attribute may not be zero */
 } virXMLPropFlags;
 
 int              virXPathBoolean(const char *xpath,
@@ -91,6 +92,11 @@ int     virXMLPropTristateSwitch(xmlNodePtr node,
                                  const char *name,
                                  virXMLPropFlags flags,
                                  virTristateSwitch *result);
+int                virXMLPropInt(xmlNodePtr node,
+                                 const char *name,
+                                 int base,
+                                 virXMLPropFlags flags,
+                                 int *result);
 
 /* Internal function; prefer the macros below.  */
 xmlDocPtr      virXMLParseHelper(int domcode,
-- 
2.26.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux