diff options
| author | Wilmer van der Gaast <wilmer@gaast.net> | 2007-10-12 01:08:58 +0100 | 
|---|---|---|
| committer | Wilmer van der Gaast <wilmer@gaast.net> | 2007-10-12 01:08:58 +0100 | 
| commit | eda54e40d04c83028d84e91c895a550c1929b436 (patch) | |
| tree | 878f985af2ab5d2b9c59e8c955448bc4e9ddec17 /lib/misc.c | |
| parent | 82135c7178b6379f35741991f6c06bb308143194 (diff) | |
| parent | d444c09e6c7ac6fc3c1686af0e63c09805d8cd00 (diff) | |
Merge from devel.
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 ); +} | 
