Add new functions to allow parsing integers with base 16
This will be used to e.g. parse PCI vendor IDs.
Signed-off-by: Mark McLoughlin <markmc(a)redhat.com>
---
src/xml.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++-------------
src/xml.h | 8 ++++
2 files changed, 96 insertions(+), 24 deletions(-)
diff --git a/src/xml.c b/src/xml.c
index 9c27a10..edfdc17 100644
--- 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)
diff --git a/src/xml.h b/src/xml.h
index da9d3b5..3754af2 100644
--- 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);
--
1.6.0.6