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

[or-cvs] Move string-splitting into a separate function



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

Modified Files:
	util.c util.h 
Log Message:
Move string-splitting into a separate function

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.125
retrieving revision 1.126
diff -u -d -r1.125 -r1.126
--- util.c	17 Aug 2004 06:28:34 -0000	1.125
+++ util.c	17 Aug 2004 21:06:36 -0000	1.126
@@ -481,6 +481,46 @@
   }
 }
 
+/**
+ * Split a string <b>str</b> along all occurences of <b>sep</b>, adding the
+ * split strings, in order, to <b>sl</b>.  If <b>skipSpace</b> is true,
+ * remove initial and trailing space from each entry.
+ */
+int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
+                           int skipSpace)
+{
+  const char *cp, *end, *next;
+  int n = 0;
+
+  tor_assert(sl && str && sep);
+
+  cp = str;
+  while (1) {
+    if (skipSpace) {
+      while (isspace(*cp)) ++cp;
+    }
+    end = strstr(cp,sep);
+    if (!end) {
+      end = strchr(cp,'\0');
+      next = NULL;
+    } else {
+      next = end+strlen(sep);
+    }
+
+    if (skipSpace) {
+      while (end > cp && isspace(*(end-1)))
+        --end;
+    }
+    smartlist_add(sl, tor_strndup(cp, end-cp));
+    ++n;
+    if (!next)
+      break;
+    cp = next;
+  }
+
+  return n;
+}
+
 /* 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.82
retrieving revision 1.83
diff -u -d -r1.82 -r1.83
--- util.h	17 Aug 2004 06:28:34 -0000	1.82
+++ util.h	17 Aug 2004 21:06:36 -0000	1.83
@@ -158,6 +158,9 @@
 void *smartlist_del_keeporder(smartlist_t *sl, int idx);
 void smartlist_insert(smartlist_t *sl, int idx, void *val);
 int smartlist_len(const smartlist_t *sl);
+int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
+  int skipSpace);
+
 #define SMARTLIST_FOREACH(sl, type, var, cmd)                   \
   do {                                                          \
     int sl_idx, sl_len=smartlist_len(sl);                       \