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