Mercurial > hg > th-libs
diff th_string.c @ 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 | 51eec969b07a |
children | 7638fa9d191f |
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"); + } +} + +