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