Added the case-insensitive startSkip almost in the form suggested by Olaf Conradi.

Added methods for converting a String or Configuration entry to double.


git-svn-id: http://yate.null.ro/svn/yate/trunk@711 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
paulc 2006-03-13 20:28:34 +00:00
parent 20eacad589
commit 4fb35c4015
5 changed files with 66 additions and 7 deletions

View File

@ -89,6 +89,12 @@ int Configuration::getIntValue(const String& sect, const String& key, const Toke
return s ? s->toInteger(tokens,defvalue) : defvalue; return s ? s->toInteger(tokens,defvalue) : defvalue;
} }
double Configuration::getDoubleValue(const String& sect, const String& key, double defvalue) const
{
const NamedString *s = getKey(sect,key);
return s ? s->toDouble(defvalue) : defvalue;
}
bool Configuration::getBoolValue(const String& sect, const String& key, bool defvalue) const bool Configuration::getBoolValue(const String& sect, const String& key, bool defvalue) const
{ {
const NamedString *s = getKey(sect,key); const NamedString *s = getKey(sect,key);

View File

@ -130,6 +130,12 @@ int NamedList::getIntValue(const String& name, const TokenDict* tokens, int defv
return s ? s->toInteger(tokens,defvalue) : defvalue; return s ? s->toInteger(tokens,defvalue) : defvalue;
} }
double NamedList::getDoubleValue(const String& name, double defvalue) const
{
const NamedString *s = getParam(name);
return s ? s->toDouble(defvalue) : defvalue;
}
bool NamedList::getBoolValue(const String& name, bool defvalue) const bool NamedList::getBoolValue(const String& name, bool defvalue) const
{ {
const NamedString *s = getParam(name); const NamedString *s = getParam(name);

View File

@ -339,6 +339,17 @@ int String::toInteger(const TokenDict* tokens, int defvalue, int base) const
return toInteger(defvalue,base); return toInteger(defvalue,base);
} }
double String::toDouble(double defvalue) const
{
if (!m_string)
return defvalue;
char *eptr = 0;
double val= ::strtod(m_string,&eptr);
if (!eptr || *eptr)
return defvalue;
return val;
}
static const char* str_false[] = { "false", "no", "off", "disable", 0 }; static const char* str_false[] = { "false", "no", "off", "disable", 0 };
static const char* str_true[] = { "true", "yes", "on", "enable", 0 }; static const char* str_true[] = { "true", "yes", "on", "enable", 0 };
@ -641,7 +652,7 @@ int String::rfind(char what) const
return s ? s-m_string : -1; return s ? s-m_string : -1;
} }
bool String::startsWith(const char* what, bool wordBreak) const bool String::startsWith(const char* what, bool wordBreak, bool caseInsensitive) const
{ {
if (!(m_string && what && *what)) if (!(m_string && what && *what))
return false; return false;
@ -650,12 +661,15 @@ bool String::startsWith(const char* what, bool wordBreak) const
return false; return false;
else if (wordBreak && (m_length > l) && !isWordBreak(m_string[l])) else if (wordBreak && (m_length > l) && !isWordBreak(m_string[l]))
return false; return false;
if (caseInsensitive)
return (::strncasecmp(m_string,what,l) == 0);
return (::strncmp(m_string,what,l) == 0); return (::strncmp(m_string,what,l) == 0);
} }
bool String::startSkip(const char* what, bool wordBreak) bool String::startSkip(const char* what, bool wordBreak, bool caseInsensitive)
{ {
if (startsWith(what,wordBreak)) { if (startsWith(what,wordBreak,caseInsensitive)) {
const char *p = m_string + ::strlen(what); const char *p = m_string + ::strlen(what);
if (wordBreak) if (wordBreak)
while (isWordBreak(*p)) while (isWordBreak(*p))
@ -666,7 +680,7 @@ bool String::startSkip(const char* what, bool wordBreak)
return false; return false;
} }
bool String::endsWith(const char* what, bool wordBreak) const bool String::endsWith(const char* what, bool wordBreak, bool caseInsensitive) const
{ {
if (!(m_string && what && *what)) if (!(m_string && what && *what))
return false; return false;
@ -675,6 +689,8 @@ bool String::endsWith(const char* what, bool wordBreak) const
return false; return false;
else if (wordBreak && (m_length > l) && !isWordBreak(m_string[m_length-l-1])) else if (wordBreak && (m_length > l) && !isWordBreak(m_string[m_length-l-1]))
return false; return false;
if (caseInsensitive)
return (::strncasecmp(m_string+m_length-l,what,l) == 0);
return (::strncmp(m_string+m_length-l,what,l) == 0); return (::strncmp(m_string+m_length-l,what,l) == 0);
} }

View File

@ -79,6 +79,10 @@ typedef unsigned long in_addr_t;
#define strcasecmp _stricmp #define strcasecmp _stricmp
#endif #endif
#ifndef strncasecmp
#define strncasecmp _strnicmp
#endif
#define vsnprintf _vsnprintf #define vsnprintf _vsnprintf
#define snprintf _snprintf #define snprintf _snprintf
#define strdup _strdup #define strdup _strdup
@ -1192,6 +1196,13 @@ public:
*/ */
int toInteger(const TokenDict* tokens, int defvalue = 0, int base = 0) const; int toInteger(const TokenDict* tokens, int defvalue = 0, int base = 0) const;
/**
* Convert the string to a floating point value.
* @param defvalue Default to return if the string is not a number
* @return The floating-point interpretation or defvalue.
*/
double toDouble(double defvalue = 0.0) const;
/** /**
* Convert the string to a boolean value. * Convert the string to a boolean value.
* @param defvalue Default to return if the string is not a bool * @param defvalue Default to return if the string is not a bool
@ -1426,17 +1437,19 @@ public:
* Checks if the string starts with a substring * Checks if the string starts with a substring
* @param what Substring to search for * @param what Substring to search for
* @param wordBreak Check if a word boundary follows the substring * @param wordBreak Check if a word boundary follows the substring
* @param caseInsensitive Compare case-insensitive if set
* @return True if the substring occurs at the beginning of the string * @return True if the substring occurs at the beginning of the string
*/ */
bool startsWith(const char* what, bool wordBreak = false) const; bool startsWith(const char* what, bool wordBreak = false, bool caseInsensitive = false) const;
/** /**
* Checks if the string ends with a substring * Checks if the string ends with a substring
* @param what Substring to search for * @param what Substring to search for
* @param wordBreak Check if a word boundary precedes the substring * @param wordBreak Check if a word boundary precedes the substring
* @param caseInsensitive Compare case-insensitive if set
* @return True if the substring occurs at the end of the string * @return True if the substring occurs at the end of the string
*/ */
bool endsWith(const char* what, bool wordBreak = false) const; bool endsWith(const char* what, bool wordBreak = false, bool caseInsensitive = false) const;
/** /**
* Checks if the string starts with a substring and removes it * Checks if the string starts with a substring and removes it
@ -1444,11 +1457,12 @@ public:
* @param wordBreak Check if a word boundary follows the substring; * @param wordBreak Check if a word boundary follows the substring;
* this parameter defaults to True because the intended use of this * this parameter defaults to True because the intended use of this
* method is to separate commands from their parameters * method is to separate commands from their parameters
* @param caseInsensitive Compare case-insensitive if set
* @return True if the substring occurs at the beginning of the string * @return True if the substring occurs at the beginning of the string
* and also removes the substring; if wordBreak is True any word * and also removes the substring; if wordBreak is True any word
* breaking characters are also removed * breaking characters are also removed
*/ */
bool startSkip(const char* what, bool wordBreak = true); bool startSkip(const char* what, bool wordBreak = true, bool caseInsensitive = false);
/** /**
* Checks if matches another string * Checks if matches another string
@ -2461,6 +2475,14 @@ public:
*/ */
int getIntValue(const String& name, const TokenDict* tokens, int defvalue = 0) const; int getIntValue(const String& name, const TokenDict* tokens, int defvalue = 0) const;
/**
* Retrive the floating point value of a parameter.
* @param name Name of parameter to locate
* @param defvalue Default value to return if not found
* @return The number contained in the named parameter or the default
*/
double getDoubleValue(const String& name, double defvalue = 0.0) const;
/** /**
* Retrive the boolean value of a parameter. * Retrive the boolean value of a parameter.
* @param name Name of parameter to locate * @param name Name of parameter to locate

View File

@ -117,6 +117,15 @@ public:
*/ */
int getIntValue(const String& sect, const String& key, const TokenDict* tokens, int defvalue = 0) const; int getIntValue(const String& sect, const String& key, const TokenDict* tokens, int defvalue = 0) const;
/**
* Retrive the floating point value of a key in a section.
* @param sect Name of the section
* @param key Name of the key in section
* @param defvalue Default value to return if not found
* @return The numeric value contained in the key or the default
*/
double getDoubleValue(const String& sect, const String& key, double defvalue = 0.0) const;
/** /**
* Retrive the boolean value of a key in a section. * Retrive the boolean value of a key in a section.
* @param sect Name of the section * @param sect Name of the section