2004-05-22 00:05:20 +00:00
|
|
|
/**
|
|
|
|
* NamedList.cpp
|
|
|
|
* This file is part of the YATE Project http://YATE.null.ro
|
2004-11-29 03:56:41 +00:00
|
|
|
*
|
|
|
|
* Yet Another Telephony Engine - a fully featured software PBX and IVR
|
2006-05-27 15:08:43 +00:00
|
|
|
* Copyright (C) 2004-2006 Null Team
|
2004-11-29 03:56:41 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-05-27 15:08:43 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-05-22 00:05:20 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-08 10:55:22 +00:00
|
|
|
#include "yateclass.h"
|
2004-05-22 00:05:20 +00:00
|
|
|
|
|
|
|
using namespace TelEngine;
|
|
|
|
|
2008-10-22 13:32:25 +00:00
|
|
|
static const NamedList s_empty("");
|
|
|
|
|
|
|
|
const NamedList& NamedList::empty()
|
|
|
|
{
|
|
|
|
return s_empty;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
NamedList::NamedList(const char* name)
|
2004-05-22 00:05:20 +00:00
|
|
|
: String(name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-02-21 14:56:39 +00:00
|
|
|
NamedList::NamedList(const NamedList& original)
|
|
|
|
: String(original)
|
|
|
|
{
|
2011-02-23 10:26:26 +00:00
|
|
|
for (const ObjList* l = original.m_params.skipNull(); l; l = l->skipNext()) {
|
|
|
|
const NamedString* p = static_cast<const NamedString*>(l->get());
|
|
|
|
m_params.append(new NamedString(p->name(),*p));
|
2006-02-21 14:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-31 13:36:37 +00:00
|
|
|
NamedList::NamedList(const char* name, const NamedList& original, const String& prefix)
|
|
|
|
: String(name)
|
|
|
|
{
|
|
|
|
copySubParams(original,prefix);
|
|
|
|
}
|
|
|
|
|
2010-05-04 10:14:59 +00:00
|
|
|
NamedList& NamedList::operator=(const NamedList& value)
|
|
|
|
{
|
|
|
|
String::operator=(value);
|
|
|
|
clearParams();
|
|
|
|
return copyParams(value);
|
|
|
|
}
|
|
|
|
|
2006-03-22 18:22:34 +00:00
|
|
|
void* NamedList::getObject(const String& name) const
|
|
|
|
{
|
2011-06-03 12:26:53 +00:00
|
|
|
if (name == YSTRING("NamedList"))
|
2006-03-22 18:22:34 +00:00
|
|
|
return const_cast<NamedList*>(this);
|
|
|
|
return String::getObject(name);
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
NamedList& NamedList::addParam(NamedString* param)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugInfo,"NamedList::addParam(%p) [\"%s\",\"%s\"]",
|
2011-02-23 10:26:26 +00:00
|
|
|
param,(param ? param->name().c_str() : ""),TelEngine::c_safe(param));
|
|
|
|
if (param)
|
|
|
|
m_params.append(param);
|
2004-05-22 00:05:20 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-05-04 10:35:52 +00:00
|
|
|
NamedList& NamedList::addParam(const char* name, const char* value, bool emptyOK)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2010-05-04 10:35:52 +00:00
|
|
|
XDebug(DebugInfo,"NamedList::addParam(\"%s\",\"%s\",%s)",name,value,String::boolText(emptyOK));
|
|
|
|
if (emptyOK || !TelEngine::null(value))
|
|
|
|
m_params.append(new NamedString(name, value));
|
2004-05-22 00:05:20 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
NamedList& NamedList::setParam(NamedString* param)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugInfo,"NamedList::setParam(%p) [\"%s\",\"%s\"]",
|
2011-02-23 10:26:26 +00:00
|
|
|
param,(param ? param->name().c_str() : ""),TelEngine::c_safe(param));
|
2008-01-17 15:40:47 +00:00
|
|
|
if (!param)
|
|
|
|
return *this;
|
|
|
|
ObjList* p = m_params.find(param->name());
|
|
|
|
if (p)
|
|
|
|
p->set(param);
|
2004-05-22 00:05:20 +00:00
|
|
|
else
|
|
|
|
m_params.append(param);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
NamedList& NamedList::setParam(const char* name, const char* value)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugInfo,"NamedList::setParam(\"%s\",\"%s\")",name,value);
|
2004-05-22 00:05:20 +00:00
|
|
|
NamedString *s = getParam(name);
|
|
|
|
if (s)
|
|
|
|
*s = value;
|
|
|
|
else
|
|
|
|
m_params.append(new NamedString(name, value));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2006-11-30 00:53:15 +00:00
|
|
|
NamedList& NamedList::clearParam(const String& name, char childSep)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2006-11-30 00:53:15 +00:00
|
|
|
XDebug(DebugInfo,"NamedList::clearParam(\"%s\",'%1s')",
|
|
|
|
name.c_str(),&childSep);
|
|
|
|
String tmp;
|
|
|
|
if (childSep)
|
|
|
|
tmp << name << childSep;
|
2004-05-22 00:05:20 +00:00
|
|
|
ObjList *p = &m_params;
|
|
|
|
while (p) {
|
|
|
|
NamedString *s = static_cast<NamedString *>(p->get());
|
2006-11-30 00:53:15 +00:00
|
|
|
if (s && ((s->name() == name) || s->name().startsWith(tmp)))
|
2004-05-22 00:05:20 +00:00
|
|
|
p->remove();
|
|
|
|
else
|
|
|
|
p = p->next();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-02-18 12:59:50 +00:00
|
|
|
// Remove a specific parameter
|
|
|
|
NamedList& NamedList::clearParam(NamedString* param)
|
|
|
|
{
|
|
|
|
if (!param)
|
|
|
|
return *this;
|
|
|
|
ObjList* o = m_params.find(param);
|
|
|
|
if (o)
|
|
|
|
o->remove();
|
|
|
|
XDebug(DebugInfo,"NamedList::clearParam(%p) found=%p",param,o);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2006-11-30 00:53:15 +00:00
|
|
|
NamedList& NamedList::copyParam(const NamedList& original, const String& name, char childSep)
|
|
|
|
{
|
|
|
|
XDebug(DebugInfo,"NamedList::copyParam(%p,\"%s\",'%1s')",
|
|
|
|
&original,name.c_str(),&childSep);
|
|
|
|
if (!childSep) {
|
|
|
|
// faster and simpler - used in most cases
|
|
|
|
const NamedString* s = original.getParam(name);
|
|
|
|
return s ? setParam(name,*s) : clearParam(name);
|
|
|
|
}
|
|
|
|
clearParam(name,childSep);
|
|
|
|
String tmp;
|
|
|
|
tmp << name << childSep;
|
2011-02-23 10:26:26 +00:00
|
|
|
for (const ObjList* l = original.m_params.skipNull(); l; l = l->skipNext()) {
|
|
|
|
const NamedString* s = static_cast<const NamedString*>(l->get());
|
|
|
|
if ((s->name() == name) || s->name().startsWith(tmp))
|
2006-11-30 00:53:15 +00:00
|
|
|
addParam(s->name(),*s);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-10-14 10:15:01 +00:00
|
|
|
NamedList& NamedList::copyParams(const NamedList& original)
|
|
|
|
{
|
|
|
|
XDebug(DebugInfo,"NamedList::copyParams(%p) [%p]",&original,this);
|
2011-02-23 10:26:26 +00:00
|
|
|
for (const ObjList* l = original.m_params.skipNull(); l; l = l->skipNext()) {
|
|
|
|
const NamedString* p = static_cast<const NamedString*>(l->get());
|
|
|
|
setParam(p->name(),*p);
|
2008-10-14 10:15:01 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2007-05-12 13:27:44 +00:00
|
|
|
NamedList& NamedList::copyParams(const NamedList& original, ObjList* list, char childSep)
|
|
|
|
{
|
2008-10-14 10:15:01 +00:00
|
|
|
XDebug(DebugInfo,"NamedList::copyParams(%p,%p,'%1s') [%p]",
|
|
|
|
&original,list,&childSep,this);
|
2007-05-12 13:27:44 +00:00
|
|
|
for (; list; list = list->next()) {
|
|
|
|
GenObject* obj = list->get();
|
|
|
|
if (!obj)
|
|
|
|
continue;
|
|
|
|
String name = obj->toString();
|
|
|
|
name.trimBlanks();
|
|
|
|
if (name)
|
|
|
|
copyParam(original,name,childSep);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
NamedList& NamedList::copyParams(const NamedList& original, const String& list, char childSep)
|
|
|
|
{
|
2008-10-14 10:15:01 +00:00
|
|
|
XDebug(DebugInfo,"NamedList::copyParams(%p,\"%s\",'%1s') [%p]",
|
|
|
|
&original,list.c_str(),&childSep,this);
|
2007-05-12 13:27:44 +00:00
|
|
|
ObjList* l = list.split(',',false);
|
|
|
|
if (l) {
|
|
|
|
copyParams(original,l,childSep);
|
|
|
|
l->destruct();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-03-17 19:46:26 +00:00
|
|
|
NamedList& NamedList::copySubParams(const NamedList& original, const String& prefix, bool skipPrefix)
|
2009-08-31 13:36:37 +00:00
|
|
|
{
|
2011-03-17 19:46:26 +00:00
|
|
|
XDebug(DebugInfo,"NamedList::copySubParams(%p,\"%s\",%s) [%p]",
|
|
|
|
&original,prefix.c_str(),String::boolText(skipPrefix),this);
|
2009-08-31 13:36:37 +00:00
|
|
|
if (prefix) {
|
2011-03-17 19:46:26 +00:00
|
|
|
unsigned int offs = skipPrefix ? prefix.length() : 0;
|
|
|
|
for (const ObjList* l = original.m_params.skipNull(); l; l = l->skipNext()) {
|
|
|
|
const NamedString* s = static_cast<const NamedString*>(l->get());
|
2011-02-23 10:26:26 +00:00
|
|
|
if (s->name().startsWith(prefix)) {
|
2009-08-31 13:36:37 +00:00
|
|
|
const char* name = s->name().c_str() + offs;
|
|
|
|
if (*name)
|
|
|
|
addParam(name,*s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-05-13 10:13:45 +00:00
|
|
|
void NamedList::dump(String& str, const char* separator, char quote, bool force) const
|
|
|
|
{
|
|
|
|
if (force && str.null())
|
|
|
|
str << separator;
|
|
|
|
str << quote << *this << quote;
|
|
|
|
const ObjList *p = m_params.skipNull();
|
|
|
|
for (; p; p = p->skipNext()) {
|
|
|
|
const NamedString* s = static_cast<const NamedString *>(p->get());
|
|
|
|
String tmp;
|
|
|
|
tmp << quote << s->name() << quote << "=" << quote << *s << quote;
|
|
|
|
str.append(tmp,separator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-30 00:53:15 +00:00
|
|
|
int NamedList::getIndex(const NamedString* param) const
|
|
|
|
{
|
|
|
|
if (!param)
|
|
|
|
return -1;
|
|
|
|
const ObjList *p = &m_params;
|
|
|
|
for (int i=0; p; p=p->next(),i++) {
|
|
|
|
if (static_cast<const NamedString *>(p->get()) == param)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NamedList::getIndex(const String& name) const
|
|
|
|
{
|
|
|
|
const ObjList *p = &m_params;
|
|
|
|
for (int i=0; p; p=p->next(),i++) {
|
|
|
|
NamedString *s = static_cast<NamedString *>(p->get());
|
|
|
|
if (s && (s->name() == name))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
NamedString* NamedList::getParam(const String& name) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugInfo,"NamedList::getParam(\"%s\")",name.c_str());
|
2005-04-08 12:45:19 +00:00
|
|
|
const ObjList *p = m_params.skipNull();
|
2006-11-30 00:53:15 +00:00
|
|
|
for (; p; p=p->skipNext()) {
|
2004-05-22 00:05:20 +00:00
|
|
|
NamedString *s = static_cast<NamedString *>(p->get());
|
2005-04-08 12:45:19 +00:00
|
|
|
if (s->name() == name)
|
2004-05-22 00:05:20 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
NamedString* NamedList::getParam(unsigned int index) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugInfo,"NamedList::getParam(%u)",index);
|
2005-03-20 03:11:53 +00:00
|
|
|
return static_cast<NamedString *>(m_params[index]);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2008-01-30 17:55:29 +00:00
|
|
|
const String& NamedList::operator[](const String& name) const
|
|
|
|
{
|
|
|
|
const String* s = getParam(name);
|
|
|
|
return s ? *s : String::empty();
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
const char* NamedList::getValue(const String& name, const char* defvalue) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugInfo,"NamedList::getValue(\"%s\",\"%s\")",name.c_str(),defvalue);
|
2004-05-22 00:05:20 +00:00
|
|
|
const NamedString *s = getParam(name);
|
|
|
|
return s ? s->c_str() : defvalue;
|
|
|
|
}
|
2005-03-28 00:58:26 +00:00
|
|
|
|
2011-08-23 15:14:28 +00:00
|
|
|
int NamedList::getIntValue(const String& name, int defvalue, int minvalue, int maxvalue,
|
2011-08-23 15:47:01 +00:00
|
|
|
bool clamp) const
|
2005-04-19 00:45:40 +00:00
|
|
|
{
|
|
|
|
const NamedString *s = getParam(name);
|
2011-08-23 15:47:01 +00:00
|
|
|
return s ? s->toInteger(defvalue,0,minvalue,maxvalue,clamp) : defvalue;
|
2005-04-19 00:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int NamedList::getIntValue(const String& name, const TokenDict* tokens, int defvalue) const
|
|
|
|
{
|
|
|
|
const NamedString *s = getParam(name);
|
|
|
|
return s ? s->toInteger(tokens,defvalue) : defvalue;
|
|
|
|
}
|
|
|
|
|
2006-03-13 20:28:34 +00:00
|
|
|
double NamedList::getDoubleValue(const String& name, double defvalue) const
|
|
|
|
{
|
|
|
|
const NamedString *s = getParam(name);
|
|
|
|
return s ? s->toDouble(defvalue) : defvalue;
|
|
|
|
}
|
|
|
|
|
2005-04-19 00:45:40 +00:00
|
|
|
bool NamedList::getBoolValue(const String& name, bool defvalue) const
|
|
|
|
{
|
|
|
|
const NamedString *s = getParam(name);
|
|
|
|
return s ? s->toBoolean(defvalue) : defvalue;
|
|
|
|
}
|
2006-03-16 11:33:06 +00:00
|
|
|
|
|
|
|
int NamedList::replaceParams(String& str, bool sqlEsc, char extraEsc) const
|
|
|
|
{
|
2008-08-29 11:46:26 +00:00
|
|
|
int p1 = 0;
|
2006-03-16 11:33:06 +00:00
|
|
|
int cnt = 0;
|
2008-08-29 11:46:26 +00:00
|
|
|
while ((p1 = str.find("${",p1)) >= 0) {
|
2006-03-16 11:33:06 +00:00
|
|
|
int p2 = str.find('}',p1+2);
|
|
|
|
if (p2 > 0) {
|
2009-09-25 13:13:58 +00:00
|
|
|
String def;
|
2006-03-16 11:33:06 +00:00
|
|
|
String tmp = str.substr(p1+2,p2-p1-2);
|
|
|
|
tmp.trimBlanks();
|
2009-09-25 13:13:58 +00:00
|
|
|
int pq = tmp.find('$');
|
|
|
|
if (pq >= 0) {
|
|
|
|
// param is in ${<name>$<default>} format
|
|
|
|
def = tmp.substr(pq+1).trimBlanks();
|
|
|
|
tmp = tmp.substr(0,pq).trimBlanks();
|
|
|
|
}
|
2006-03-16 11:33:06 +00:00
|
|
|
DDebug(DebugAll,"NamedList replacing parameter '%s' [%p]",tmp.c_str(),this);
|
2009-09-25 13:13:58 +00:00
|
|
|
const String* ns = getParam(tmp);
|
2009-02-06 19:37:01 +00:00
|
|
|
if (ns) {
|
|
|
|
if (sqlEsc) {
|
|
|
|
const DataBlock* data = 0;
|
|
|
|
if (ns->null()) {
|
|
|
|
NamedPointer* np = YOBJECT(NamedPointer,ns);
|
|
|
|
if (np)
|
|
|
|
data = YOBJECT(DataBlock,np->userData());
|
|
|
|
}
|
|
|
|
if (data)
|
|
|
|
tmp = data->sqlEscape(extraEsc);
|
|
|
|
else
|
|
|
|
tmp = ns->sqlEscape(extraEsc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tmp = *ns;
|
|
|
|
}
|
|
|
|
else
|
2009-09-25 13:13:58 +00:00
|
|
|
tmp = def;
|
2006-03-16 11:33:06 +00:00
|
|
|
str = str.substr(0,p1) + tmp + str.substr(p2+1);
|
2008-08-29 11:46:26 +00:00
|
|
|
// advance search offset past the string we just replaced
|
|
|
|
p1 += tmp.length();
|
2008-08-29 18:46:04 +00:00
|
|
|
cnt++;
|
2006-03-16 11:33:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
2010-05-04 10:14:59 +00:00
|
|
|
|
2005-03-28 00:58:26 +00:00
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|