Mercurial > hg > th-libs
changeset 162:578d9298cc1e
Actually, move th_print_wrap() to th_string module.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 09 Feb 2015 18:52:56 +0200 |
parents | a765a51cbd5c |
children | 44887a053df0 |
files | th_string.c th_string.h th_util.c th_util.h |
diffstat | 4 files changed, 58 insertions(+), 56 deletions(-) [+] |
line wrap: on
line diff
--- a/th_string.c Mon Feb 09 18:41:00 2015 +0200 +++ b/th_string.c Mon Feb 09 18:52:56 2015 +0200 @@ -572,3 +572,59 @@ return (len == 6) ? val : -1; } + + +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 Mon Feb 09 18:41:00 2015 +0200 +++ b/th_string.h Mon Feb 09 18:52:56 2015 +0200 @@ -79,6 +79,8 @@ int th_get_hex_triplet(const char *); +void th_print_wrap(FILE *fh, const char *str, int spad, int rpad, int width); + #ifdef __cplusplus }
--- a/th_util.c Mon Feb 09 18:41:00 2015 +0200 +++ b/th_util.c Mon Feb 09 18:52:56 2015 +0200 @@ -142,60 +142,6 @@ } -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()
--- a/th_util.h Mon Feb 09 18:41:00 2015 +0200 +++ b/th_util.h Mon Feb 09 18:52:56 2015 +0200 @@ -123,8 +123,6 @@ 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);