# HG changeset patch # User Matti Hamalainen # Date 1578832029 -7200 # Node ID 390c66af09cf71f2b927594786264bdf1c55f040 # Parent d60e1d751b5b1484f0073ace78d1ead54f845b27 Move th_print_wrap() and th_print_pad() to th_args from th_string. diff -r d60e1d751b5b -r 390c66af09cf th_args.c --- a/th_args.c Sun Jan 12 13:00:58 2020 +0200 +++ b/th_args.c Sun Jan 12 14:27:09 2020 +0200 @@ -177,6 +177,65 @@ } +void th_print_pad(FILE *fh, int count, const char och) +{ + while (count--) + fputc(och, fh); +} + + +void th_print_wrap(FILE *fh, const char *str, + const int spad, int const rpad, const int width) +{ + size_t pos = 0; + BOOL first = TRUE; + + while (str[pos]) + { + // Pre-pad line + int linelen = first ? spad : rpad; + th_print_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"); + } +} + + /** * Print help for commandline arguments/options * @param fh stdio file handle to output to diff -r d60e1d751b5b -r 390c66af09cf th_args.h --- a/th_args.h Sun Jan 12 13:00:58 2020 +0200 +++ b/th_args.h Sun Jan 12 14:27:09 2020 +0200 @@ -43,13 +43,16 @@ } th_optarg; -BOOL th_args_process(int argc, char *argv[], - const th_optarg *opts, const int nopts, - BOOL (*handle_option)(int id, char *, char *), - BOOL (*handle_other)(char *), const int flags); +BOOL th_args_process(int argc, char *argv[], + const th_optarg *opts, const int nopts, + BOOL (*handle_option)(int id, char *, char *), + BOOL (*handle_other)(char *), const int flags); -void th_args_help(FILE *fh, const th_optarg *opts, - const int nopts, const int flags); +void th_args_help(FILE *fh, const th_optarg *opts, + const int nopts, const int flags); + +void th_print_wrap(FILE *fh, const char *str, + const int spad, const int rpad, const int width); #ifdef __cplusplus } diff -r d60e1d751b5b -r 390c66af09cf th_string.c --- a/th_string.c Sun Jan 12 13:00:58 2020 +0200 +++ b/th_string.c Sun Jan 12 14:27:09 2020 +0200 @@ -683,57 +683,3 @@ } return TRUE; } - - -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"); - } -} diff -r d60e1d751b5b -r 390c66af09cf th_string.h --- a/th_string.h Sun Jan 12 13:00:58 2020 +0200 +++ b/th_string.h Sun Jan 12 14:27:09 2020 +0200 @@ -183,8 +183,6 @@ BOOL th_get_boolean(const char *str, BOOL *value); BOOL th_get_int(const char *str, unsigned int *value, BOOL *neg); -void th_print_wrap(FILE *fh, const char *str, int spad, int rpad, int width); - #ifdef __cplusplus }