# HG changeset patch # User Mikael Berthe # Date 1354121703 -3600 # Node ID 998feaa54ef0e10f438f8c55777243ed69668e86 # Parent 0ba755e5dc48ce5d628f3432a59c50ccda16f832 Add ut_unescape_tabs_cr() diff -r 0ba755e5dc48 -r 998feaa54ef0 mcabber/mcabber/api.h --- a/mcabber/mcabber/api.h Wed Nov 28 17:50:25 2012 +0100 +++ b/mcabber/mcabber/api.h Wed Nov 28 17:55:03 2012 +0100 @@ -4,7 +4,7 @@ #include #include // For MCABBER_BRANCH -#define MCABBER_API_VERSION 24 +#define MCABBER_API_VERSION 25 #define MCABBER_API_MIN 24 #define MCABBER_BRANCH_DEV 1 diff -r 0ba755e5dc48 -r 998feaa54ef0 mcabber/mcabber/utils.c --- a/mcabber/mcabber/utils.c Wed Nov 28 17:50:25 2012 +0100 +++ b/mcabber/mcabber/utils.c Wed Nov 28 17:55:03 2012 +0100 @@ -706,6 +706,53 @@ return xtext; } +// ut_unescape_tabs_cr(text) +// Expand CR or TAB character sequences (\n, \t) in string text. +// If there is no CR/TAB in text, then the original pointer is returned +// (be careful _not_ to free the pointer in this case). +// If there are some unescaped sequences, a new string with those chars +// replaced with real newline/tab characters is allocated; in this case +// this is up to the caller to free this string after use. +char *ut_unescape_tabs_cr(const char *text) +{ + char *xtext, *linestart; + char *p, *q; + + if (!text) + return NULL; + + p = g_strstr_len(text, -1, "\\n"); + if (!p) { + p = g_strstr_len(text, -1, "\\t"); + if (!p) + return (char*)text; + } + + xtext = g_new(char, strlen(text) + 1); + p = (char*)text; + q = linestart = xtext; + do { + if (*p == '\\') { + if (*(p+1) == '\\' && (*(p+2) == 'n' || *(p+2) == 't')) { + // This is an escaped CR sequence + *q++ = '\\'; + *q++ = 'n'; + p += 2; + continue; + } + if (*(p+1) == 'n' || *(p+1) == 't') { + // This is a CR sequence + p++; + *q++ = (*p == 'n' ? '\n' : '\t'); + continue; + } + } + *q++ = *p; + } while (*p++); + + return xtext; +} + /* Cygwin's newlib does not have strcasestr() */ /* The author of the code before the endif is diff -r 0ba755e5dc48 -r 998feaa54ef0 mcabber/mcabber/utils.h --- a/mcabber/mcabber/utils.h Wed Nov 28 17:50:25 2012 +0100 +++ b/mcabber/mcabber/utils.h Wed Nov 28 17:55:03 2012 +0100 @@ -45,6 +45,7 @@ void replace_nl_with_dots(char *bufstr); char *ut_expand_tabs(const char *text); +char *ut_unescape_tabs_cr(const char *text); #if !defined (HAVE_STRCASESTR) char *strcasestr(const char *haystack, const char *needle);