This function parses a space separated string into a string list.

Signed-off-by: Stefan Beller <sbel...@google.com>
---
 string-list.c | 12 ++++++++++++
 string-list.h |  1 +
 2 files changed, 13 insertions(+)

diff --git a/string-list.c b/string-list.c
index 2a32a3f..f71384f 100644
--- a/string-list.c
+++ b/string-list.c
@@ -7,6 +7,18 @@ void string_list_init(struct string_list *list, int 
strdup_strings)
        list->strdup_strings = strdup_strings;
 }
 
+void from_space_separated_string(struct string_list *list, char *line)
+{
+       char *save_ptr;
+       const char delimiters[] = " \t\n";
+       const char *token = strtok_r(line, delimiters, &save_ptr);
+
+       while (token) {
+               string_list_append(list, xstrdup(token));
+               token = strtok_r(NULL, delimiters, &save_ptr);
+       }
+}
+
 /* if there is no exact match, point to the index where the entry could be
  * inserted */
 static int get_entry_index(const struct string_list *list, const char *string,
diff --git a/string-list.h b/string-list.h
index d3809a1..88c18e9 100644
--- a/string-list.h
+++ b/string-list.h
@@ -19,6 +19,7 @@ struct string_list {
 #define STRING_LIST_INIT_DUP   { NULL, 0, 0, 1, NULL }
 
 void string_list_init(struct string_list *list, int strdup_strings);
+void from_space_separated_string(struct string_list *list, char *line);
 
 void print_string_list(const struct string_list *p, const char *text);
 void string_list_clear(struct string_list *list, int free_util);
-- 
2.4.1.345.gab207b6.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to