Added replaceParams method in NamedList.

git-svn-id: http://yate.null.ro/svn/yate/trunk@713 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
paulc 2006-03-16 11:33:06 +00:00
parent 0787185738
commit aa294918c2
2 changed files with 35 additions and 0 deletions

View File

@ -141,5 +141,31 @@ bool NamedList::getBoolValue(const String& name, bool defvalue) const
const NamedString *s = getParam(name);
return s ? s->toBoolean(defvalue) : defvalue;
}
int NamedList::replaceParams(String& str, bool sqlEsc, char extraEsc) const
{
int p1;
int cnt = 0;
while ((p1 = str.find("${")) >= 0) {
int p2 = str.find('}',p1+2);
if (p2 > 0) {
String tmp = str.substr(p1+2,p2-p1-2);
tmp.trimBlanks();
DDebug(DebugAll,"NamedList replacing parameter '%s' [%p]",tmp.c_str(),this);
tmp = getValue(tmp);
if (sqlEsc)
tmp = tmp.sqlEscape(extraEsc);
str = str.substr(0,p1) + tmp + str.substr(p2+1);
// put a limit to avoid infinite loops
if (++cnt >= 1000) {
Debug(DebugWarn,"NamedList reached count %d replacing into '%s' [%p]",cnt,str.c_str(),this);
return -1;
}
}
else
return -1;
}
return cnt;
}
/* vi: set ts=8 sw=4 sts=4 noet: */

View File

@ -2491,6 +2491,15 @@ public:
*/
bool getBoolValue(const String& name, bool defvalue = false) const;
/**
* Replaces all ${paramname} in a String with the corresponding parameters
* @param str String in which the replacements will be made
* @param sqlEsc True to apply SQL escaping to parameter values
* @param extraEsc Character to escape other than the SQL default ones
* @return Number of replacements made, -1 if an error occured
*/
int replaceParams(String& str, bool sqlEsc = false, char extraEsc = 0) const;
private:
NamedList(); // no default constructor please
NamedList& operator=(const NamedList& value); // no assignment please