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

[or-cvs] Basic string-join functionality



Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv23212/src/common

Modified Files:
	util.c util.h 
Log Message:
Basic string-join functionality

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.145
retrieving revision 1.146
diff -u -d -r1.145 -r1.146
--- util.c	14 Oct 2004 02:48:57 -0000	1.145
+++ util.c	14 Oct 2004 19:51:46 -0000	1.146
@@ -609,6 +609,32 @@
   return n;
 }
 
+char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate)
+{
+  int i;
+  size_t n = 0, jlen;
+  char *r = NULL, *dst, *src;
+  
+  tor_assert(sl && join);
+  jlen = strlen(join);
+  for (i = 0; i < sl->num_used; ++i) {
+    n += strlen(sl->list[i]);
+    n += jlen;
+  }
+  if (!terminate) n -= jlen;
+  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;
+    }
+  }
+  *dst = '\0';
+  return r;
+}
+
 /* Splay-tree implementation of string-to-void* map
  */
 struct strmap_entry_t {

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.97
retrieving revision 1.98
diff -u -d -r1.97 -r1.98
--- util.h	14 Oct 2004 02:49:48 -0000	1.97
+++ util.h	14 Oct 2004 19:51:47 -0000	1.98
@@ -145,6 +145,7 @@
 #define SPLIT_IGNORE_BLANK 0x02
 int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
                            int flags, int max);
+char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate);
 
 #define SMARTLIST_FOREACH(sl, type, var, cmd)                   \
   do {                                                          \