diff options
author | Nick Mathewson <nickm@torproject.org> | 2006-12-15 21:26:23 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2006-12-15 21:26:23 +0000 |
commit | 078aab810b7410086685d648b4d3328b140ddea5 (patch) | |
tree | d2f60647cad33c30c2c1adc1b97558a9bbde4c6a /src/common | |
parent | ab2fc7bd62f2a91dfe003f2a99b7cf223762fec8 (diff) | |
download | tor-078aab810b7410086685d648b4d3328b140ddea5.tar.gz tor-078aab810b7410086685d648b4d3328b140ddea5.zip |
r11597@Kushana: nickm | 2006-12-15 15:49:27 -0500
Add a rudimentary line-wrapping function for use in dumping comments in config files.
svn:r9132
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/util.c | 73 | ||||
-rw-r--r-- | src/common/util.h | 4 |
2 files changed, 76 insertions, 1 deletions
diff --git a/src/common/util.c b/src/common/util.c index 6cd74e9980..7e7b3827ec 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -747,6 +747,79 @@ escaped(const char *s) return _escaped_val; } +/** Rudimentary string wrapping code: given a un-wrapped <b>string</b> (no + * newlines!), break the string into newline-terminated lines of no more than + * <b>width</b> characters long (not counting newline) and insert them into + * <b>out</b> in order. Precede the first line with prefix0, and subsequent + * lines with prefixRest. + */ +/* This uses a stupid greedy wrapping algorithm right now: + * - For each line: + * - Try to fit as much stuff as possible, but break on a space. + * - If the first "word" of the line will extend beyond the allowable + * width, break the word at the end of the width. + */ +void +wrap_string(smartlist_t *out, const char *string, size_t width, + const char *prefix0, const char *prefixRest) +{ + size_t p0Len, pRestLen, pCurLen; + const char *eos, *prefixCur; + tor_assert(out); + tor_assert(string); + tor_assert(width); + if (!prefix0) + prefix0 = ""; + if (!prefixRest) + prefixRest = ""; + + p0Len = strlen(prefix0); + pRestLen = strlen(prefixRest); + tor_assert(width > p0Len && width > pRestLen); + eos = strchr(string, '\0'); + tor_assert(eos); + pCurLen = p0Len; + prefixCur = prefix0; + + while ((eos-string)+pCurLen > width) { + const char *eol = string + width - pCurLen; + while (eol > string && *eol != ' ') + --eol; + /* eol is now the last space that can fit, or the start of the string. */ + if (eol > string) { + size_t line_len = (eol-string) + pCurLen + 2; + char *line = tor_malloc(line_len); + memcpy(line, prefixCur, pCurLen); + memcpy(line+pCurLen, string, eol-string); + line[line_len-2] = '\n'; + line[line_len-1] = '\0'; + smartlist_add(out, line); + string = eol + 1; + } else { + size_t line_len = width + 2; + char *line = tor_malloc(line_len); + memcpy(line, prefixCur, pCurLen); + memcpy(line+pCurLen, string, width - pCurLen); + line[line_len-2] = '\n'; + line[line_len-1] = '\0'; + smartlist_add(out, line); + string += width-pCurLen; + } + prefixCur = prefixRest; + pCurLen = pRestLen; + } + + if (string < eos) { + size_t line_len = (eos-string) + pCurLen + 2; + char *line = tor_malloc(line_len); + memcpy(line, prefixCur, pCurLen); + memcpy(line+pCurLen, string, eos-string); + line[line_len-2] = '\n'; + line[line_len-1] = '\0'; + smartlist_add(out, line); + } +} + /* ===== * Time * ===== */ diff --git a/src/common/util.h b/src/common/util.h index 13d51b4413..54a2552da5 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -138,6 +138,9 @@ int tor_mem_is_zero(const char *mem, size_t len) ATTR_PURE; int tor_digest_is_zero(const char *digest) ATTR_PURE; char *esc_for_log(const char *string) ATTR_MALLOC; const char *escaped(const char *string); +struct smartlist_t; +void wrap_string(struct smartlist_t *out, const char *string, size_t width, + const char *prefix0, const char *prefixRest); void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen); int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen); @@ -174,7 +177,6 @@ typedef struct sized_chunk_t { const char *bytes; size_t len; } sized_chunk_t; -struct smartlist_t; int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks, int bin); int append_bytes_to_file(const char *fname, const char *str, size_t len, |