Mercurial > hg > th-libs
changeset 584:390c66af09cf
Move th_print_wrap() and th_print_pad() to th_args from th_string.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 12 Jan 2020 14:27:09 +0200 |
parents | d60e1d751b5b |
children | 1a4359ecc5d5 |
files | th_args.c th_args.h th_string.c th_string.h |
diffstat | 4 files changed, 68 insertions(+), 62 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- 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 }
--- 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"); - } -}
--- 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 }