[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r9132: Add a rudimentary line-wrapping function for use in dumping (in tor/trunk: . src/common src/or)



Author: nickm
Date: 2006-12-15 16:26:23 -0500 (Fri, 15 Dec 2006)
New Revision: 9132

Modified:
   tor/trunk/
   tor/trunk/src/common/util.c
   tor/trunk/src/common/util.h
   tor/trunk/src/or/test.c
Log:
 r11597@Kushana:  nickm | 2006-12-15 15:49:27 -0500
 Add a rudimentary line-wrapping function for use in dumping comments in config files.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r11597] on c95137ef-5f19-0410-b913-86e773d04f59

Modified: tor/trunk/src/common/util.c
===================================================================
--- tor/trunk/src/common/util.c	2006-12-15 21:26:15 UTC (rev 9131)
+++ tor/trunk/src/common/util.c	2006-12-15 21:26:23 UTC (rev 9132)
@@ -747,6 +747,79 @@
   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
  * ===== */

Modified: tor/trunk/src/common/util.h
===================================================================
--- tor/trunk/src/common/util.h	2006-12-15 21:26:15 UTC (rev 9131)
+++ tor/trunk/src/common/util.h	2006-12-15 21:26:23 UTC (rev 9132)
@@ -138,6 +138,9 @@
 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 @@
   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,

Modified: tor/trunk/src/or/test.c
===================================================================
--- tor/trunk/src/or/test.c	2006-12-15 21:26:15 UTC (rev 9131)
+++ tor/trunk/src/or/test.c	2006-12-15 21:26:23 UTC (rev 9132)
@@ -778,6 +778,30 @@
   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