From: Ricardo Mones Date: Mon, 4 Nov 2013 23:55:10 +0000 (+0100) Subject: New utils function escape_internal_quotes X-Git-Tag: 3.9.3~30 X-Git-Url: http://git.claws-mail.org/?p=claws.git;a=commitdiff_plain;h=ab9cadc4a4ea4875fec8a2d4651440780a7922d5 New utils function escape_internal_quotes Searchs for all quotation characters within a string, creating (only if required) a copy with the quotation characters escaped. --- diff --git a/src/common/utils.c b/src/common/utils.c index 53a9ddb47..fb4ba75f5 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -810,6 +810,47 @@ void extract_quote(gchar *str, gchar quote_chr) } } +/* Returns a newly allocated string with all quote_chr not at the beginning + or the end of str escaped with '\' or the given str if not required. */ +gchar *escape_internal_quotes(gchar *str, gchar quote_chr) +{ + register gchar *p, *q; + gchar *qstr; + int k = 0, l = 0; + + if (str == NULL || *str == '\0') + return str; + + /* search for unescaped quote_chr */ + p = str; + if (*p == quote_chr) + ++p, ++l; + while (*p) { + if (*p == quote_chr && *(p - 1) != '\\' && *(p + 1) != '\0') + ++k; + ++p, ++l; + } + if (!k) /* nothing to escape */ + return str; + + /* unescaped quote_chr found */ + qstr = g_malloc(l + k + 1); + p = str; + q = qstr; + if (*p == quote_chr) { + *q = quote_chr; + ++p, ++q; + } + while (*p) { + if (*p == quote_chr && *(p - 1) != '\\' && *(p + 1) != '\0') + *q++ = '\\'; + *q++ = *p++; + } + *q = '\0'; + + return qstr; +} + void eliminate_address_comment(gchar *str) { register gchar *srcp, *destp; diff --git a/src/common/utils.h b/src/common/utils.h index b613499dc..8766ff28e 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -338,6 +338,8 @@ void extract_parenthesis (gchar *str, void extract_quote (gchar *str, gchar quote_chr); +gchar *escape_internal_quotes (gchar *str, + gchar quote_chr); void eliminate_address_comment (gchar *str); gchar *strchr_with_skip_quote (const gchar *str, gint quote_chr,