Added String method that converts to long integer.

git-svn-id: http://voip.null.ro/svn/yate@4899 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
paulc 2012-02-13 08:27:48 +00:00
parent 4503260a9d
commit e88762e14f
2 changed files with 34 additions and 0 deletions

View File

@ -444,6 +444,27 @@ int String::toInteger(const TokenDict* tokens, int defvalue, int base) const
return toInteger(defvalue,base);
}
long int String::toLong(long int defvalue, int base, long int minvalue, long int maxvalue,
bool clamp) const
{
if (!m_string)
return defvalue;
char *eptr = 0;
errno = 0;
long int val = ::strtol(m_string,&eptr,base);
// on overflow/underflow mark the entire string as unreadable
if ((errno == ERANGE) && eptr)
eptr = m_string;
if (!eptr || *eptr)
return defvalue;
if (val >= minvalue && val <= maxvalue)
return val;
if (clamp)
return (val < minvalue) ? minvalue : maxvalue;
return defvalue;
}
double String::toDouble(double defvalue) const
{
if (!m_string)

View File

@ -1734,6 +1734,19 @@ public:
*/
int toInteger(const TokenDict* tokens, int defvalue = 0, int base = 0) const;
/**
* Convert the string to an long integer value.
* @param defvalue Default to return if the string is not a number
* @param base Numeration base, 0 to autodetect
* @param minvalue Minimum value allowed
* @param maxvalue Maximum value allowed
* @param clamp Control the out of bound values: true to adjust to the nearest
* bound, false to return the default value
* @return The long integer interpretation or defvalue.
*/
long int toLong(long int defvalue = 0, int base = 0, long int minvalue = LONG_MIN,
long int maxvalue = LONG_MAX, bool clamp = true) const;
/**
* Convert the string to a floating point value.
* @param defvalue Default to return if the string is not a number