mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 11:31:16 +00:00
util: add missing string->integer conversion functions
It was awkward having only int conversion in the virStrToLong family, but only long conversion in the virXPath family. Make both families support both types. * src/util/util.h (virStrToLong_l, virStrToLong_ul): New prototypes. * src/util/xml.h (virXPathInt, virXPathUInt): Likewise. * src/util/util.c (virStrToLong_l, virStrToLong_ul): New functions. * src/util/xml.c (virXPathInt, virXPathUInt): Likewise. * src/libvirt_private.syms (util.h, xml.h): Export them.
This commit is contained in:
parent
efc892ad6f
commit
1ff03b28e9
@ -886,8 +886,10 @@ virSetUIDGID;
|
|||||||
virSkipSpaces;
|
virSkipSpaces;
|
||||||
virStrToDouble;
|
virStrToDouble;
|
||||||
virStrToLong_i;
|
virStrToLong_i;
|
||||||
|
virStrToLong_l;
|
||||||
virStrToLong_ll;
|
virStrToLong_ll;
|
||||||
virStrToLong_ui;
|
virStrToLong_ui;
|
||||||
|
virStrToLong_ul;
|
||||||
virStrToLong_ull;
|
virStrToLong_ull;
|
||||||
virStrcpy;
|
virStrcpy;
|
||||||
virStrncpy;
|
virStrncpy;
|
||||||
@ -926,6 +928,7 @@ virStrerror;
|
|||||||
# xml.h
|
# xml.h
|
||||||
virXMLPropString;
|
virXMLPropString;
|
||||||
virXPathBoolean;
|
virXPathBoolean;
|
||||||
|
virXPathInt;
|
||||||
virXPathLong;
|
virXPathLong;
|
||||||
virXPathLongHex;
|
virXPathLongHex;
|
||||||
virXPathLongLong;
|
virXPathLongLong;
|
||||||
@ -934,6 +937,7 @@ virXPathNodeSet;
|
|||||||
virXPathNumber;
|
virXPathNumber;
|
||||||
virXPathString;
|
virXPathString;
|
||||||
virXPathStringLimit;
|
virXPathStringLimit;
|
||||||
|
virXPathUInt;
|
||||||
virXPathULong;
|
virXPathULong;
|
||||||
virXPathULongHex;
|
virXPathULongHex;
|
||||||
virXPathULongLong;
|
virXPathULongLong;
|
||||||
|
@ -2081,7 +2081,45 @@ virStrToLong_ui(char const *s, char **end_ptr, int base, unsigned int *result)
|
|||||||
return 0;
|
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
|
int
|
||||||
virStrToLong_ll(char const *s, char **end_ptr, int base, long long *result)
|
virStrToLong_ll(char const *s, char **end_ptr, int base, long long *result)
|
||||||
{
|
{
|
||||||
|
@ -188,6 +188,14 @@ int virStrToLong_ui(char const *s,
|
|||||||
char **end_ptr,
|
char **end_ptr,
|
||||||
int base,
|
int base,
|
||||||
unsigned int *result);
|
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,
|
int virStrToLong_ll(char const *s,
|
||||||
char **end_ptr,
|
char **end_ptr,
|
||||||
int base,
|
int base,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* xml.c: XML based interfaces for the libvir library
|
* 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
|
* See COPYING.LIB for the License of this software
|
||||||
*
|
*
|
||||||
@ -194,6 +194,35 @@ virXPathLongBase(const char *xpath,
|
|||||||
return (ret);
|
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:
|
* virXPathLong:
|
||||||
* @xpath: the XPath string to evaluate
|
* @xpath: the XPath string to evaluate
|
||||||
@ -278,6 +307,35 @@ virXPathULongBase(const char *xpath,
|
|||||||
return (ret);
|
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:
|
* virXPathULong:
|
||||||
* @xpath: the XPath string to evaluate
|
* @xpath: the XPath string to evaluate
|
||||||
|
@ -21,19 +21,25 @@ char * virXPathStringLimit(const char *xpath,
|
|||||||
int virXPathNumber(const char *xpath,
|
int virXPathNumber(const char *xpath,
|
||||||
xmlXPathContextPtr ctxt,
|
xmlXPathContextPtr ctxt,
|
||||||
double *value);
|
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,
|
int virXPathLong(const char *xpath,
|
||||||
xmlXPathContextPtr ctxt,
|
xmlXPathContextPtr ctxt,
|
||||||
long *value);
|
long *value);
|
||||||
int virXPathULong(const char *xpath,
|
int virXPathULong(const char *xpath,
|
||||||
xmlXPathContextPtr ctxt,
|
xmlXPathContextPtr ctxt,
|
||||||
unsigned long *value);
|
unsigned long *value);
|
||||||
int virXPathULongLong(const char *xpath,
|
int virXPathULongLong(const char *xpath,
|
||||||
xmlXPathContextPtr ctxt,
|
xmlXPathContextPtr ctxt,
|
||||||
unsigned long long *value);
|
unsigned long long *value);
|
||||||
int virXPathLongLong(const char *xpath,
|
int virXPathLongLong(const char *xpath,
|
||||||
xmlXPathContextPtr ctxt,
|
xmlXPathContextPtr ctxt,
|
||||||
long long *value);
|
long long *value);
|
||||||
int virXPathLongHex (const char *xpath,
|
int virXPathLongHex(const char *xpath,
|
||||||
xmlXPathContextPtr ctxt,
|
xmlXPathContextPtr ctxt,
|
||||||
long *value);
|
long *value);
|
||||||
int virXPathULongHex(const char *xpath,
|
int virXPathULongHex(const char *xpath,
|
||||||
|
Loading…
Reference in New Issue
Block a user