# HG changeset patch # User Matti Hamalainen # Date 1423500060 -7200 # Node ID a765a51cbd5cfec1a82a0bb74ea41196e103e81b # Parent a69764b4305f7b4bbeafd786e0fa59438564eeb8 Make th_print_wrap() function public and move the code to th_util. diff -r a69764b4305f -r a765a51cbd5c th_args.c --- a/th_args.c Mon Feb 09 04:18:18 2015 +0200 +++ b/th_args.c Mon Feb 09 18:41:00 2015 +0200 @@ -158,60 +158,6 @@ /* Print help for commandline arguments/options */ -static void th_pad(FILE *outFile, int count) -{ - while (count--) - fputc(' ', outFile); -} - - -static void th_print_wrap(FILE *fh, const char *str, int spad, int rpad, int width) -{ - size_t pos = 0; - BOOL first = TRUE; - - while (str[pos]) - { - // Pre-pad line - int linelen = first ? spad : rpad; - th_pad(fh, first ? 0 : rpad); - first = FALSE; - - // Skip whitespace at line start - while (th_isspace(str[pos]) || str[pos] == '\n') pos++; - - // Handle each word - while (str[pos] && str[pos] != '\n') - { - size_t next; - int wlen; - - // Find word length and next break - for (wlen = 0, next = pos; str[next] && !th_isspace(str[next]) && str[next] != '\n'; next++, wlen++); - - // Check if we have too much of text? - if (linelen + wlen >= width) - break; - - // Print what we have - for (;pos < next; pos++, linelen++) - fputc(str[pos], fh); - - // Check if we are at end of input or hard linefeed - if (str[next] == '\n' || str[next] == 0) - break; - else - { - fputc(str[pos], fh); - pos++; - linelen++; - } - } - fprintf(fh, "\n"); - } -} - - void th_args_help(FILE *fh, const th_optarg_t *opts, const int numOpts, const int flags) diff -r a69764b4305f -r a765a51cbd5c th_util.c --- a/th_util.c Mon Feb 09 04:18:18 2015 +0200 +++ b/th_util.c Mon Feb 09 18:41:00 2015 +0200 @@ -142,6 +142,60 @@ } +static void th_pad(FILE *outFile, int count) +{ + while (count--) + fputc(' ', outFile); +} + + +void th_print_wrap(FILE *fh, const char *str, int spad, int rpad, int width) +{ + size_t pos = 0; + BOOL first = TRUE; + + while (str[pos]) + { + // Pre-pad line + int linelen = first ? spad : rpad; + th_pad(fh, first ? 0 : rpad); + first = FALSE; + + // Skip whitespace at line start + while (th_isspace(str[pos]) || str[pos] == '\n') pos++; + + // Handle each word + while (str[pos] && str[pos] != '\n') + { + size_t next; + int wlen; + + // Find word length and next break + for (wlen = 0, next = pos; str[next] && !th_isspace(str[next]) && str[next] != '\n'; next++, wlen++); + + // Check if we have too much of text? + if (linelen + wlen >= width) + break; + + // Print what we have + for (;pos < next; pos++, linelen++) + fputc(str[pos], fh); + + // Check if we are at end of input or hard linefeed + if (str[next] == '\n' || str[next] == 0) + break; + else + { + fputc(str[pos], fh); + pos++; + linelen++; + } + } + fprintf(fh, "\n"); + } +} + + /* Error handling */ int th_get_error() diff -r a69764b4305f -r a765a51cbd5c th_util.h --- a/th_util.h Mon Feb 09 04:18:18 2015 +0200 +++ b/th_util.h Mon Feb 09 18:41:00 2015 +0200 @@ -123,6 +123,8 @@ int th_term_width(); int th_term_height(); +void th_print_wrap(FILE *fh, const char *str, int spad, int rpad, int width); + int th_get_error(); int th_errno_to_error(int error); const char *th_error_str(int error);