diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index a166bd941cfbe1bc20559b6fe8bf5abfcfbdfaa3..d95ef31eea665567768bf193d85954ec72b5c71f 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -886,8 +886,10 @@ virSetUIDGID; virSkipSpaces; virStrToDouble; virStrToLong_i; +virStrToLong_l; virStrToLong_ll; virStrToLong_ui; +virStrToLong_ul; virStrToLong_ull; virStrcpy; virStrncpy; @@ -926,6 +928,7 @@ virStrerror; # xml.h virXMLPropString; virXPathBoolean; +virXPathInt; virXPathLong; virXPathLongHex; virXPathLongLong; @@ -934,6 +937,7 @@ virXPathNodeSet; virXPathNumber; virXPathString; virXPathStringLimit; +virXPathUInt; virXPathULong; virXPathULongHex; virXPathULongLong; diff --git a/src/util/util.c b/src/util/util.c index 18b38f45562b7b87e74ca4c4ee31e220805b120b..f412a8303586d8f45b07842774dae53bd264d7c9 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -2081,7 +2081,45 @@ virStrToLong_ui(char const *s, char **end_ptr, int base, unsigned int *result) return 0; } -/* Just like virStrToLong_i, above, but produce an "long long" value. */ +/* Just like virStrToLong_i, above, but produce a "long" value. */ +int +virStrToLong_l(char const *s, char **end_ptr, int base, long *result) +{ + long int val; + char *p; + int err; + + errno = 0; + val = strtol(s, &p, base); + err = (errno || (!end_ptr && *p) || p == s); + if (end_ptr) + *end_ptr = p; + if (err) + return -1; + *result = val; + return 0; +} + +/* Just like virStrToLong_i, above, but produce an "unsigned long" value. */ +int +virStrToLong_ul(char const *s, char **end_ptr, int base, unsigned long *result) +{ + unsigned long int val; + char *p; + int err; + + errno = 0; + val = strtoul(s, &p, base); + err = (errno || (!end_ptr && *p) || p == s); + if (end_ptr) + *end_ptr = p; + if (err) + return -1; + *result = val; + return 0; +} + +/* Just like virStrToLong_i, above, but produce a "long long" value. */ int virStrToLong_ll(char const *s, char **end_ptr, int base, long long *result) { diff --git a/src/util/util.h b/src/util/util.h index 932db03b081daf2a43b0afa0b80fe455be8bc160..8373038de8406e86ad8ca26c77b0b47e2073d430 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -188,6 +188,14 @@ int virStrToLong_ui(char const *s, char **end_ptr, int base, unsigned int *result); +int virStrToLong_l(char const *s, + char **end_ptr, + int base, + long *result); +int virStrToLong_ul(char const *s, + char **end_ptr, + int base, + unsigned long *result); int virStrToLong_ll(char const *s, char **end_ptr, int base, diff --git a/src/util/xml.c b/src/util/xml.c index e2c2c6c04b927a27d4aade9e87d317215f59c64f..de5e9de17959b9d02512f4e86ead9b57ce79496d 100644 --- a/src/util/xml.c +++ b/src/util/xml.c @@ -1,7 +1,7 @@ /* * xml.c: XML based interfaces for the libvir library * - * Copyright (C) 2005, 2007-2010 Red Hat, Inc. + * Copyright (C) 2005, 2007-2011 Red Hat, Inc. * * See COPYING.LIB for the License of this software * @@ -194,6 +194,35 @@ virXPathLongBase(const char *xpath, return (ret); } +/** + * virXPathInt: + * @xpath: the XPath string to evaluate + * @ctxt: an XPath context + * @value: the returned int 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 an int format. + */ +int +virXPathInt(const char *xpath, + xmlXPathContextPtr ctxt, + int *value) +{ + long tmp; + int ret; + + ret = virXPathLongBase(xpath, ctxt, 10, &tmp); + if (ret < 0) + return ret; + if ((int) tmp != tmp) + return -2; + *value = tmp; + return 0; +} + /** * virXPathLong: * @xpath: the XPath string to evaluate @@ -278,6 +307,35 @@ virXPathULongBase(const char *xpath, return (ret); } +/** + * virXPathUInt: + * @xpath: the XPath string to evaluate + * @ctxt: an XPath context + * @value: the returned int 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 an int format. + */ +int +virXPathUInt(const char *xpath, + xmlXPathContextPtr ctxt, + unsigned int *value) +{ + unsigned long tmp; + int ret; + + ret = virXPathULongBase(xpath, ctxt, 10, &tmp); + if (ret < 0) + return ret; + if ((unsigned int) tmp != tmp) + return -2; + *value = tmp; + return 0; +} + /** * virXPathULong: * @xpath: the XPath string to evaluate diff --git a/src/util/xml.h b/src/util/xml.h index b1da741eea4e3f0172e7d89a002c0041266bc1b3..b342e8355307fe14c47bed7bdd04328f859f83f3 100644 --- a/src/util/xml.h +++ b/src/util/xml.h @@ -21,19 +21,25 @@ char * virXPathStringLimit(const char *xpath, int virXPathNumber(const char *xpath, xmlXPathContextPtr ctxt, double *value); +int virXPathInt(const char *xpath, + xmlXPathContextPtr ctxt, + int *value); +int virXPathUInt(const char *xpath, + xmlXPathContextPtr ctxt, + unsigned int *value); int virXPathLong(const char *xpath, xmlXPathContextPtr ctxt, long *value); -int virXPathULong(const char *xpath, +int virXPathULong(const char *xpath, xmlXPathContextPtr ctxt, unsigned long *value); int virXPathULongLong(const char *xpath, xmlXPathContextPtr ctxt, unsigned long long *value); -int virXPathLongLong(const char *xpath, +int virXPathLongLong(const char *xpath, xmlXPathContextPtr ctxt, long long *value); -int virXPathLongHex (const char *xpath, +int virXPathLongHex(const char *xpath, xmlXPathContextPtr ctxt, long *value); int virXPathULongHex(const char *xpath,