chiark / gitweb /
util: add split_spaces() call
authorLennart Poettering <lennart@poettering.net>
Thu, 19 Nov 2009 01:50:21 +0000 (02:50 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 19 Nov 2009 01:50:21 +0000 (02:50 +0100)
util.c
util.h

diff --git a/util.c b/util.c
index bcb608480f7d54cc0dc6c80f0a36b392d0e21340..4bec220f7a1dbfad1c1868d8228d973bb77863df 100644 (file)
--- a/util.c
+++ b/util.c
@@ -145,3 +145,22 @@ int safe_atoi(const char *s, int *ret_i) {
         *ret_i = (unsigned) l;
         return 0;
 }
+
+/* What is interpreted as whitespace? */
+#define WHITESPACE " \t\n"
+
+/* Split a string into words. */
+char *split_spaces(const char *c, size_t *l, char **state) {
+        char *current;
+
+        current = *state ? *state : (char*) c;
+
+        if (!*current || *c == 0)
+                return NULL;
+
+        current += strspn(current, WHITESPACE);
+        *l = strcspn(current, WHITESPACE);
+        *state = current+*l;
+
+        return (char*) current;
+}
diff --git a/util.h b/util.h
index 60dc04e961cadb778a1617041bf48d559091c5d8..9dfe631b8a5aa3b88c033f4fb394c52ddf781737 100644 (file)
--- a/util.h
+++ b/util.h
@@ -59,4 +59,9 @@ int parse_boolean(const char *v);
 int safe_atou(const char *s, unsigned *ret_u);
 int safe_atoi(const char *s, int *ret_i);
 
+char *split_spaces(const char *c, size_t *l, char **state);
+
+#define FOREACH_WORD(word, length, s, state)    \
+        for ((state) = NULL, (word) = split_spaces((s), &(l), &(state)); (word); (word) = split_spaces((s), &(l), &(state)))
+
 #endif