From ee197521c5bb196907d84326eb0a5a6fae769f06 Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Tue, 24 Feb 2009 14:54:30 +0000 Subject: [PATCH] Add virXPathULongLong() Add a variant of virXPathULong() which can handle long longs. Needed for parsing storage device capacities. --- ChangeLog | 4 ++++ src/xml.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/xml.h | 4 ++++ 3 files changed, 63 insertions(+) diff --git a/ChangeLog b/ChangeLog index f1bc649965..48a334212d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Tue Feb 24 14:52:58 GMT 2009 Mark McLoughlin + + * src/xml.[ch]: Add virXPathULongLong() + Tue Feb 24 14:52:44 GMT 2009 Mark McLoughlin * src/xml.[ch]: Add virXPathLongHex() and virXPathULongHex() diff --git a/src/xml.c b/src/xml.c index edfdc178f7..a4465b2208 100644 --- 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) diff --git a/src/xml.h b/src/xml.h index 3754af293a..34b0f10c4e 100644 --- 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,