diff options
author | Wilmer van der Gaast <wilmer@gaast.net> | 2007-10-12 01:06:50 +0100 |
---|---|---|
committer | Wilmer van der Gaast <wilmer@gaast.net> | 2007-10-12 01:06:50 +0100 |
commit | d444c09e6c7ac6fc3c1686af0e63c09805d8cd00 (patch) | |
tree | b567c3ee431a72ed68713f023c977781f10e23cb /lib/misc.c | |
parent | 285b55d3b38d6e1c8b3fc4a64945f2bb9ace07f4 (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.c | 48 |
1 files changed, 48 insertions, 0 deletions
@@ -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 ); +} |