Blame SOURCES/0145-lib-parse-list-delimited-by-any-character.patch

28bab8
From 72a59b68e2d7cbe14ed6f8e0b2837c0a98e2899a Mon Sep 17 00:00:00 2001
28bab8
From: Jakub Filak <jfilak@redhat.com>
28bab8
Date: Thu, 2 Jul 2015 14:59:58 +0200
28bab8
Subject: [PATCH] lib: parse list delimited by any character
28bab8
28bab8
Related: #1224984
28bab8
28bab8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
28bab8
---
28bab8
 src/include/internal_libreport.h |  2 ++
28bab8
 src/lib/glib_support.c           | 34 +++++++++++++++++++++++++++------
28bab8
 tests/glib_helpers.at            | 41 ++++++++++++++++++++++++++++++++++++++++
28bab8
 3 files changed, 71 insertions(+), 6 deletions(-)
28bab8
28bab8
diff --git a/src/include/internal_libreport.h b/src/include/internal_libreport.h
28bab8
index d35d715..b36cbd9 100644
28bab8
--- a/src/include/internal_libreport.h
28bab8
+++ b/src/include/internal_libreport.h
28bab8
@@ -779,6 +779,8 @@ file_obj_t *new_file_obj(const char* fullpath, const char* filename);
28bab8
 void free_file_obj(file_obj_t *f);
28bab8
 #define load_workflow_config_data libreport_load_workflow_config_data
28bab8
 GHashTable *load_workflow_config_data(const char* path);
28bab8
+#define parse_delimited_list libreport_parse_delimited_list
28bab8
+GList *parse_delimited_list(char* list, const char *delim);
28bab8
 #define parse_list libreport_parse_list
28bab8
 GList *parse_list(const char* list);
28bab8
 
28bab8
diff --git a/src/lib/glib_support.c b/src/lib/glib_support.c
28bab8
index 6276e9d..02c2dfd 100644
28bab8
--- a/src/lib/glib_support.c
28bab8
+++ b/src/lib/glib_support.c
28bab8
@@ -53,12 +53,15 @@ void glib_init(void)
28bab8
 }
28bab8
 
28bab8
 /*
28bab8
- * Parser comma separated list of strings to Glist
28bab8
+ * Parser a list of strings to Glist
28bab8
  *
28bab8
- * @param list comma separated list of strings
28bab8
+ * The function modifies the passed list.
28bab8
+ *
28bab8
+ * @param list a separated list of strings
28bab8
+ * @param delim a set of bytes that delimit the tokens in the parsed string
28bab8
  * @returns GList or null if the list is empty
28bab8
  */
28bab8
-GList *parse_list(const char* list)
28bab8
+GList *parse_delimited_list(char* list, const char *delim)
28bab8
 {
28bab8
     if (list == NULL)
28bab8
         return NULL;
28bab8
@@ -66,18 +69,37 @@ GList *parse_list(const char* list)
28bab8
     GList *l = NULL;
28bab8
 
28bab8
     char *saved_ptr = NULL;
28bab8
-    char *tmp_list = xstrdup(list);
28bab8
-    char *item = strtok_r(tmp_list, LIST_DELIMITER, &saved_ptr);
28bab8
+    char *item = strtok_r(list, delim, &saved_ptr);
28bab8
     while (item)
28bab8
     {
28bab8
         l = g_list_append(l, strtrim(xstrdup(item)));
28bab8
-        item = strtok_r(NULL, LIST_DELIMITER, &saved_ptr);
28bab8
+        item = strtok_r(NULL, delim, &saved_ptr);
28bab8
     }
28bab8
 
28bab8
+    return l;
28bab8
+}
28bab8
+
28bab8
+/*
28bab8
+ * Parser comma separated list of strings to Glist
28bab8
+ *
28bab8
+ * @param list comma separated list of strings
28bab8
+ * @returns GList or null if the list is empty
28bab8
+ */
28bab8
+GList *parse_list(const char* list)
28bab8
+{
28bab8
+    if (list == NULL)
28bab8
+        return NULL;
28bab8
+
28bab8
+    char *tmp_list = xstrdup(list);
28bab8
+
28bab8
+    GList *l = parse_delimited_list(list, LIST_DELIMITER);
28bab8
+
28bab8
     free(tmp_list);
28bab8
+
28bab8
     return l;
28bab8
 }
28bab8
 
28bab8
+
28bab8
 void list_free_with_free(GList *list)
28bab8
 {
28bab8
     GList *li;
28bab8
diff --git a/tests/glib_helpers.at b/tests/glib_helpers.at
28bab8
index e118819..0dc7f7e 100644
28bab8
--- a/tests/glib_helpers.at
28bab8
+++ b/tests/glib_helpers.at
28bab8
@@ -2,6 +2,47 @@
28bab8
 
28bab8
 AT_BANNER([glib helpers])
28bab8
 
28bab8
+## ------------ ##
28bab8
+## parse a list ##
28bab8
+## ------------ ##
28bab8
+
28bab8
+AT_TESTFUN([parse_delimited_list],
28bab8
+[[
28bab8
+#include "internal_libreport.h"
28bab8
+#include <assert.h>
28bab8
+
28bab8
+int test(const char *list, const char *delimiter, const char *strings[])
28bab8
+{
28bab8
+    char *tmp_list = xstrdup(list);
28bab8
+    GList *l = parse_delimited_list(tmp_list, delimiter);
28bab8
+    free(tmp_list);
28bab8
+
28bab8
+    char **tmp = (char **)strings;
28bab8
+    int retval = 0;
28bab8
+
28bab8
+    while(l != NULL) {
28bab8
+        log("is: '%s'", (char *)l->data);
28bab8
+        log("should be: '%s'", *tmp);
28bab8
+        retval |= strcmp((char *)l->data, *(tmp++)) != 0;
28bab8
+        if (retval)
28bab8
+            break; // no need to continue further
28bab8
+        l = g_list_next(l);
28bab8
+    }
28bab8
+
28bab8
+    return retval;
28bab8
+}
28bab8
+
28bab8
+int main(void)
28bab8
+{
28bab8
+    const char *new_line_list = "hello \n world \n fedora \n redhat";
28bab8
+    const char *colon_list = "hello:world:fedora:redhat";
28bab8
+    const char *strings[] = {"hello", "world", "fedora", "redhat"};
28bab8
+
28bab8
+    assert(test(new_line_list, "\n", strings) == 0);
28bab8
+    assert(test(colon_list, ":", strings) == 0);
28bab8
+}
28bab8
+]])
28bab8
+
28bab8
 ## -------------------------- ##
28bab8
 ## parse comma separated list ##
28bab8
 ## -------------------------- ##
28bab8
-- 
28bab8
2.4.3
28bab8