[PATCH 15/17] util: xml: Introduce virXMLPropEnumDefault

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

 



The helper is almost identical to virXMLPropEnum but it allows to pass a
default value to initialize the result to.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/util/virxml.c        | 30 ++++++++++++++++++++++++++++++
 src/util/virxml.h        | 11 +++++++++++
 3 files changed, 42 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 9f761c2c00..1b12c49018 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3550,6 +3550,7 @@ virXMLNodeToString;
 virXMLParseHelper;
 virXMLPickShellSafeComment;
 virXMLPropEnum;
+virXMLPropEnumDefault;
 virXMLPropInt;
 virXMLPropString;
 virXMLPropStringLimit;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 8e28629e9a..3ad596b3e2 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -827,6 +827,36 @@ virXMLPropULongLong(xmlNodePtr node,
 }


+/**
+ * virXMLPropEnumDefault:
+ * @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
+ * @defaultResult: default value set to @result in case the property is missing
+ *
+ * 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
+virXMLPropEnumDefault(xmlNodePtr node,
+                      const char* name,
+                      int (*strToInt)(const char*),
+                      virXMLPropFlags flags,
+                      unsigned int *result,
+                      unsigned int defaultResult)
+{
+    *result = defaultResult;
+
+    return virXMLPropEnumInternal(node, name, strToInt, flags, result);
+}
+
+
 /**
  * virXMLPropEnum:
  * @node: XML dom node pointer
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 939d2482cb..ed02abd2e9 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -152,6 +152,17 @@ virXMLPropEnum(xmlNodePtr node,
     ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
     ATTRIBUTE_NONNULL(4);

+int
+virXMLPropEnumDefault(xmlNodePtr node,
+                      const char* name,
+                      int (*strToInt)(const char*),
+                      virXMLPropFlags flags,
+                      unsigned int *result,
+                      unsigned int defaultResult)
+    ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
+    ATTRIBUTE_NONNULL(4);
+
+
 /* Internal function; prefer the macros below.  */
 xmlDocPtr
 virXMLParseHelper(int domcode,
-- 
2.30.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