commit ab9cadc4a4ea4875fec8a2d4651440780a7922d5
parent 6c24a52c96905f126c45a30805eb2a277cdc91b6
Author: Ricardo Mones <ricardo@mones.org>
Date: Tue, 5 Nov 2013 00:55:10 +0100
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.
Diffstat:
2 files changed, 43 insertions(+), 0 deletions(-)
diff --git 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
@@ -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,