2004-05-22 00:05:20 +00:00
|
|
|
/**
|
|
|
|
* String.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
|
|
|
|
2007-10-29 17:51:45 +00:00
|
|
|
#include <errno.h>
|
2004-05-22 00:05:20 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <regex.h>
|
|
|
|
|
|
|
|
namespace TelEngine {
|
|
|
|
|
2007-10-29 17:51:45 +00:00
|
|
|
// String to regular integer conversion, takes into account overflows
|
|
|
|
static int strtoi(const char* nptr, char** endptr, int base)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
long int val = ::strtol(nptr,endptr,base);
|
|
|
|
#if INT_MAX != LONG_MAX
|
|
|
|
if (val >= INT_MAX) {
|
|
|
|
errno = ERANGE;
|
|
|
|
val = INT_MAX;
|
|
|
|
}
|
|
|
|
else if (val <= INT_MIN) {
|
|
|
|
errno = ERANGE;
|
|
|
|
val = INT_MIN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// on overflow/underflow mark the entire string as unreadable
|
|
|
|
if ((errno == ERANGE) && endptr)
|
|
|
|
*endptr = (char*) nptr;
|
|
|
|
return (int) val;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String operator+(const String& s1, const String& s2)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
String s(s1.c_str());
|
|
|
|
s += s2.c_str();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String operator+(const String& s1, const char* s2)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
String s(s1.c_str());
|
|
|
|
s += s2;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String operator+(const char* s1, const String& s2)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
String s(s1);
|
|
|
|
s += s2;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
int lookup(const char* str, const TokenDict* tokens, int defvalue, int base)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (!str)
|
|
|
|
return defvalue;
|
|
|
|
if (tokens) {
|
|
|
|
for (; tokens->token; tokens++)
|
|
|
|
if (!::strcmp(str,tokens->token))
|
|
|
|
return tokens->value;
|
|
|
|
}
|
|
|
|
char *eptr = 0;
|
2007-10-29 17:51:45 +00:00
|
|
|
int val = strtoi(str,&eptr,base);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (!eptr || *eptr)
|
|
|
|
return defvalue;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
const char* lookup(int value, const TokenDict* tokens, const char* defvalue)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (tokens) {
|
|
|
|
for (; tokens->token; tokens++)
|
|
|
|
if (value == tokens->value)
|
|
|
|
return tokens->token;
|
|
|
|
}
|
|
|
|
return defvalue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_MATCH 9
|
|
|
|
|
|
|
|
class StringMatchPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StringMatchPrivate();
|
|
|
|
void fixup();
|
|
|
|
void clear();
|
|
|
|
int count;
|
|
|
|
regmatch_t rmatch[MAX_MATCH+1];
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
using namespace TelEngine;
|
|
|
|
|
|
|
|
static bool isWordBreak(char c, bool nullOk = false)
|
|
|
|
{
|
2008-12-19 14:16:53 +00:00
|
|
|
return (c == ' ' || c == '\t' || c == '\r' || c == '\n' || (nullOk && !c));
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2006-05-18 15:22:55 +00:00
|
|
|
// Decode a single nibble, return -1 on error
|
|
|
|
static int hexDecode(char c)
|
|
|
|
{
|
|
|
|
if (('0' <= c) && (c <= '9'))
|
|
|
|
return c - '0';
|
|
|
|
if (('A' <= c) && (c <= 'F'))
|
|
|
|
return c - 'A' + 10;
|
|
|
|
if (('a' <= c) && (c <= 'f'))
|
|
|
|
return c - 'a' + 10;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode a single nibble
|
|
|
|
static inline char hexEncode(char nib)
|
|
|
|
{
|
|
|
|
static const char hex[] = "0123456789abcdef";
|
|
|
|
return hex[nib & 0x0f];
|
|
|
|
}
|
|
|
|
|
2007-10-29 17:51:45 +00:00
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
StringMatchPrivate::StringMatchPrivate()
|
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"StringMatchPrivate::StringMatchPrivate() [%p]",this);
|
2004-05-22 00:05:20 +00:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringMatchPrivate::clear()
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
for (int i = 0; i <= MAX_MATCH; i++) {
|
|
|
|
rmatch[i].rm_so = -1;
|
|
|
|
rmatch[i].rm_eo = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringMatchPrivate::fixup()
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
rmatch[0].rm_so = rmatch[1].rm_so;
|
|
|
|
rmatch[0].rm_eo = 0;
|
|
|
|
int i, c = 0;
|
|
|
|
for (i = 1; i <= MAX_MATCH; i++) {
|
|
|
|
if (rmatch[i].rm_so != -1) {
|
|
|
|
rmatch[0].rm_eo = rmatch[i].rm_eo - rmatch[0].rm_so;
|
|
|
|
rmatch[i].rm_eo -= rmatch[i].rm_so;
|
|
|
|
c = i;
|
|
|
|
}
|
2005-01-01 22:21:32 +00:00
|
|
|
else
|
|
|
|
rmatch[i].rm_eo = 0;
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
// Cope with the regexp stupidity.
|
|
|
|
if (c > 1) {
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
rmatch[i] = rmatch[i+1];
|
|
|
|
rmatch[c].rm_so = -1;
|
|
|
|
c--;
|
|
|
|
}
|
|
|
|
count = c;
|
|
|
|
}
|
|
|
|
|
2007-10-29 17:51:45 +00:00
|
|
|
|
2008-10-22 13:32:25 +00:00
|
|
|
static const String s_empty;
|
2013-04-12 13:19:14 +00:00
|
|
|
static ObjList s_atoms;
|
|
|
|
static Mutex s_mutex(false,"Atom");
|
2004-12-04 15:44:54 +00:00
|
|
|
|
|
|
|
const String& String::empty()
|
|
|
|
{
|
|
|
|
return s_empty;
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
String::String()
|
2013-02-14 14:29:41 +00:00
|
|
|
: m_string(0), m_length(0), m_hash(YSTRING_INIT_HASH), m_matches(0)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"String::String() [%p]",this);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String::String(const char* value, int len)
|
2013-02-14 14:29:41 +00:00
|
|
|
: m_string(0), m_length(0), m_hash(YSTRING_INIT_HASH), m_matches(0)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"String::String(\"%s\",%d) [%p]",value,len,this);
|
2004-05-22 00:05:20 +00:00
|
|
|
assign(value,len);
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String::String(const String& value)
|
2009-06-19 11:19:20 +00:00
|
|
|
: GenObject(),
|
2013-02-14 14:29:41 +00:00
|
|
|
m_string(0), m_length(0), m_hash(YSTRING_INIT_HASH), m_matches(0)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"String::String(%p) [%p]",&value,this);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (!value.null()) {
|
|
|
|
m_string = ::strdup(value.c_str());
|
2005-01-17 23:38:12 +00:00
|
|
|
if (!m_string)
|
|
|
|
Debug("String",DebugFail,"strdup() returned NULL!");
|
2013-04-12 13:19:14 +00:00
|
|
|
else
|
|
|
|
m_length = value.length();
|
2004-05-22 00:05:20 +00:00
|
|
|
changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String::String(char value, unsigned int repeat)
|
2013-02-14 14:29:41 +00:00
|
|
|
: m_string(0), m_length(0), m_hash(YSTRING_INIT_HASH), m_matches(0)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"String::String('%c',%d) [%p]",value,repeat,this);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (value && repeat) {
|
|
|
|
m_string = (char *) ::malloc(repeat+1);
|
2005-01-17 23:38:12 +00:00
|
|
|
if (m_string) {
|
|
|
|
::memset(m_string,value,repeat);
|
|
|
|
m_string[repeat] = 0;
|
2013-04-12 13:19:14 +00:00
|
|
|
m_length = repeat;
|
2005-01-17 23:38:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Debug("String",DebugFail,"malloc(%d) returned NULL!",repeat+1);
|
2004-05-22 00:05:20 +00:00
|
|
|
changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String::String(int value)
|
2013-02-14 14:29:41 +00:00
|
|
|
: m_string(0), m_length(0), m_hash(YSTRING_INIT_HASH), m_matches(0)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"String::String(%d) [%p]",value,this);
|
2004-05-22 00:05:20 +00:00
|
|
|
char buf[64];
|
|
|
|
::sprintf(buf,"%d",value);
|
|
|
|
m_string = ::strdup(buf);
|
2005-01-17 23:38:12 +00:00
|
|
|
if (!m_string)
|
|
|
|
Debug("String",DebugFail,"strdup() returned NULL!");
|
2004-05-22 00:05:20 +00:00
|
|
|
changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
String::String(unsigned int value)
|
2013-02-14 14:29:41 +00:00
|
|
|
: m_string(0), m_length(0), m_hash(YSTRING_INIT_HASH), m_matches(0)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"String::String(%u) [%p]",value,this);
|
2004-05-22 00:05:20 +00:00
|
|
|
char buf[64];
|
|
|
|
::sprintf(buf,"%u",value);
|
|
|
|
m_string = ::strdup(buf);
|
2005-01-17 23:38:12 +00:00
|
|
|
if (!m_string)
|
|
|
|
Debug("String",DebugFail,"strdup() returned NULL!");
|
2004-05-22 00:05:20 +00:00
|
|
|
changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
String::String(bool value)
|
2013-02-14 14:29:41 +00:00
|
|
|
: m_string(0), m_length(0), m_hash(YSTRING_INIT_HASH), m_matches(0)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"String::String(%u) [%p]",value,this);
|
2005-03-20 03:11:53 +00:00
|
|
|
m_string = ::strdup(boolText(value));
|
2005-01-17 23:38:12 +00:00
|
|
|
if (!m_string)
|
|
|
|
Debug("String",DebugFail,"strdup() returned NULL!");
|
2004-05-22 00:05:20 +00:00
|
|
|
changed();
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String::String(const String* value)
|
2013-02-14 14:29:41 +00:00
|
|
|
: m_string(0), m_length(0), m_hash(YSTRING_INIT_HASH), m_matches(0)
|
2005-01-04 02:22:43 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"String::String(%p) [%p]",&value,this);
|
2005-01-04 02:22:43 +00:00
|
|
|
if (value && !value->null()) {
|
|
|
|
m_string = ::strdup(value->c_str());
|
2005-01-17 23:38:12 +00:00
|
|
|
if (!m_string)
|
|
|
|
Debug("String",DebugFail,"strdup() returned NULL!");
|
2013-04-12 13:19:14 +00:00
|
|
|
else
|
|
|
|
m_length = value->length();
|
2005-01-04 02:22:43 +00:00
|
|
|
changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
String::~String()
|
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"String::~String() [%p] (\"%s\")",this,m_string);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (m_matches) {
|
|
|
|
StringMatchPrivate *odata = m_matches;
|
|
|
|
m_matches = 0;
|
|
|
|
delete odata;
|
|
|
|
}
|
|
|
|
if (m_string) {
|
|
|
|
char *odata = m_string;
|
|
|
|
m_length = 0;
|
|
|
|
m_string = 0;
|
|
|
|
::free(odata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String& String::assign(const char* value, int len)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (len && value && *value) {
|
2005-04-22 20:07:26 +00:00
|
|
|
if (len < 0)
|
|
|
|
len = ::strlen(value);
|
|
|
|
else {
|
|
|
|
int l = 0;
|
|
|
|
for (const char* p = value; l < len; l++)
|
|
|
|
if (!*p++)
|
|
|
|
break;
|
|
|
|
len = l;
|
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
if (value != m_string || len != (int)m_length) {
|
2006-06-03 16:08:48 +00:00
|
|
|
char* data = (char*) ::malloc(len+1);
|
2005-01-17 23:38:12 +00:00
|
|
|
if (data) {
|
|
|
|
::memcpy(data,value,len);
|
|
|
|
data[len] = 0;
|
2006-06-03 16:08:48 +00:00
|
|
|
char* odata = m_string;
|
2005-01-17 23:38:12 +00:00
|
|
|
m_string = data;
|
2013-04-12 13:19:14 +00:00
|
|
|
m_length = len;
|
2005-01-17 23:38:12 +00:00
|
|
|
changed();
|
|
|
|
if (odata)
|
|
|
|
::free(odata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Debug("String",DebugFail,"malloc(%d) returned NULL!",len+1);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2006-06-03 16:08:48 +00:00
|
|
|
String& String::assign(char value, unsigned int repeat)
|
|
|
|
{
|
|
|
|
if (repeat && value) {
|
|
|
|
char* data = (char*) ::malloc(repeat+1);
|
|
|
|
if (data) {
|
|
|
|
::memset(data,value,repeat);
|
|
|
|
data[repeat] = 0;
|
|
|
|
char* odata = m_string;
|
|
|
|
m_string = data;
|
2013-04-12 13:19:14 +00:00
|
|
|
m_length = repeat;
|
2006-06-03 16:08:48 +00:00
|
|
|
changed();
|
|
|
|
if (odata)
|
|
|
|
::free(odata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Debug("String",DebugFail,"malloc(%d) returned NULL!",repeat+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::hexify(void* data, unsigned int len, char sep, bool upCase)
|
|
|
|
{
|
|
|
|
const char* hex = upCase ? "0123456789ABCDEF" : "0123456789abcdef";
|
|
|
|
if (data && len) {
|
|
|
|
const unsigned char* s = (const unsigned char*) data;
|
|
|
|
unsigned int repeat = sep ? 3*len-1 : 2*len;
|
|
|
|
// I know it's ugly to reuse but... copy/paste...
|
|
|
|
char* data = (char*) ::malloc(repeat+1);
|
|
|
|
if (data) {
|
|
|
|
char* d = data;
|
|
|
|
while (len--) {
|
|
|
|
unsigned char c = *s++;
|
|
|
|
*d++ = hex[(c >> 4) & 0x0f];
|
|
|
|
*d++ = hex[c & 0x0f];
|
|
|
|
if (sep)
|
|
|
|
*d++ = sep;
|
|
|
|
}
|
|
|
|
// wrote one too many - go back...
|
|
|
|
if (sep)
|
|
|
|
d--;
|
|
|
|
*d = '\0';
|
|
|
|
char* odata = m_string;
|
|
|
|
m_string = data;
|
2013-04-12 13:19:14 +00:00
|
|
|
m_length = repeat;
|
2006-06-03 16:08:48 +00:00
|
|
|
changed();
|
|
|
|
if (odata)
|
|
|
|
::free(odata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Debug("String",DebugFail,"malloc(%d) returned NULL!",repeat+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
void String::changed()
|
|
|
|
{
|
|
|
|
clearMatches();
|
2013-02-14 14:29:41 +00:00
|
|
|
m_hash = YSTRING_INIT_HASH;
|
2013-04-12 13:19:14 +00:00
|
|
|
if (!m_string)
|
|
|
|
m_length = 0;
|
|
|
|
else if (!m_length)
|
|
|
|
m_length = ::strlen(m_string);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void String::clear()
|
|
|
|
{
|
|
|
|
if (m_string) {
|
|
|
|
char *odata = m_string;
|
|
|
|
m_string = 0;
|
|
|
|
changed();
|
|
|
|
::free(odata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char String::at(int index) const
|
|
|
|
{
|
|
|
|
if ((index < 0) || ((unsigned)index >= m_length) || !m_string)
|
|
|
|
return 0;
|
|
|
|
return m_string[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::substr(int offs, int len) const
|
|
|
|
{
|
|
|
|
if (offs < 0) {
|
|
|
|
offs += m_length;
|
|
|
|
if (offs < 0)
|
|
|
|
offs = 0;
|
|
|
|
}
|
|
|
|
if ((unsigned int)offs >= m_length)
|
|
|
|
return String();
|
|
|
|
return String(c_str()+offs,len);
|
|
|
|
}
|
|
|
|
|
2011-08-23 15:14:28 +00:00
|
|
|
int String::toInteger(int defvalue, int base, int minvalue, int maxvalue,
|
2011-08-23 15:47:01 +00:00
|
|
|
bool clamp) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (!m_string)
|
|
|
|
return defvalue;
|
|
|
|
char *eptr = 0;
|
2007-10-29 17:51:45 +00:00
|
|
|
int val = strtoi(m_string,&eptr,base);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (!eptr || *eptr)
|
|
|
|
return defvalue;
|
2011-08-23 15:14:28 +00:00
|
|
|
if (val >= minvalue && val <= maxvalue)
|
|
|
|
return val;
|
2011-08-23 15:47:01 +00:00
|
|
|
if (clamp)
|
2011-08-23 15:14:28 +00:00
|
|
|
return (val < minvalue) ? minvalue : maxvalue;
|
|
|
|
return defvalue;
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
int String::toInteger(const TokenDict* tokens, int defvalue, int base) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (!m_string)
|
|
|
|
return defvalue;
|
|
|
|
if (tokens) {
|
|
|
|
for (; tokens->token; tokens++)
|
|
|
|
if (operator==(tokens->token))
|
|
|
|
return tokens->value;
|
|
|
|
}
|
|
|
|
return toInteger(defvalue,base);
|
|
|
|
}
|
|
|
|
|
2012-02-13 08:27:48 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-03-13 20:28:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-09-01 15:04:29 +00:00
|
|
|
static const char* str_false[] = { "false", "no", "off", "disable", "f", 0 };
|
|
|
|
static const char* str_true[] = { "true", "yes", "on", "enable", "t", 0 };
|
2004-05-22 00:05:20 +00:00
|
|
|
|
|
|
|
bool String::toBoolean(bool defvalue) const
|
|
|
|
{
|
|
|
|
if (!m_string)
|
|
|
|
return defvalue;
|
|
|
|
const char **test;
|
|
|
|
for (test=str_false; *test; test++)
|
|
|
|
if (!::strcmp(m_string,*test))
|
|
|
|
return false;
|
|
|
|
for (test=str_true; *test; test++)
|
|
|
|
if (!::strcmp(m_string,*test))
|
|
|
|
return true;
|
|
|
|
return defvalue;
|
|
|
|
}
|
|
|
|
|
2005-05-06 18:13:33 +00:00
|
|
|
bool String::isBoolean() const
|
|
|
|
{
|
|
|
|
if (!m_string)
|
|
|
|
return false;
|
|
|
|
const char **test;
|
|
|
|
for (test=str_false; *test; test++)
|
|
|
|
if (!::strcmp(m_string,*test))
|
|
|
|
return true;
|
|
|
|
for (test=str_true; *test; test++)
|
|
|
|
if (!::strcmp(m_string,*test))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-12-24 03:26:13 +00:00
|
|
|
String& String::toUpper()
|
|
|
|
{
|
|
|
|
if (m_string) {
|
2005-04-02 00:49:38 +00:00
|
|
|
char c;
|
2005-04-02 13:28:14 +00:00
|
|
|
for (char *s = m_string; (c = *s); s++) {
|
2004-12-24 03:26:13 +00:00
|
|
|
if (('a' <= c) && (c <= 'z'))
|
|
|
|
*s = c + 'A' - 'a';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::toLower()
|
|
|
|
{
|
|
|
|
if (m_string) {
|
2005-04-02 00:49:38 +00:00
|
|
|
char c;
|
2005-04-02 13:28:14 +00:00
|
|
|
for (char *s = m_string; (c = *s); s++) {
|
2004-12-24 03:26:13 +00:00
|
|
|
if (('A' <= c) && (c <= 'Z'))
|
2004-12-24 04:53:34 +00:00
|
|
|
*s = c + 'a' - 'A';
|
2004-12-24 03:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
String& String::trimBlanks()
|
|
|
|
{
|
|
|
|
if (m_string) {
|
|
|
|
const char *s = m_string;
|
|
|
|
while (*s == ' ' || *s == '\t')
|
|
|
|
s++;
|
|
|
|
const char *e = s;
|
|
|
|
for (const char *p = e; *p; p++)
|
|
|
|
if (*p != ' ' && *p != '\t')
|
|
|
|
e = p+1;
|
|
|
|
assign(s,e-s);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-12-19 14:16:53 +00:00
|
|
|
String& String::trimSpaces()
|
|
|
|
{
|
|
|
|
if (m_string) {
|
|
|
|
const char *s = m_string;
|
|
|
|
while (*s == ' ' || *s == '\t' || *s == '\v' || *s == '\f' || *s == '\r' || *s == '\n')
|
|
|
|
s++;
|
|
|
|
const char *e = s;
|
|
|
|
for (const char *p = e; *p; p++)
|
2009-01-20 12:27:53 +00:00
|
|
|
if (*p != ' ' && *p != '\t' && *p != '\v' && *p != '\f' && *p != '\r' && *p != '\n')
|
2008-12-19 14:16:53 +00:00
|
|
|
e = p+1;
|
|
|
|
assign(s,e-s);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String& String::operator=(const char* value)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-01-22 02:20:42 +00:00
|
|
|
if (value && !*value)
|
|
|
|
value = 0;
|
2004-05-22 00:05:20 +00:00
|
|
|
if (value != c_str()) {
|
|
|
|
char *tmp = m_string;
|
|
|
|
m_string = value ? ::strdup(value) : 0;
|
2013-04-12 13:19:14 +00:00
|
|
|
m_length = 0;
|
2005-01-22 02:20:42 +00:00
|
|
|
if (value && !m_string)
|
2005-01-17 23:38:12 +00:00
|
|
|
Debug("String",DebugFail,"strdup() returned NULL!");
|
2004-05-22 00:05:20 +00:00
|
|
|
changed();
|
|
|
|
if (tmp)
|
|
|
|
::free(tmp);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::operator=(char value)
|
|
|
|
{
|
|
|
|
char buf[2] = {value,0};
|
|
|
|
return operator=(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::operator=(int value)
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
::sprintf(buf,"%d",value);
|
|
|
|
return operator=(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::operator=(unsigned int value)
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
::sprintf(buf,"%u",value);
|
|
|
|
return operator=(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::operator+=(char value)
|
|
|
|
{
|
|
|
|
char buf[2] = {value,0};
|
|
|
|
return operator+=(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::operator+=(int value)
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
::sprintf(buf,"%d",value);
|
|
|
|
return operator+=(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::operator+=(unsigned int value)
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
::sprintf(buf,"%u",value);
|
|
|
|
return operator+=(buf);
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String& String::operator>>(const char* skip)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (m_string && skip && *skip) {
|
|
|
|
const char *loc = ::strstr(m_string,skip);
|
|
|
|
if (loc)
|
|
|
|
assign(loc+::strlen(skip));
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String& String::operator>>(char& store)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (m_string) {
|
|
|
|
store = m_string[0];
|
|
|
|
assign(m_string+1);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String& String::operator>>(int& store)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (m_string) {
|
|
|
|
char *end = 0;
|
2007-10-29 17:51:45 +00:00
|
|
|
int l = strtoi(m_string,&end,0);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (end && (m_string != end)) {
|
|
|
|
store = l;
|
|
|
|
assign(end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String& String::operator>>(unsigned int& store)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (m_string) {
|
|
|
|
char *end = 0;
|
2007-10-29 17:51:45 +00:00
|
|
|
errno = 0;
|
2004-05-22 00:05:20 +00:00
|
|
|
unsigned long int l = ::strtoul(m_string,&end,0);
|
2007-10-29 17:51:45 +00:00
|
|
|
#if UINT_MAX != ULONG_MAX
|
|
|
|
if (l > UINT_MAX) {
|
|
|
|
l = UINT_MAX;
|
|
|
|
errno = ERANGE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!errno && end && (m_string != end)) {
|
2004-05-22 00:05:20 +00:00
|
|
|
store = l;
|
|
|
|
assign(end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String& String::operator>>(bool& store)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (m_string) {
|
|
|
|
const char *s = m_string;
|
|
|
|
while (*s == ' ' || *s == '\t')
|
|
|
|
s++;
|
|
|
|
const char **test;
|
|
|
|
for (test=str_false; *test; test++) {
|
|
|
|
int l = ::strlen(*test);
|
|
|
|
if (!::strncmp(s,*test,l) && isWordBreak(s[l],true)) {
|
|
|
|
store = false;
|
|
|
|
assign(s+l);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (test=str_true; *test; test++) {
|
|
|
|
int l = ::strlen(*test);
|
|
|
|
if (!::strncmp(s,*test,l) && isWordBreak(s[l],true)) {
|
|
|
|
store = true;
|
|
|
|
assign(s+l);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-04-12 12:39:04 +00:00
|
|
|
String& String::append(const char* value, int len)
|
|
|
|
{
|
|
|
|
if (len && value && *value) {
|
|
|
|
if (len < 0) {
|
|
|
|
if (!m_string) {
|
|
|
|
m_string = ::strdup(value);
|
2013-04-12 13:19:14 +00:00
|
|
|
m_length = 0;
|
2013-04-12 12:39:04 +00:00
|
|
|
if (!m_string)
|
|
|
|
Debug("String",DebugFail,"strdup() returned NULL!");
|
|
|
|
changed();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
len = ::strlen(value);
|
|
|
|
}
|
|
|
|
int olen = length();
|
|
|
|
len += olen;
|
|
|
|
char *tmp1 = m_string;
|
|
|
|
char *tmp2 = (char *) ::malloc(len+1);
|
|
|
|
if (tmp2) {
|
|
|
|
if (m_string)
|
|
|
|
::strncpy(tmp2,m_string,olen);
|
|
|
|
::strncpy(tmp2+olen,value,len-olen);
|
|
|
|
tmp2[len] = 0;
|
|
|
|
m_string = tmp2;
|
2013-04-12 13:19:14 +00:00
|
|
|
m_length = len;
|
2013-04-12 12:39:04 +00:00
|
|
|
::free(tmp1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Debug("String",DebugFail,"malloc(%d) returned NULL!",len+1);
|
|
|
|
changed();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2005-03-28 00:58:26 +00:00
|
|
|
String& String::append(const char* value, const char* separator, bool force)
|
|
|
|
{
|
|
|
|
if (value || force) {
|
|
|
|
if (!null())
|
|
|
|
operator+=(separator);
|
|
|
|
operator+=(value);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2007-03-26 18:18:22 +00:00
|
|
|
String& String::append(const ObjList* list, const char* separator, bool force)
|
|
|
|
{
|
2010-11-12 16:34:26 +00:00
|
|
|
if (!list)
|
|
|
|
return *this;
|
|
|
|
int olen = length();
|
|
|
|
int sepLen = 0;
|
|
|
|
if (!TelEngine::null(separator))
|
|
|
|
sepLen = ::strlen(separator);
|
|
|
|
int len = 0;
|
|
|
|
for (ObjList* o = list->skipNull(); o; o = o->skipNext()) {
|
|
|
|
const String& src = o->get()->toString();
|
|
|
|
if (sepLen && (len || olen) && (src.length() || force))
|
|
|
|
len += sepLen;
|
|
|
|
len += src.length();
|
2007-03-26 18:18:22 +00:00
|
|
|
}
|
2010-11-12 16:34:26 +00:00
|
|
|
if (!len)
|
|
|
|
return *this;
|
|
|
|
char* oldStr = m_string;
|
|
|
|
char* newStr = (char*)::malloc(olen + len + 1);
|
|
|
|
if (!newStr) {
|
|
|
|
Debug("String",DebugFail,"malloc(%d) returned NULL!",olen + len + 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
if (m_string)
|
|
|
|
::memcpy(newStr,m_string,olen);
|
|
|
|
for (list = list->skipNull(); list; list = list->skipNext()) {
|
|
|
|
const String& src = list->get()->toString();
|
|
|
|
if (sepLen && olen && (src.length() || force)) {
|
|
|
|
::memcpy(newStr + olen,separator,sepLen);
|
|
|
|
olen += sepLen;
|
|
|
|
}
|
|
|
|
::memcpy(newStr + olen,src.c_str(),src.length());
|
|
|
|
olen += src.length();
|
|
|
|
}
|
|
|
|
newStr[olen] = 0;
|
|
|
|
m_string = newStr;
|
2013-04-12 13:19:14 +00:00
|
|
|
m_length = olen;
|
2010-11-12 16:34:26 +00:00
|
|
|
::free(oldStr);
|
|
|
|
changed();
|
2007-03-26 18:18:22 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2006-05-24 10:04:22 +00:00
|
|
|
String& String::append(double value, unsigned int decimals)
|
|
|
|
{
|
|
|
|
if (decimals > 12)
|
|
|
|
decimals = 12;
|
|
|
|
char buf[80];
|
2010-03-26 09:41:42 +00:00
|
|
|
::sprintf(buf,"%0.*f",decimals,value);
|
2006-05-24 10:04:22 +00:00
|
|
|
return operator+=(buf);
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool String::operator==(const char* value) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (!m_string)
|
|
|
|
return !(value && *value);
|
|
|
|
return value && !::strcmp(m_string,value);
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool String::operator!=(const char* value) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (!m_string)
|
|
|
|
return value && *value;
|
|
|
|
return (!value) || ::strcmp(m_string,value);
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool String::operator&=(const char* value) const
|
2004-12-27 15:08:12 +00:00
|
|
|
{
|
|
|
|
if (!m_string)
|
|
|
|
return !(value && *value);
|
|
|
|
return value && !::strcasecmp(m_string,value);
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
bool String::operator|=(const char* value) const
|
2004-12-27 15:08:12 +00:00
|
|
|
{
|
|
|
|
if (!m_string)
|
|
|
|
return value && *value;
|
|
|
|
return (!value) || ::strcasecmp(m_string,value);
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
int String::find(char what, unsigned int offs) const
|
|
|
|
{
|
|
|
|
if (!m_string || (offs > m_length))
|
|
|
|
return -1;
|
|
|
|
const char *s = ::strchr(m_string+offs,what);
|
|
|
|
return s ? s-m_string : -1;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
int String::find(const char* what, unsigned int offs) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (!(m_string && what && *what) || (offs > m_length))
|
|
|
|
return -1;
|
|
|
|
const char *s = ::strstr(m_string+offs,what);
|
|
|
|
return s ? s-m_string : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int String::rfind(char what) const
|
|
|
|
{
|
|
|
|
if (!m_string)
|
|
|
|
return -1;
|
|
|
|
const char *s = ::strrchr(m_string,what);
|
|
|
|
return s ? s-m_string : -1;
|
|
|
|
}
|
|
|
|
|
2006-03-13 20:28:34 +00:00
|
|
|
bool String::startsWith(const char* what, bool wordBreak, bool caseInsensitive) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (!(m_string && what && *what))
|
|
|
|
return false;
|
|
|
|
unsigned int l = ::strlen(what);
|
|
|
|
if (m_length < l)
|
|
|
|
return false;
|
|
|
|
else if (wordBreak && (m_length > l) && !isWordBreak(m_string[l]))
|
|
|
|
return false;
|
2006-03-13 20:28:34 +00:00
|
|
|
|
|
|
|
if (caseInsensitive)
|
|
|
|
return (::strncasecmp(m_string,what,l) == 0);
|
2004-05-22 00:05:20 +00:00
|
|
|
return (::strncmp(m_string,what,l) == 0);
|
|
|
|
}
|
|
|
|
|
2006-03-13 20:28:34 +00:00
|
|
|
bool String::startSkip(const char* what, bool wordBreak, bool caseInsensitive)
|
2004-12-01 00:54:04 +00:00
|
|
|
{
|
2006-03-13 20:28:34 +00:00
|
|
|
if (startsWith(what,wordBreak,caseInsensitive)) {
|
2004-12-01 00:54:04 +00:00
|
|
|
const char *p = m_string + ::strlen(what);
|
|
|
|
if (wordBreak)
|
|
|
|
while (isWordBreak(*p))
|
|
|
|
p++;
|
|
|
|
assign(p);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-03-13 20:28:34 +00:00
|
|
|
bool String::endsWith(const char* what, bool wordBreak, bool caseInsensitive) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (!(m_string && what && *what))
|
|
|
|
return false;
|
|
|
|
unsigned int l = ::strlen(what);
|
|
|
|
if (m_length < l)
|
|
|
|
return false;
|
|
|
|
else if (wordBreak && (m_length > l) && !isWordBreak(m_string[m_length-l-1]))
|
|
|
|
return false;
|
2006-03-13 20:28:34 +00:00
|
|
|
if (caseInsensitive)
|
|
|
|
return (::strncasecmp(m_string+m_length-l,what,l) == 0);
|
2004-05-22 00:05:20 +00:00
|
|
|
return (::strncmp(m_string+m_length-l,what,l) == 0);
|
|
|
|
}
|
|
|
|
|
2011-03-28 16:18:45 +00:00
|
|
|
String& String::extractTo(const char* sep, String& str)
|
|
|
|
{
|
|
|
|
int pos = find(sep);
|
|
|
|
if (pos >= 0) {
|
|
|
|
str = substr(0,pos);
|
|
|
|
assign(m_string+pos+::strlen(sep));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str = *this;
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::extractTo(const char* sep, bool& store)
|
|
|
|
{
|
|
|
|
String str;
|
|
|
|
extractTo(sep,str);
|
|
|
|
store = str.toBoolean(store);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::extractTo(const char* sep, int& store, int base)
|
|
|
|
{
|
|
|
|
String str;
|
|
|
|
extractTo(sep,str);
|
|
|
|
store = str.toInteger(store,base);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::extractTo(const char* sep, int& store, const TokenDict* tokens, int base)
|
|
|
|
{
|
|
|
|
String str;
|
|
|
|
extractTo(sep,str);
|
|
|
|
store = str.toInteger(tokens,store,base);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::extractTo(const char* sep, double& store)
|
|
|
|
{
|
|
|
|
String str;
|
|
|
|
extractTo(sep,str);
|
|
|
|
store = str.toDouble(store);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-06-17 11:36:52 +00:00
|
|
|
bool String::matches(const Regexp& rexp)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (m_matches)
|
|
|
|
clearMatches();
|
|
|
|
else
|
|
|
|
m_matches = new StringMatchPrivate;
|
|
|
|
if (rexp.matches(c_str(),m_matches)) {
|
|
|
|
m_matches->fixup();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int String::matchOffset(int index) const
|
|
|
|
{
|
|
|
|
if ((!m_matches) || (index < 0) || (index > m_matches->count))
|
|
|
|
return -1;
|
|
|
|
return m_matches->rmatch[index].rm_so;
|
|
|
|
}
|
|
|
|
|
|
|
|
int String::matchLength(int index) const
|
|
|
|
{
|
|
|
|
if ((!m_matches) || (index < 0) || (index > m_matches->count))
|
|
|
|
return 0;
|
|
|
|
return m_matches->rmatch[index].rm_eo;
|
|
|
|
}
|
|
|
|
|
|
|
|
int String::matchCount() const
|
|
|
|
{
|
|
|
|
if (!m_matches)
|
|
|
|
return 0;
|
|
|
|
return m_matches->count;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String String::replaceMatches(const String& templ) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
String s;
|
|
|
|
int pos, ofs = 0;
|
|
|
|
for (;;) {
|
|
|
|
pos = templ.find('\\',ofs);
|
|
|
|
if (pos < 0) {
|
2004-12-01 00:54:04 +00:00
|
|
|
s << templ.substr(ofs);
|
2004-05-22 00:05:20 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-12-01 00:54:04 +00:00
|
|
|
s << templ.substr(ofs,pos-ofs);
|
2004-05-22 00:05:20 +00:00
|
|
|
pos++;
|
|
|
|
char c = templ[pos];
|
|
|
|
if (c == '\\') {
|
|
|
|
pos++;
|
2004-12-01 00:54:04 +00:00
|
|
|
s << "\\";
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
else if ('0' <= c && c <= '9') {
|
|
|
|
pos++;
|
2004-12-01 00:54:04 +00:00
|
|
|
s << matchString(c - '0');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pos++;
|
|
|
|
s << "\\" << c;
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
ofs = pos;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::clearMatches()
|
|
|
|
{
|
|
|
|
if (m_matches)
|
|
|
|
m_matches->clear();
|
|
|
|
}
|
|
|
|
|
2004-11-29 21:01:04 +00:00
|
|
|
ObjList* String::split(char separator, bool emptyOK) const
|
|
|
|
{
|
2012-09-19 10:59:42 +00:00
|
|
|
ObjList* list = new ObjList;
|
|
|
|
ObjList* dest = list;
|
2004-11-29 21:01:04 +00:00
|
|
|
int p = 0;
|
|
|
|
int s;
|
|
|
|
while ((s = find(separator,p)) >= 0) {
|
|
|
|
if (emptyOK || (s > p))
|
2012-09-19 10:59:42 +00:00
|
|
|
dest = dest->append(new String(m_string+p,s-p));
|
2004-11-29 21:01:04 +00:00
|
|
|
p = s + 1;
|
|
|
|
}
|
|
|
|
if (emptyOK || (m_string && m_string[p]))
|
2012-09-19 10:59:42 +00:00
|
|
|
dest->append(new String(m_string+p));
|
2004-11-29 21:01:04 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String String::msgEscape(const char* str, char extraEsc)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
String s;
|
2006-05-18 15:22:55 +00:00
|
|
|
if (TelEngine::null(str))
|
|
|
|
return s;
|
2004-05-22 00:05:20 +00:00
|
|
|
char c;
|
2013-04-12 12:39:04 +00:00
|
|
|
const char* pos = str;
|
|
|
|
char buff[3] = {'%', '%', '\0'};
|
|
|
|
while ((c=*pos++)) {
|
|
|
|
if ((unsigned char)c < ' ' || c == ':' || c == extraEsc)
|
2004-05-22 00:05:20 +00:00
|
|
|
c += '@';
|
2013-04-12 12:39:04 +00:00
|
|
|
else if (c != '%')
|
|
|
|
continue;
|
|
|
|
buff[1] = c;
|
|
|
|
s.append(str,pos - str - 1);
|
|
|
|
s += buff;
|
|
|
|
str = pos;
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
2013-04-12 12:39:04 +00:00
|
|
|
s += str;
|
2004-05-22 00:05:20 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
String String::msgUnescape(const char* str, int* errptr, char extraEsc)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2006-05-18 15:22:55 +00:00
|
|
|
String s;
|
|
|
|
if (TelEngine::null(str))
|
|
|
|
return s;
|
2004-05-22 00:05:20 +00:00
|
|
|
if (extraEsc)
|
|
|
|
extraEsc += '@';
|
|
|
|
const char *pos = str;
|
|
|
|
char c;
|
|
|
|
while ((c=*pos++)) {
|
2006-08-03 17:49:31 +00:00
|
|
|
if ((unsigned char)c < ' ') {
|
2004-05-22 00:05:20 +00:00
|
|
|
if (errptr)
|
2006-05-18 15:22:55 +00:00
|
|
|
*errptr = (pos-str) - 1;
|
2013-04-12 12:39:04 +00:00
|
|
|
s.append(str,pos - str - 1);
|
2004-05-22 00:05:20 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
else if (c == '%') {
|
|
|
|
c=*pos++;
|
2007-06-21 12:47:56 +00:00
|
|
|
if ((c > '@' && c <= '_') || c == 'z' || c == extraEsc)
|
2004-05-22 00:05:20 +00:00
|
|
|
c -= '@';
|
|
|
|
else if (c != '%') {
|
|
|
|
if (errptr)
|
2006-05-18 15:22:55 +00:00
|
|
|
*errptr = (pos-str) - 1;
|
2013-04-12 12:39:04 +00:00
|
|
|
s.append(str,pos - str - 1);
|
2004-05-22 00:05:20 +00:00
|
|
|
return s;
|
|
|
|
}
|
2013-04-12 12:39:04 +00:00
|
|
|
s.append(str,pos - str - 2);
|
|
|
|
s += c;
|
|
|
|
str = pos;
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-12 12:39:04 +00:00
|
|
|
s += str;
|
2004-05-22 00:05:20 +00:00
|
|
|
if (errptr)
|
|
|
|
*errptr = -1;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-11-16 05:53:33 +00:00
|
|
|
String String::sqlEscape(const char* str, char extraEsc)
|
|
|
|
{
|
|
|
|
String s;
|
2006-05-18 15:22:55 +00:00
|
|
|
if (TelEngine::null(str))
|
|
|
|
return s;
|
2005-11-16 05:53:33 +00:00
|
|
|
char c;
|
|
|
|
while ((c=*str++)) {
|
|
|
|
if (c == '\\' || c == '\'' || c == extraEsc)
|
|
|
|
s += '\\';
|
|
|
|
s += c;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-09-01 14:32:00 +00:00
|
|
|
String String::uriEscape(const char* str, char extraEsc, const char* noEsc)
|
2006-05-18 15:22:55 +00:00
|
|
|
{
|
|
|
|
String s;
|
|
|
|
if (TelEngine::null(str))
|
|
|
|
return s;
|
|
|
|
char c;
|
|
|
|
while ((c=*str++)) {
|
2009-09-01 14:32:00 +00:00
|
|
|
if ((unsigned char)c <= ' ' || c == '%' || c == extraEsc ||
|
|
|
|
((c == '+' || c == '?' || c == '&') && !(noEsc && ::strchr(noEsc,c))))
|
2006-05-18 15:22:55 +00:00
|
|
|
s << '%' << hexEncode(c >> 4) << hexEncode(c);
|
|
|
|
else
|
|
|
|
s += c;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
String String::uriUnescape(const char* str, int* errptr)
|
|
|
|
{
|
|
|
|
String s;
|
|
|
|
if (TelEngine::null(str))
|
|
|
|
return s;
|
|
|
|
const char *pos = str;
|
|
|
|
char c;
|
|
|
|
while ((c=*pos++)) {
|
2006-08-03 17:49:31 +00:00
|
|
|
if ((unsigned char)c < ' ') {
|
2006-05-18 15:22:55 +00:00
|
|
|
if (errptr)
|
|
|
|
*errptr = (pos-str) - 1;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
else if (c == '%') {
|
|
|
|
int hiNibble = hexDecode(*pos++);
|
|
|
|
if (hiNibble < 0) {
|
|
|
|
if (errptr)
|
|
|
|
*errptr = (pos-str) - 1;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
int loNibble = hexDecode(*pos++);
|
|
|
|
if (loNibble < 0) {
|
|
|
|
if (errptr)
|
|
|
|
*errptr = (pos-str) - 1;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
c = ((hiNibble << 4) | loNibble) & 0xff;
|
|
|
|
}
|
|
|
|
s += c;
|
|
|
|
}
|
|
|
|
if (errptr)
|
|
|
|
*errptr = -1;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
unsigned int String::hash(const char* value)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned int h = 0;
|
2010-06-11 12:29:00 +00:00
|
|
|
// sdbm hash algorithm, hash(i) = hash(i-1) * 65599 + str[i]
|
2004-05-22 00:05:20 +00:00
|
|
|
while (unsigned char c = (unsigned char) *value++)
|
2010-06-11 12:29:00 +00:00
|
|
|
h = (h << 6) + (h << 16) - h + c;
|
2004-05-22 00:05:20 +00:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2008-05-26 11:09:24 +00:00
|
|
|
int String::lenUtf8(const char* value, unsigned int maxSeq, bool overlong)
|
2008-05-23 17:21:28 +00:00
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
return 0;
|
|
|
|
if (maxSeq < 1)
|
2008-05-26 11:09:24 +00:00
|
|
|
maxSeq = 4; // RFC 3629 default limit
|
2008-05-23 17:21:28 +00:00
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
unsigned int more = 0;
|
2008-05-26 11:09:24 +00:00
|
|
|
int32_t min = 0;
|
|
|
|
int32_t val = 0;
|
|
|
|
|
2008-05-23 17:21:28 +00:00
|
|
|
while (unsigned char c = (unsigned char) *value++) {
|
|
|
|
if (more) {
|
|
|
|
// all continuation bytes are in range [128..191]
|
|
|
|
if ((c & 0xc0) != 0x80)
|
|
|
|
return -1;
|
2008-05-26 11:09:24 +00:00
|
|
|
val = (val << 6) | (c & 0x3f);
|
|
|
|
if (!--more) {
|
|
|
|
if (overlong)
|
|
|
|
continue;
|
|
|
|
// got full value, check for overlongs
|
|
|
|
if (val < min)
|
|
|
|
return -1;
|
|
|
|
}
|
2008-05-23 17:21:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
// from 1st byte we find out how many are supposed to follow
|
2008-05-26 11:09:24 +00:00
|
|
|
if (c < 128) // 1 byte, 0...0x7F, ASCII characters, no check
|
2008-05-23 17:21:28 +00:00
|
|
|
;
|
2008-05-26 11:09:24 +00:00
|
|
|
else if (c < 192) // invalid as first UFT-8 byte
|
2008-05-23 17:21:28 +00:00
|
|
|
return -1;
|
2008-05-26 11:09:24 +00:00
|
|
|
else if (c < 224) {
|
|
|
|
// 2 bytes, 0x80...0x7FF
|
|
|
|
min = 0x80;
|
|
|
|
val = c & 0x1f;
|
2008-05-23 17:21:28 +00:00
|
|
|
more = 1;
|
2008-05-26 11:09:24 +00:00
|
|
|
}
|
|
|
|
else if (c < 240) {
|
|
|
|
// 3 bytes, 0x800...0xFFFF, Basic Multilingual Plane
|
|
|
|
min = 0x800;
|
|
|
|
val = c & 0x0f;
|
2008-05-23 17:21:28 +00:00
|
|
|
more = 2;
|
2008-05-26 11:09:24 +00:00
|
|
|
}
|
|
|
|
else if (c < 248) {
|
|
|
|
// 4 bytes, 0x10000...0x1FFFFF, RFC 3629 limit (10FFFF)
|
|
|
|
min = 0x10000;
|
|
|
|
val = c & 0x07;
|
2008-05-23 17:21:28 +00:00
|
|
|
more = 3;
|
2008-05-26 11:09:24 +00:00
|
|
|
}
|
|
|
|
else if (c < 252) {
|
|
|
|
// 5 bytes, 0x200000...0x3FFFFFF
|
|
|
|
min = 0x200000;
|
|
|
|
val = c & 0x03;
|
2008-05-23 17:21:28 +00:00
|
|
|
more = 4;
|
2008-05-26 11:09:24 +00:00
|
|
|
}
|
|
|
|
else if (c < 254) {
|
|
|
|
// 6 bytes, 0x4000000...0x7FFFFFFF
|
|
|
|
min = 0x4000000;
|
|
|
|
val = c & 0x01;
|
2008-05-23 17:21:28 +00:00
|
|
|
more = 5;
|
2008-05-26 11:09:24 +00:00
|
|
|
}
|
2008-05-23 17:21:28 +00:00
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
// check if we accept a character with such sequence length
|
|
|
|
if (more >= maxSeq)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (more)
|
|
|
|
return -1;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2008-05-26 11:09:24 +00:00
|
|
|
int String::fixUtf8(const char* replace, unsigned int maxSeq, bool overlong)
|
|
|
|
{
|
|
|
|
if (null())
|
|
|
|
return 0;
|
|
|
|
if (maxSeq < 1)
|
|
|
|
maxSeq = 4; // RFC 3629 default limit
|
|
|
|
if (!replace)
|
|
|
|
replace = "\xEF\xBF\xBD";
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
unsigned int more = 0;
|
|
|
|
int32_t min = 0;
|
|
|
|
int32_t val = 0;
|
|
|
|
unsigned int pos = 0;
|
|
|
|
bool bad = false;
|
|
|
|
String tmp;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < m_length; i++) {
|
|
|
|
unsigned char c = (unsigned char) at(i);
|
|
|
|
if (more) {
|
|
|
|
// remember to reject a character with a too long sequence
|
|
|
|
if (more >= maxSeq)
|
|
|
|
bad = true;
|
|
|
|
// all continuation bytes are in range [128..191]
|
|
|
|
if ((c & 0xc0) != 0x80) {
|
|
|
|
// truncated sequence, must search for 1st byte again
|
|
|
|
more = 0;
|
|
|
|
count++;
|
|
|
|
tmp += replace;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
val = (val << 6) | (c & 0x3f);
|
|
|
|
if (!--more) {
|
|
|
|
// got full value, check for overlongs
|
|
|
|
if ((val < min) && !overlong)
|
|
|
|
bad = true;
|
|
|
|
// finished multibyte, add it to temporary
|
|
|
|
if (bad) {
|
|
|
|
count++;
|
|
|
|
tmp += replace;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tmp += substr(pos,(int)(i+1-pos));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos = i;
|
|
|
|
bad = false;
|
|
|
|
// from 1st byte we find out how many are supposed to follow
|
|
|
|
if (c < 128) // 1 byte, 0...0x7F, ASCII characters, good
|
|
|
|
;
|
|
|
|
else if (c < 192) // invalid as first UFT-8 byte
|
|
|
|
bad = true;
|
|
|
|
else if (c < 224) {
|
|
|
|
// 2 bytes, 0x80...0x7FF
|
|
|
|
min = 0x80;
|
|
|
|
val = c & 0x1f;
|
|
|
|
more = 1;
|
|
|
|
}
|
|
|
|
else if (c < 240) {
|
|
|
|
// 3 bytes, 0x800...0xFFFF, Basic Multilingual Plane
|
|
|
|
min = 0x800;
|
|
|
|
val = c & 0x0f;
|
|
|
|
more = 2;
|
|
|
|
}
|
|
|
|
else if (c < 248) {
|
|
|
|
// 4 bytes, 0x10000...0x1FFFFF, RFC 3629 limit (10FFFF)
|
|
|
|
min = 0x10000;
|
|
|
|
val = c & 0x07;
|
|
|
|
more = 3;
|
|
|
|
}
|
|
|
|
else if (c < 252) {
|
|
|
|
// 5 bytes, 0x200000...0x3FFFFFF
|
|
|
|
min = 0x200000;
|
|
|
|
val = c & 0x03;
|
|
|
|
more = 4;
|
|
|
|
}
|
|
|
|
else if (c < 254) {
|
|
|
|
// 6 bytes, 0x4000000...0x7FFFFFFF
|
|
|
|
min = 0x4000000;
|
|
|
|
val = c & 0x01;
|
|
|
|
more = 5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bad = true;
|
|
|
|
if (!more) {
|
|
|
|
if (bad) {
|
|
|
|
count++;
|
|
|
|
tmp += replace;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tmp += (char)c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (more) {
|
|
|
|
// UTF-8 truncated at end of string
|
|
|
|
count++;
|
|
|
|
tmp += replace;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count)
|
|
|
|
operator=(tmp);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2008-01-17 15:31:48 +00:00
|
|
|
void* String::getObject(const String& name) const
|
|
|
|
{
|
2013-04-12 13:19:14 +00:00
|
|
|
if (name == YATOM("String"))
|
2008-01-17 15:31:48 +00:00
|
|
|
return const_cast<String*>(this);
|
|
|
|
return GenObject::getObject(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
const String& String::toString() const
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-04-12 13:19:14 +00:00
|
|
|
const String* String::atom(const String*& str, const char* val)
|
|
|
|
{
|
|
|
|
if (!str) {
|
|
|
|
s_mutex.lock();
|
|
|
|
if (!str) {
|
|
|
|
if (TelEngine::null(val))
|
|
|
|
str = &s_empty;
|
|
|
|
else {
|
|
|
|
str = static_cast<const String*>(s_atoms[val]);
|
|
|
|
if (!str) {
|
|
|
|
str = new String(val);
|
|
|
|
s_atoms.insert(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s_mutex.unlock();
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2008-01-17 15:31:48 +00:00
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
Regexp::Regexp()
|
2005-02-06 21:06:32 +00:00
|
|
|
: m_regexp(0), m_flags(0)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"Regexp::Regexp() [%p]",this);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
Regexp::Regexp(const char* value, bool extended, bool insensitive)
|
2005-02-06 21:06:32 +00:00
|
|
|
: String(value), m_regexp(0), m_flags(0)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"Regexp::Regexp(\"%s\",%d,%d) [%p]",
|
2005-02-06 21:06:32 +00:00
|
|
|
value,extended,insensitive,this);
|
|
|
|
setFlags(extended,insensitive);
|
2010-01-31 20:05:43 +00:00
|
|
|
compile();
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
Regexp::Regexp(const Regexp& value)
|
2005-02-06 21:06:32 +00:00
|
|
|
: String(value.c_str()), m_regexp(0), m_flags(value.m_flags)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"Regexp::Regexp(%p) [%p]",&value,this);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Regexp::~Regexp()
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
2010-06-17 11:36:52 +00:00
|
|
|
bool Regexp::matches(const char* value, StringMatchPrivate* matchlist) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-12-09 21:33:10 +00:00
|
|
|
XDebug(DebugInfo,"Regexp::matches(\"%s\",%p)",value,matchlist);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (!value)
|
2005-06-04 00:06:59 +00:00
|
|
|
value = "";
|
2004-05-22 00:05:20 +00:00
|
|
|
if (!compile())
|
|
|
|
return false;
|
2005-12-09 21:33:10 +00:00
|
|
|
int mm = matchlist ? MAX_MATCH : 0;
|
|
|
|
regmatch_t *mt = matchlist ? (matchlist->rmatch)+1 : 0;
|
2004-05-22 00:05:20 +00:00
|
|
|
return !::regexec((regex_t *)m_regexp,value,mm,mt,0);
|
|
|
|
}
|
|
|
|
|
2005-12-09 21:33:10 +00:00
|
|
|
bool Regexp::matches(const char* value) const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2010-06-17 11:36:52 +00:00
|
|
|
return matches(value,0);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Regexp::changed()
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
String::changed();
|
|
|
|
}
|
|
|
|
|
2010-06-17 11:36:52 +00:00
|
|
|
bool Regexp::compile() const
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2005-07-09 18:27:49 +00:00
|
|
|
XDebug(DebugInfo,"Regexp::compile()");
|
2004-05-22 00:05:20 +00:00
|
|
|
if (c_str() && !m_regexp) {
|
|
|
|
regex_t *data = (regex_t *) ::malloc(sizeof(regex_t));
|
2005-01-17 23:38:12 +00:00
|
|
|
if (!data) {
|
2005-12-30 02:45:23 +00:00
|
|
|
Debug("Regexp",DebugFail,"malloc(%d) returned NULL!",(int)sizeof(regex_t));
|
2005-01-17 23:38:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-02-06 21:06:32 +00:00
|
|
|
if (::regcomp(data,c_str(),m_flags)) {
|
2004-05-22 00:05:20 +00:00
|
|
|
Debug(DebugWarn,"Regexp::compile() \"%s\" failed",c_str());
|
|
|
|
::regfree(data);
|
|
|
|
::free(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_regexp = (void *)data;
|
|
|
|
}
|
|
|
|
return (m_regexp != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Regexp::cleanup()
|
|
|
|
{
|
2005-07-09 18:27:49 +00:00
|
|
|
XDebug(DebugInfo,"Regexp::cleanup()");
|
2004-05-22 00:05:20 +00:00
|
|
|
if (m_regexp) {
|
|
|
|
regex_t *data = (regex_t *)m_regexp;
|
|
|
|
m_regexp = 0;
|
|
|
|
::regfree(data);
|
|
|
|
::free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-06 21:06:32 +00:00
|
|
|
void Regexp::setFlags(bool extended, bool insensitive)
|
|
|
|
{
|
|
|
|
int f = (extended ? REG_EXTENDED : 0) | (insensitive ? REG_ICASE : 0);
|
|
|
|
if (m_flags != f) {
|
|
|
|
cleanup();
|
|
|
|
m_flags = f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Regexp::isExtended() const
|
|
|
|
{
|
|
|
|
return (m_flags & REG_EXTENDED) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Regexp::isCaseInsensitive() const
|
|
|
|
{
|
|
|
|
return (m_flags & REG_ICASE) != 0;
|
|
|
|
}
|
|
|
|
|
2008-01-17 15:31:48 +00:00
|
|
|
|
2005-03-20 03:11:53 +00:00
|
|
|
NamedString::NamedString(const char* name, const char* value)
|
2004-05-22 00:05:20 +00:00
|
|
|
: String(value), m_name(name)
|
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
XDebug(DebugAll,"NamedString::NamedString(\"%s\",\"%s\") [%p]",name,value,this);
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
2004-12-04 15:44:54 +00:00
|
|
|
|
2008-01-17 15:31:48 +00:00
|
|
|
const String& NamedString::toString() const
|
2005-04-14 03:14:20 +00:00
|
|
|
{
|
2008-01-17 15:31:48 +00:00
|
|
|
return m_name;
|
2005-04-14 03:14:20 +00:00
|
|
|
}
|
|
|
|
|
2008-01-17 15:31:48 +00:00
|
|
|
void* NamedString::getObject(const String& name) const
|
2004-12-04 15:44:54 +00:00
|
|
|
{
|
2013-04-12 13:19:14 +00:00
|
|
|
if (name == YATOM("NamedString"))
|
2008-01-17 15:31:48 +00:00
|
|
|
return (void*)this;
|
|
|
|
return String::getObject(name);
|
2004-12-04 15:44:54 +00:00
|
|
|
}
|
|
|
|
|
2008-01-17 15:31:48 +00:00
|
|
|
|
|
|
|
NamedPointer::NamedPointer(const char* name, GenObject* data, const char* value)
|
|
|
|
: NamedString(name,value),
|
|
|
|
m_data(0)
|
2005-04-14 03:14:20 +00:00
|
|
|
{
|
2008-01-17 15:31:48 +00:00
|
|
|
userData(data);
|
2005-04-14 03:14:20 +00:00
|
|
|
}
|
|
|
|
|
2008-01-17 15:31:48 +00:00
|
|
|
NamedPointer::~NamedPointer()
|
2004-12-04 15:44:54 +00:00
|
|
|
{
|
2008-01-17 15:31:48 +00:00
|
|
|
userData(0);
|
2004-12-04 15:44:54 +00:00
|
|
|
}
|
|
|
|
|
2008-01-17 15:31:48 +00:00
|
|
|
// Set obscure data carried by this object.
|
|
|
|
void NamedPointer::userData(GenObject* data)
|
2004-12-04 15:44:54 +00:00
|
|
|
{
|
2008-01-17 15:31:48 +00:00
|
|
|
TelEngine::destruct(m_data);
|
|
|
|
m_data = data;
|
|
|
|
}
|
|
|
|
|
2010-06-11 11:25:10 +00:00
|
|
|
// Retrieve the pointer carried by this object and release ownership
|
2008-01-17 15:31:48 +00:00
|
|
|
GenObject* NamedPointer::takeData()
|
|
|
|
{
|
|
|
|
GenObject* tmp = m_data;
|
|
|
|
m_data = 0;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* NamedPointer::getObject(const String& name) const
|
|
|
|
{
|
2013-04-12 13:19:14 +00:00
|
|
|
if (name == YATOM("NamedPointer"))
|
2008-01-17 15:31:48 +00:00
|
|
|
return (void*)this;
|
|
|
|
void* p = NamedString::getObject(name);
|
|
|
|
if (p)
|
|
|
|
return p;
|
|
|
|
if (m_data)
|
|
|
|
return m_data->getObject(name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called whenever the string value changed. Release the pointer
|
|
|
|
void NamedPointer::changed()
|
|
|
|
{
|
|
|
|
userData(0);
|
2008-07-02 14:49:21 +00:00
|
|
|
NamedString::changed();
|
2008-01-17 15:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* GenObject::getObject(const String& name) const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const String& GenObject::toString() const
|
|
|
|
{
|
|
|
|
return String::empty();
|
2004-12-04 15:44:54 +00:00
|
|
|
}
|
2005-03-28 00:58:26 +00:00
|
|
|
|
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|