mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-24 04:13:28 +01:00
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
This commit is contained in:
parent
ab2fc7bd62
commit
078aab810b
@ -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
|
||||
* ===== */
|
||||
|
@ -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,
|
||||
|
@ -778,6 +778,30 @@ test_util(void)
|
||||
test_streq("\"abcd\"", escaped("abcd"));
|
||||
test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
|
||||
test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
|
||||
|
||||
/* Test wrap_string */
|
||||
{
|
||||
smartlist_t *sl = smartlist_create();
|
||||
wrap_string(sl, "This is a test of string wrapping functionality: woot.",
|
||||
10, "", "");
|
||||
cp = smartlist_join_strings(sl, "", 0, NULL);
|
||||
test_streq(cp,
|
||||
"This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n");
|
||||
tor_free(cp);
|
||||
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
||||
smartlist_clear(sl);
|
||||
|
||||
wrap_string(sl, "This is a test of string wrapping functionality: woot.",
|
||||
16, "### ", "# ");
|
||||
cp = smartlist_join_strings(sl, "", 0, NULL);
|
||||
test_streq(cp,
|
||||
"### This is a\n# test of string\n# wrapping\n# functionality:\n"
|
||||
"# woot.\n");
|
||||
|
||||
tor_free(cp);
|
||||
SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
|
||||
smartlist_clear(sl);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
|
Loading…
Reference in New Issue
Block a user