Better handling of quoted strings, added parameter to force quoting.

Added convenience method to retrieve an unquoted string.


git-svn-id: http://voip.null.ro/svn/yate@2361 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
paulc 2008-11-24 13:42:06 +00:00
parent 2e6f00e6db
commit d9cdd4b910
2 changed files with 60 additions and 10 deletions

View File

@ -148,30 +148,69 @@ void MimeHeaderLine::delParam(const char* name)
}
// Utility function, puts quotes around a string
void MimeHeaderLine::addQuotes(String& str)
void MimeHeaderLine::addQuotes(String& str, bool force)
{
str.trimBlanks();
int l = str.length();
if ((l < 2) || (str[0] != '"') || (str[l-1] != '"'))
unsigned int l = str.length();
if (force || (l < 2) || (str[0] != '"') || (str[l-1] != '"')) {
str = "\"" + str + "\"";
force = true;
}
for (l = 1; l < str.length() - 1; l++) {
switch (str.at(l)) {
case '\\':
if (!force) {
// check only, don't quote again
switch (str.at(l+1)) {
case '\\':
case '"':
// already quoted, skip it
l++;
continue;
}
}
// fall through
case '"':
str = str.substr(0,l) + "\\" + str.substr(l);
l++;
default:
break;
}
}
}
// Utility function, removes quotes around a string
void MimeHeaderLine::delQuotes(String& str)
void MimeHeaderLine::delQuotes(String& str, bool force)
{
str.trimBlanks();
int l = str.length();
unsigned int l = str.length();
if ((l >= 2) && (str[0] == '"') && (str[l-1] == '"')) {
str = str.substr(1,l-2);
str.trimBlanks();
force = true;
}
if (force) {
for (l = 0; l < str.length(); l++) {
if (str.at(l) == '\\')
str = str.substr(0,l) + str.substr(l+1);
// since the string is shorter the loop will skip past next char
}
}
}
// Utility function, puts quotes around a string
String MimeHeaderLine::quote(const String& str)
String MimeHeaderLine::quote(const String& str, bool force)
{
String tmp(str);
addQuotes(tmp);
addQuotes(tmp,force);
return tmp;
}
// Utility function, removed quotes around a string
String MimeHeaderLine::unquote(const String& str, bool force)
{
String tmp(str);
delQuotes(tmp,force);
return tmp;
}

View File

@ -131,21 +131,32 @@ public:
/**
* Utility function, puts quotes around a string.
* @param str String to put quotes around.
* @param force True to force quoting even if was already quoted
*/
static void addQuotes(String& str);
static void addQuotes(String& str, bool force = false);
/**
* Utility function, removes quotes around a string.
* @param str String to remove quotes.
* @param force True to force unquoting even if wasn't properly quoted
*/
static void delQuotes(String& str);
static void delQuotes(String& str, bool force = false);
/**
* Utility function, puts quotes around a string.
* @param str String to put quotes around.
* @param force True to force quoting even if was already quoted
* @return The input string enclosed in quotes.
*/
static String quote(const String& str);
static String quote(const String& str, bool force = false);
/**
* Utility function, removes quotes around a string.
* @param str String to remove quotes around.
* @param force True to force unquoting even if wasn't properly quoted
* @return The input string with enclosing quotes removed.
*/
static String unquote(const String& str, bool force = false);
/**
* Utility function to find a separator not in "quotes" or inside \<uri\>.