aboutsummaryrefslogtreecommitdiff
path: root/src/common/container.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2004-11-03 18:28:00 +0000
committerNick Mathewson <nickm@torproject.org>2004-11-03 18:28:00 +0000
commit11de62aa6059d15837431c13ab38e32a84394c0f (patch)
treec8778600987440a006a1a481dd47276a653f285b /src/common/container.c
parent11979dc1f5fae74df8fba8c339f0ac5ee4c52796 (diff)
downloadtor-11de62aa6059d15837431c13ab38e32a84394c0f.tar
tor-11de62aa6059d15837431c13ab38e32a84394c0f.tar.gz
Add a slightly trickier string-join interface for making NUL-terminated string messages
svn:r2659
Diffstat (limited to 'src/common/container.c')
-rw-r--r--src/common/container.c33
1 files changed, 25 insertions, 8 deletions
diff --git a/src/common/container.c b/src/common/container.c
index c7602d14b..d1cafd80b 100644
--- a/src/common/container.c
+++ b/src/common/container.c
@@ -301,32 +301,49 @@ int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
/** Allocate and return a new string containing the concatenation of
* the elements of <b>sl</b>, in order, separated by <b>join</b>. If
* <b>terminate</b> is true, also terminate the string with <b>join</b>.
- * Requires that every element of <b>sl</b> is NUL-terminated string.
+ * If <b>len_out</b> is not NULL, set <b>len_out</b> to the length of
+ * the returned string. Requires that every element of <b>sl</b> is
+ * NUL-terminated string.
*/
-char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate)
+char *smartlist_join_strings(smartlist_t *sl, const char *join,
+ int terminate, size_t *len_out)
+{
+ return smartlist_join_strings2(sl,join,strlen(join),terminate,len_out);
+}
+
+/** As smartlist_join_strings2, but instead of separating/terminated with a
+ * NUL-terminated string <b>join</b>, uses the <b>join_len</b>-byte sequence
+ * at <b>join</b>. (Useful for generating a sequenct of NUL-terminated
+ * strings.)
+ */
+char *smartlist_join_strings2(smartlist_t *sl, const char *join,
+ size_t join_len, int terminate, size_t *len_out)
{
int i;
- size_t n = 0, jlen;
+ size_t n = 0;
char *r = NULL, *dst, *src;
tor_assert(sl);
tor_assert(join);
- jlen = strlen(join);
+ join_len = strlen(join);
for (i = 0; i < sl->num_used; ++i) {
n += strlen(sl->list[i]);
- n += jlen;
+ n += join_len;
}
- if (!terminate) n -= jlen;
+ if (!terminate) n -= join_len;
dst = r = tor_malloc(n+1);
for (i = 0; i < sl->num_used; ) {
for (src = sl->list[i]; *src; )
*dst++ = *src++;
if (++i < sl->num_used || terminate) {
- memcpy(dst, join, jlen);
- dst += jlen;
+ memcpy(dst, join, join_len);
+ dst += join_len;
}
}
*dst = '\0';
+
+ if (len_out)
+ *len_out = dst-r;
return r;
}