]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Add virXPathLongHex() and virXPathULongHex()
authorMark McLoughlin <markmc@redhat.com>
Tue, 24 Feb 2009 14:53:30 +0000 (14:53 +0000)
committerMark McLoughlin <markmc@redhat.com>
Tue, 24 Feb 2009 14:53:30 +0000 (14:53 +0000)
Add new functions to allow parsing integers with base 16

This will be used to e.g. parse PCI vendor IDs.

ChangeLog
src/xml.c
src/xml.h

index f06e987226d29860da4e022e3367e7ecf7b458e1..f1bc64996595a0b51c390ec96a34535531d20650 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Tue Feb 24 14:52:44 GMT 2009 Mark McLoughlin <markmc@redhat.com>
+
+       * src/xml.[ch]: Add virXPathLongHex() and virXPathULongHex()
+
 Tue Feb 24 14:51:32 GMT 2009 Mark McLoughlin <markmc@redhat.com>
 
        * docs/formatdomain.html: fix a typo in hostdev docs
index 9c27a106d75c6c77c430e999f26fa503472a4553..edfdc178f769bc144fd7e70691cd48bdbdc408cf 100644 (file)
--- a/src/xml.c
+++ b/src/xml.c
@@ -116,23 +116,12 @@ virXPathNumber(virConnectPtr conn,
     return (0);
 }
 
-/**
- * virXPathLong:
- * @xpath: the XPath string to evaluate
- * @ctxt: an XPath context
- * @value: the returned long value
- *
- * Convenience function to evaluate an XPath number
- *
- * Returns 0 in case of success in which case @value is set,
- *         or -1 if the XPath evaluation failed or -2 if the
- *         value doesn't have a long format.
- */
-int
-virXPathLong(virConnectPtr conn,
-             const char *xpath,
-             xmlXPathContextPtr ctxt,
-             long *value)
+static int
+virXPathLongBase(virConnectPtr conn,
+                 const char *xpath,
+                 xmlXPathContextPtr ctxt,
+                 int base,
+                 long *value)
 {
     xmlXPathObjectPtr obj;
     xmlNodePtr relnode;
@@ -150,7 +139,7 @@ virXPathLong(virConnectPtr conn,
         char *conv = NULL;
         long val;
 
-        val = strtol((const char *) obj->stringval, &conv, 10);
+        val = strtol((const char *) obj->stringval, &conv, base);
         if (conv == (const char *) obj->stringval) {
             ret = -2;
         } else {
@@ -172,7 +161,7 @@ virXPathLong(virConnectPtr conn,
 }
 
 /**
- * virXPathULong:
+ * virXPathLong:
  * @xpath: the XPath string to evaluate
  * @ctxt: an XPath context
  * @value: the returned long value
@@ -184,10 +173,42 @@ virXPathLong(virConnectPtr conn,
  *         value doesn't have a long format.
  */
 int
-virXPathULong(virConnectPtr conn,
-              const char *xpath,
-              xmlXPathContextPtr ctxt,
-              unsigned long *value)
+virXPathLong(virConnectPtr conn,
+             const char *xpath,
+             xmlXPathContextPtr ctxt,
+             long *value)
+{
+    return virXPathLongBase(conn, xpath, ctxt, 10, value);
+}
+
+/**
+ * virXPathLongHex:
+ * @xpath: the XPath string to evaluate
+ * @ctxt: an XPath context
+ * @value: the returned long value
+ *
+ * Convenience function to evaluate an XPath number
+ * according to a base of 16
+ *
+ * Returns 0 in case of success in which case @value is set,
+ *         or -1 if the XPath evaluation failed or -2 if the
+ *         value doesn't have a long format.
+ */
+int
+virXPathLongHex(virConnectPtr conn,
+                const char *xpath,
+                xmlXPathContextPtr ctxt,
+                long *value)
+{
+    return virXPathLongBase(conn, xpath, ctxt, 16, value);
+}
+
+static int
+virXPathULongBase(virConnectPtr conn,
+                  const char *xpath,
+                  xmlXPathContextPtr ctxt,
+                  int base,
+                  unsigned long *value)
 {
     xmlXPathObjectPtr obj;
     xmlNodePtr relnode;
@@ -205,7 +226,7 @@ virXPathULong(virConnectPtr conn,
         char *conv = NULL;
         long val;
 
-        val = strtoul((const char *) obj->stringval, &conv, 10);
+        val = strtoul((const char *) obj->stringval, &conv, base);
         if (conv == (const char *) obj->stringval) {
             ret = -2;
         } else {
@@ -226,6 +247,49 @@ virXPathULong(virConnectPtr conn,
     return (ret);
 }
 
+/**
+ * virXPathULong:
+ * @xpath: the XPath string to evaluate
+ * @ctxt: an XPath context
+ * @value: the returned long value
+ *
+ * Convenience function to evaluate an XPath number
+ *
+ * Returns 0 in case of success in which case @value is set,
+ *         or -1 if the XPath evaluation failed or -2 if the
+ *         value doesn't have a long format.
+ */
+int
+virXPathULong(virConnectPtr conn,
+              const char *xpath,
+              xmlXPathContextPtr ctxt,
+              unsigned long *value)
+{
+    return virXPathULongBase(conn, xpath, ctxt, 10, value);
+}
+
+/**
+ * virXPathUHex:
+ * @xpath: the XPath string to evaluate
+ * @ctxt: an XPath context
+ * @value: the returned long value
+ *
+ * Convenience function to evaluate an XPath number
+ * according to base of 16
+ *
+ * Returns 0 in case of success in which case @value is set,
+ *         or -1 if the XPath evaluation failed or -2 if the
+ *         value doesn't have a long format.
+ */
+int
+virXPathULongHex(virConnectPtr conn,
+                 const char *xpath,
+                 xmlXPathContextPtr ctxt,
+                 unsigned long *value)
+{
+    return virXPathULongBase(conn, xpath, ctxt, 16, value);
+}
+
 char *
 virXMLPropString(xmlNodePtr node,
                  const char *name)
index da9d3b593081a29ed6a7430408982b9d8185309f..3754af293a7ae781543784f10b78392950447170 100644 (file)
--- a/src/xml.h
+++ b/src/xml.h
@@ -29,6 +29,14 @@ int          virXPathULong   (virConnectPtr conn,
                                  const char *xpath,
                                  xmlXPathContextPtr ctxt,
                                  unsigned long *value);
+int            virXPathLongHex (virConnectPtr conn,
+                                 const char *xpath,
+                                 xmlXPathContextPtr ctxt,
+                                 long *value);
+int            virXPathULongHex(virConnectPtr conn,
+                                 const char *xpath,
+                                 xmlXPathContextPtr ctxt,
+                                 unsigned long *value);
 xmlNodePtr     virXPathNode    (virConnectPtr conn,
                                  const char *xpath,
                                  xmlXPathContextPtr ctxt);