]> xenbits.xensource.com Git - libvirt.git/commitdiff
Add virXPathULongLong()
authorMark McLoughlin <markmc@redhat.com>
Tue, 24 Feb 2009 14:54:30 +0000 (14:54 +0000)
committerMark McLoughlin <markmc@redhat.com>
Tue, 24 Feb 2009 14:54:30 +0000 (14:54 +0000)
Add a variant of virXPathULong() which can handle long longs.

Needed for parsing storage device capacities.

ChangeLog
src/xml.c
src/xml.h

index f1bc64996595a0b51c390ec96a34535531d20650..48a334212da9d916dd0034a5df3933fcd1056f66 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Tue Feb 24 14:52:58 GMT 2009 Mark McLoughlin <markmc@redhat.com>
+
+       * src/xml.[ch]: Add virXPathULongLong()
+
 Tue Feb 24 14:52:44 GMT 2009 Mark McLoughlin <markmc@redhat.com>
 
        * src/xml.[ch]: Add virXPathLongHex() and virXPathULongHex()
index edfdc178f769bc144fd7e70691cd48bdbdc408cf..a4465b22088a69c21dd2a99397e804e320857082 100644 (file)
--- a/src/xml.c
+++ b/src/xml.c
@@ -290,6 +290,61 @@ virXPathULongHex(virConnectPtr conn,
     return virXPathULongBase(conn, xpath, ctxt, 16, value);
 }
 
+/**
+ * virXPathULongLong:
+ * @xpath: the XPath string to evaluate
+ * @ctxt: an XPath context
+ * @value: the returned long 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
+virXPathULongLong(virConnectPtr conn,
+                  const char *xpath,
+                  xmlXPathContextPtr ctxt,
+                  unsigned long long *value)
+{
+    xmlXPathObjectPtr obj;
+    xmlNodePtr relnode;
+    int ret = 0;
+
+    if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
+        virXMLError(conn, VIR_ERR_INTERNAL_ERROR,
+                    "%s", _("Invalid parameter to virXPathULong()"));
+        return (-1);
+    }
+    relnode = ctxt->node;
+    obj = xmlXPathEval(BAD_CAST xpath, ctxt);
+    if ((obj != NULL) && (obj->type == XPATH_STRING) &&
+        (obj->stringval != NULL) && (obj->stringval[0] != 0)) {
+        char *conv = NULL;
+        unsigned long long val;
+
+        val = strtoull((const char *) obj->stringval, &conv, 10);
+        if (conv == (const char *) obj->stringval) {
+            ret = -2;
+        } else {
+            *value = val;
+        }
+    } else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
+               (!(isnan(obj->floatval)))) {
+        *value = (unsigned long long) obj->floatval;
+        if (*value != obj->floatval) {
+            ret = -2;
+        }
+    } else {
+        ret = -1;
+    }
+
+    xmlXPathFreeObject(obj);
+    ctxt->node = relnode;
+    return (ret);
+}
+
 char *
 virXMLPropString(xmlNodePtr node,
                  const char *name)
index 3754af293a7ae781543784f10b78392950447170..34b0f10c4edd03f574588889b864787c41f0669f 100644 (file)
--- a/src/xml.h
+++ b/src/xml.h
@@ -29,6 +29,10 @@ int          virXPathULong   (virConnectPtr conn,
                                  const char *xpath,
                                  xmlXPathContextPtr ctxt,
                                  unsigned long *value);
+int            virXPathULongLong(virConnectPtr conn,
+                                 const char *xpath,
+                                 xmlXPathContextPtr ctxt,
+                                 unsigned long long *value);
 int            virXPathLongHex (virConnectPtr conn,
                                  const char *xpath,
                                  xmlXPathContextPtr ctxt,