Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/misc.c

    r36e9f62 rd444c09  
    545545        return reply;
    546546}
     547
     548/* Word wrapping. Yes, I know this isn't UTF-8 clean. I'm willing to take the risk. */
     549char *word_wrap( char *msg, int line_len )
     550{
     551        GString *ret = g_string_sized_new( strlen( msg ) + 16 );
     552       
     553        while( strlen( msg ) > line_len )
     554        {
     555                int i;
     556               
     557                /* First try to find out if there's a newline already. Don't
     558                   want to add more splits than necessary. */
     559                for( i = line_len; i > 0 && msg[i] != '\n'; i -- );
     560                if( msg[i] == '\n' )
     561                {
     562                        g_string_append_len( ret, msg, i + 1 );
     563                        msg += i + 1;
     564                        continue;
     565                }
     566               
     567                for( i = line_len; i > 0; i -- )
     568                {
     569                        if( msg[i] == '-' )
     570                        {
     571                                g_string_append_len( ret, msg, i + 1 );
     572                                g_string_append_c( ret, '\n' );
     573                                msg += i + 1;
     574                                break;
     575                        }
     576                        else if( msg[i] == ' ' )
     577                        {
     578                                g_string_append_len( ret, msg, i );
     579                                g_string_append_c( ret, '\n' );
     580                                msg += i + 1;
     581                                break;
     582                        }
     583                }
     584                if( i == 0 )
     585                {
     586                        g_string_append_len( ret, msg, line_len );
     587                        g_string_append_c( ret, '\n' );
     588                        msg += line_len;
     589                }
     590        }
     591        g_string_append( ret, msg );
     592       
     593        return g_string_free( ret, FALSE );
     594}
Note: See TracChangeset for help on using the changeset viewer.