[PATCH 19/43] util: xml: Introduce virXPathU(Int|LongLong)Base

Peter Krempa pkrempa at redhat.com
Mon Oct 24 14:14:24 UTC 2022


In an effort to remove the 'Long' variants of XPath number fetching
functions we need a way to replace the hex number parsing capability.

The new helpers are created from the originals by adding a 'base'
argument and keeping the original function as a wrapper to pass 10.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/libvirt_private.syms |  2 ++
 src/util/virxml.c        | 42 ++++++++++++++++++++++++++++++----------
 src/util/virxml.h        | 10 ++++++++++
 3 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c6445dedcb..dab3b389c8 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3699,9 +3699,11 @@ virXPathNode;
 virXPathNodeSet;
 virXPathString;
 virXPathUInt;
+virXPathUIntBase;
 virXPathULong;
 virXPathULongHex;
 virXPathULongLong;
+virXPathULongLongBase;


 # Let emacs know we want case-insensitive sorting
diff --git a/src/util/virxml.c b/src/util/virxml.c
index d885fc1680..a47a5d49fc 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -228,9 +228,10 @@ virXPathULongBase(const char *xpath,


 /**
- * virXPathUInt:
+ * virXPathUIntBase:
  * @xpath: the XPath string to evaluate
  * @ctxt: an XPath context
+ * @base: base of the number to fetch @value as
  * @value: the returned unsigned int value
  *
  * Convenience function to evaluate an XPath number. The @xpath expression
@@ -242,22 +243,32 @@ virXPathULongBase(const char *xpath,
  *         value doesn't have an unsigned int format.
  */
 int
-virXPathUInt(const char *xpath,
-             xmlXPathContextPtr ctxt,
-             unsigned int *value)
+virXPathUIntBase(const char *xpath,
+                 xmlXPathContextPtr ctxt,
+                 unsigned int base,
+                 unsigned int *value)
 {
     g_autoptr(xmlXPathObject) obj = NULL;

     if (!(obj = virXPathEvalString(xpath, ctxt)))
         return -1;

-    if (virStrToLong_ui((char *) obj->stringval, NULL, 10, value) < 0)
+    if (virStrToLong_ui((char *) obj->stringval, NULL, base, value) < 0)
         return -2;

     return 0;
 }


+int
+virXPathUInt(const char *xpath,
+             xmlXPathContextPtr ctxt,
+             unsigned int *value)
+{
+    return virXPathUIntBase(xpath, ctxt, 10, value);
+}
+
+
 /**
  * virXPathULong:
  * @xpath: the XPath string to evaluate
@@ -302,9 +313,10 @@ virXPathULongHex(const char *xpath,


 /**
- * virXPathULongLong:
+ * virXPathULongLongBase:
  * @xpath: the XPath string to evaluate
  * @ctxt: an XPath context
+ * @base: base of the number to fetch @value as
  * @value: the returned unsigned long long value
  *
  * Convenience function to evaluate an XPath number. The @xpath expression
@@ -316,22 +328,32 @@ virXPathULongHex(const char *xpath,
  *         value doesn't have a unsigned long long format.
  */
 int
-virXPathULongLong(const char *xpath,
-                  xmlXPathContextPtr ctxt,
-                  unsigned long long *value)
+virXPathULongLongBase(const char *xpath,
+                      xmlXPathContextPtr ctxt,
+                      unsigned int base,
+                      unsigned long long *value)
 {
     g_autoptr(xmlXPathObject) obj = NULL;

     if (!(obj = virXPathEvalString(xpath, ctxt)))
         return -1;

-    if (virStrToLong_ullp((char *) obj->stringval, NULL, 10, value) < 0)
+    if (virStrToLong_ullp((char *) obj->stringval, NULL, base, value) < 0)
         return -2;

     return 0;
 }


+int
+virXPathULongLong(const char *xpath,
+                  xmlXPathContextPtr ctxt,
+                  unsigned long long *value)
+{
+    return virXPathULongLongBase(xpath, ctxt, 10, value);
+}
+
+
 /**
  * virXPathLongLong:
  * @xpath: the XPath string to evaluate
diff --git a/src/util/virxml.h b/src/util/virxml.h
index fe07e2d223..bdd2e9145d 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -51,6 +51,11 @@ virXPathInt(const char *xpath,
             xmlXPathContextPtr ctxt,
             int *value);
 int
+virXPathUIntBase(const char *xpath,
+                 xmlXPathContextPtr ctxt,
+                 unsigned int base,
+                 unsigned int *value);
+int
 virXPathUInt(const char *xpath,
              xmlXPathContextPtr ctxt,
              unsigned int *value);
@@ -63,6 +68,11 @@ virXPathULong(const char *xpath,
               xmlXPathContextPtr ctxt,
               unsigned long *value);
 int
+virXPathULongLongBase(const char *xpath,
+                      xmlXPathContextPtr ctxt,
+                      unsigned int base,
+                      unsigned long long *value);
+int
 virXPathULongLong(const char *xpath,
                   xmlXPathContextPtr ctxt,
                   unsigned long long *value);
-- 
2.37.3



More information about the libvir-list mailing list