aboutsummaryrefslogtreecommitdiffstats
path: root/lib/misc.c
diff options
context:
space:
mode:
authorWilmer van der Gaast <wilmer@gaast.net>2007-10-12 01:06:50 +0100
committerWilmer van der Gaast <wilmer@gaast.net>2007-10-12 01:06:50 +0100
commitd444c09e6c7ac6fc3c1686af0e63c09805d8cd00 (patch)
treeb567c3ee431a72ed68713f023c977781f10e23cb /lib/misc.c
parent285b55d3b38d6e1c8b3fc4a64945f2bb9ace07f4 (diff)
Added word_wrap() function to misc.c and using it at the right places so
that long messages in groupchats also get wrapped properly (instead of truncated).
Diffstat (limited to 'lib/misc.c')
-rw-r--r--lib/misc.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/misc.c b/lib/misc.c
index 9061af39..5e385d4a 100644
--- a/lib/misc.c
+++ b/lib/misc.c
@@ -544,3 +544,51 @@ struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain )
return reply;
}
+
+/* Word wrapping. Yes, I know this isn't UTF-8 clean. I'm willing to take the risk. */
+char *word_wrap( char *msg, int line_len )
+{
+ GString *ret = g_string_sized_new( strlen( msg ) + 16 );
+
+ while( strlen( msg ) > line_len )
+ {
+ int i;
+
+ /* First try to find out if there's a newline already. Don't
+ want to add more splits than necessary. */
+ for( i = line_len; i > 0 && msg[i] != '\n'; i -- );
+ if( msg[i] == '\n' )
+ {
+ g_string_append_len( ret, msg, i + 1 );
+ msg += i + 1;
+ continue;
+ }
+
+ for( i = line_len; i > 0; i -- )
+ {
+ if( msg[i] == '-' )
+ {
+ g_string_append_len( ret, msg, i + 1 );
+ g_string_append_c( ret, '\n' );
+ msg += i + 1;
+ break;
+ }
+ else if( msg[i] == ' ' )
+ {
+ g_string_append_len( ret, msg, i );
+ g_string_append_c( ret, '\n' );
+ msg += i + 1;
+ break;
+ }
+ }
+ if( i == 0 )
+ {
+ g_string_append_len( ret, msg, line_len );
+ g_string_append_c( ret, '\n' );
+ msg += line_len;
+ }
+ }
+ g_string_append( ret, msg );
+
+ return g_string_free( ret, FALSE );
+}