Add a variant of virXPathULong() which can handle long longs. Needed for parsing storage device capacities. Signed-off-by: Mark McLoughlin <markmc@xxxxxxxxxx> --- src/xml.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/xml.h | 4 ++++ 2 files changed, 59 insertions(+), 0 deletions(-) diff --git a/src/xml.c b/src/xml.c index edfdc17..a4465b2 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 3754af2..34b0f10 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, -- 1.6.0.6 -- Libvir-list mailing list Libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list