summaryrefslogtreecommitdiff
path: root/main/utils.c
diff options
context:
space:
mode:
authorGeorge Joseph <george.joseph@fairview5.com>2014-09-18 19:23:39 +0000
committerGeorge Joseph <george.joseph@fairview5.com>2014-09-18 19:23:39 +0000
commitad8ef9175ad92f90d5ad1c8429fc71d791dae42f (patch)
treed2b7961aca136b4db7de7bde117a0a86743efc07 /main/utils.c
parentde72f3edbc6385a5ff438484a6b31d5548de1caf (diff)
utils: Create ast_strsep function that ignores separators inside quotes
This function acts like strsep with three exceptions... * The separator is a single character instead of a string. * Separators inside quotes are treated literally instead of like separators. * You can elect to have leading and trailing whitespace and quotes stripped from the result and have '\' sequences unescaped. Like strsep, ast_strsep maintains no internal state and you can call it recursively using different separators on the same storage. Also like strsep, for consistent results, consecutive separators are not collapsed so you may get an empty string as a valid result. Tested by: George Joseph Review: https://reviewboard.asterisk.org/r/3989/ ........ Merged revisions 423476 from http://svn.asterisk.org/svn/asterisk/branches/12 ........ Merged revisions 423478 from http://svn.asterisk.org/svn/asterisk/branches/13 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@423480 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main/utils.c')
-rw-r--r--main/utils.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/main/utils.c b/main/utils.c
index 229080b83..40818c37a 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -1473,6 +1473,66 @@ char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes)
return s;
}
+char *ast_strsep(char **iss, const char sep, uint32_t flags)
+{
+ char *st = *iss;
+ char *is;
+ int inquote = 0;
+ int found = 0;
+ char stack[8];
+
+ if (iss == NULL || *iss == '\0') {
+ return NULL;
+ }
+
+ memset(stack, 0, sizeof(stack));
+
+ for(is = st; *is; is++) {
+ if (*is == '\\') {
+ if (*++is != '\0') {
+ is++;
+ } else {
+ break;
+ }
+ }
+
+ if (*is == '\'' || *is == '"') {
+ if (*is == stack[inquote]) {
+ stack[inquote--] = '\0';
+ } else {
+ if (++inquote >= sizeof(stack)) {
+ return NULL;
+ }
+ stack[inquote] = *is;
+ }
+ }
+
+ if (*is == sep && !inquote) {
+ *is = '\0';
+ found = 1;
+ *iss = is + 1;
+ break;
+ }
+ }
+ if (!found) {
+ *iss = NULL;
+ }
+
+ if (flags & AST_STRSEP_STRIP) {
+ st = ast_strip_quoted(st, "'\"", "'\"");
+ }
+
+ if (flags & AST_STRSEP_TRIM) {
+ st = ast_strip(st);
+ }
+
+ if (flags & AST_STRSEP_UNESCAPE) {
+ ast_unescape_quoted(st);
+ }
+
+ return st;
+}
+
char *ast_unescape_semicolon(char *s)
{
char *e;