提交 9b041ca2 authored 作者: Brian West's avatar Brian West

add patch from FSCORE-89 thank you

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@7251 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 1121c272
...@@ -34,6 +34,7 @@ that much better: ...@@ -34,6 +34,7 @@ that much better:
Matt Klein <mklein@nmedia.net> Matt Klein <mklein@nmedia.net>
Jonas Gauffin <jonas at gauffin dot org> - Bugfixes and additions in mod_spidermonkey_odbc Jonas Gauffin <jonas at gauffin dot org> - Bugfixes and additions in mod_spidermonkey_odbc
Damjan Jovanovic <moctodliamgtavojtodnajmad backwards> - mod_java Damjan Jovanovic <moctodliamgtavojtodnajmad backwards> - mod_java
Juan Jose Comellas <juanjo@comellas.org> - Patch to switch_utils for arg parsing.
A big THANK YOU goes to: A big THANK YOU goes to:
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
* Contributor(s): * Contributor(s):
* *
* Anthony Minessale II <anthmct@yahoo.com> * Anthony Minessale II <anthmct@yahoo.com>
* Juan Jose Comellas <juanjo@comellas.org>
* *
* *
* switch_utils.c -- Compatability and Helper Code * switch_utils.c -- Compatability and Helper Code
...@@ -1004,62 +1005,182 @@ SWITCH_DECLARE(char *) switch_escape_char(switch_memory_pool_t *pool, char *in, ...@@ -1004,62 +1005,182 @@ SWITCH_DECLARE(char *) switch_escape_char(switch_memory_pool_t *pool, char *in,
return data; return data;
} }
/* Helper function used when separating strings to unescape a character. The
supported characters are:
SWITCH_DECLARE(unsigned int) switch_separate_string(char *buf, char delim, char **array, int arraylen) \n linefeed
\r carriage return
\t tab
\s space
Any other character is returned as it was received. */
static char unescape_char(char escaped)
{ {
int argc; char unescaped;
char *ptr;
int quot = 0;
char qc = '\'';
int x;
if (!buf || !array || !arraylen) { switch (escaped) {
return 0; case 'n':
unescaped = '\n';
break;
case 'r':
unescaped = '\r';
break;
case 't':
unescaped = '\t';
break;
case 's':
unescaped = ' ';
break;
default:
unescaped = escaped;
} }
return unescaped;
}
memset(array, 0, arraylen * sizeof(*array)); /* Helper function used when separating strings to remove quotes, leading /
trailing spaces, and to convert escaped characters. */
ptr = buf; static char *cleanup_separated_string(char *str)
{
for (argc = 0; *ptr && (argc < arraylen - 1); argc++) { char *ptr;
array[argc] = ptr; char *dest;
for (; *ptr; ptr++) { char *start;
if (*ptr == qc) { char *end = NULL;
if (quot) { int inside_quotes = 0;
quot--;
/* Skip initial whitespace */
for (ptr = str; *ptr == ' '; ++ptr) {
}
for (start = dest = ptr; *ptr; ++ptr) {
if (*ptr == '\\') {
++ptr;
*dest++ = unescape_char(*ptr);
end = dest;
} else if (*ptr == '\'') {
inside_quotes = (1 - inside_quotes);
} else { } else {
quot++; *dest++ = *ptr;
if (*ptr != ' ' || inside_quotes) {
end = dest;
}
} }
} else if ((*ptr == delim) && !quot) { }
*ptr++ = '\0'; if (end) {
*end = '\0';
}
return start;
}
/* Separate a string using a delimiter that is not a space */
static unsigned int separate_string_char_delim(char *buf, char delim, char **array, int arraylen)
{
enum tokenizer_state {
START,
FIND_DELIM
} state = START;
unsigned int count = 0;
char *ptr = buf;
int inside_quotes = 0;
int i;
while (*ptr && count < arraylen) {
switch (state) {
case START:
array[count++] = ptr;
state = FIND_DELIM;
break;
case FIND_DELIM:
/* escaped characters are copied verbatim to the destination string */
if (*ptr == '\\') {
++ptr;
} else if (*ptr == '\'') {
inside_quotes = (1 - inside_quotes);
} else if (*ptr == delim && !inside_quotes) {
*ptr = '\0';
state = START;
}
++ptr;
break; break;
} }
} }
/* strip quotes, escaped chars and leading / trailing spaces */
for (i = 0; i < count; ++i) {
array[i] = cleanup_separated_string(array[i]);
} }
return count;
}
/* Separate a string using a delimiter that is a space */
static unsigned int separate_string_blank_delim(char *buf, char **array, int arraylen)
{
enum tokenizer_state {
START,
SKIP_INITIAL_SPACE,
FIND_DELIM,
SKIP_ENDING_SPACE
} state = START;
unsigned int count = 0;
char *ptr = buf;
int inside_quotes = 0;
int i;
while (*ptr && count < arraylen) {
switch (state) {
case START:
array[count++] = ptr;
state = SKIP_INITIAL_SPACE;
break;
if (*ptr) { case SKIP_INITIAL_SPACE:
array[argc++] = ptr; if (*ptr == ' ') {
++ptr;
} else {
state = FIND_DELIM;
} }
break;
/* strip quotes and leading / trailing spaces */ case FIND_DELIM:
for (x = 0; x < argc; x++) { if (*ptr == '\\') {
char *p; ++ptr;
} else if (*ptr == '\'') {
inside_quotes = (1 - inside_quotes);
} else if (*ptr == ' ' && !inside_quotes) {
*ptr = '\0';
state = SKIP_ENDING_SPACE;
}
++ptr;
break;
while(*(array[x]) == ' ') { case SKIP_ENDING_SPACE:
(array[x])++; if (*ptr == ' ') {
++ptr;
} else {
state = START;
} }
p = array[x]; break;
while((p = strchr(array[x], qc))) {
memmove(p, p+1, strlen(p));
p++;
} }
p = array[x] + (strlen(array[x]) - 1);
while(*p == ' ') {
*p-- = '\0';
} }
/* strip quotes, escaped chars and leading / trailing spaces */
for (i = 0; i < count; ++i) {
array[i] = cleanup_separated_string(array[i]);
}
return count;
}
SWITCH_DECLARE(unsigned int) switch_separate_string(char *buf, char delim, char **array, int arraylen)
{
if (!buf || !array || !arraylen) {
return 0;
} }
return argc; memset(array, 0, arraylen * sizeof (*array));
return (delim == ' ' ?
separate_string_blank_delim(buf, array, arraylen) :
separate_string_char_delim(buf, delim, array, arraylen));
} }
SWITCH_DECLARE(const char *) switch_cut_path(const char *in) SWITCH_DECLARE(const char *) switch_cut_path(const char *in)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论