comparison mcabber/mcabber/utils.c @ 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 1210a22726d3
comparison
equal deleted inserted replaced
2049:0ba755e5dc48 2050:998feaa54ef0
704 } while (*p++); 704 } while (*p++);
705 705
706 return xtext; 706 return xtext;
707 } 707 }
708 708
709 // ut_unescape_tabs_cr(text)
710 // Expand CR or TAB character sequences (\n, \t) in string text.
711 // If there is no CR/TAB in text, then the original pointer is returned
712 // (be careful _not_ to free the pointer in this case).
713 // If there are some unescaped sequences, a new string with those chars
714 // replaced with real newline/tab characters is allocated; in this case
715 // this is up to the caller to free this string after use.
716 char *ut_unescape_tabs_cr(const char *text)
717 {
718 char *xtext, *linestart;
719 char *p, *q;
720
721 if (!text)
722 return NULL;
723
724 p = g_strstr_len(text, -1, "\\n");
725 if (!p) {
726 p = g_strstr_len(text, -1, "\\t");
727 if (!p)
728 return (char*)text;
729 }
730
731 xtext = g_new(char, strlen(text) + 1);
732 p = (char*)text;
733 q = linestart = xtext;
734 do {
735 if (*p == '\\') {
736 if (*(p+1) == '\\' && (*(p+2) == 'n' || *(p+2) == 't')) {
737 // This is an escaped CR sequence
738 *q++ = '\\';
739 *q++ = 'n';
740 p += 2;
741 continue;
742 }
743 if (*(p+1) == 'n' || *(p+1) == 't') {
744 // This is a CR sequence
745 p++;
746 *q++ = (*p == 'n' ? '\n' : '\t');
747 continue;
748 }
749 }
750 *q++ = *p;
751 } while (*p++);
752
753 return xtext;
754 }
755
709 756
710 /* Cygwin's newlib does not have strcasestr() */ 757 /* Cygwin's newlib does not have strcasestr() */
711 /* The author of the code before the endif is 758 /* The author of the code before the endif is
712 * Jeffrey Stedfast <fejj@ximian.com> 759 * Jeffrey Stedfast <fejj@ximian.com>
713 * and this code is reusable in compliance with the GPL v2. -- somian */ 760 * and this code is reusable in compliance with the GPL v2. -- somian */