changeset 2050:998feaa54ef0

Add ut_unescape_tabs_cr()
author Mikael Berthe <mikael@lilotux.net>
date Wed, 28 Nov 2012 17:55:03 +0100
parents 0ba755e5dc48
children 71d84213f850
files mcabber/mcabber/api.h mcabber/mcabber/utils.c mcabber/mcabber/utils.h
diffstat 3 files changed, 49 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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 <glib.h>
 #include <mcabber/config.h> // For MCABBER_BRANCH
 
-#define MCABBER_API_VERSION 24
+#define MCABBER_API_VERSION 25
 #define MCABBER_API_MIN     24
 
 #define MCABBER_BRANCH_DEV  1
--- 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
--- 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);