From 54d563e10f381ea01cf0ed93f2b97fcac848808c Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sun, 25 Dec 2016 01:47:50 +0100 Subject: config-parser: bc_config_get_list should return array instead of list --- src/common/config-parser.c | 15 +++++++++++---- src/common/config-parser.h | 2 +- tests/common/check_config_parser.c | 33 ++++++++++++++++++--------------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/common/config-parser.c b/src/common/config-parser.c index 19ca192..8d6c10e 100644 --- a/src/common/config-parser.c +++ b/src/common/config-parser.c @@ -240,7 +240,7 @@ bc_config_list_sections(bc_config_t *config) char **rv = bc_malloc(sizeof(char*) * (bc_slist_length(l) + 1)); - unsigned int i = 0; + size_t i = 0; for (bc_slist_t *tmp = l; tmp != NULL; tmp = tmp->next, i++) rv[i] = tmp->data; rv[i] = NULL; @@ -269,7 +269,7 @@ bc_config_list_keys(bc_config_t *config, const char *section) char **rv = bc_malloc(sizeof(char*) * (bc_slist_length(l) + 1)); - unsigned int i = 0; + size_t i = 0; for (bc_slist_t *tmp = l; tmp != NULL; tmp = tmp->next, i++) rv[i] = tmp->data; rv[i] = NULL; @@ -308,7 +308,7 @@ bc_config_get_with_default(bc_config_t *config, const char *section, const char } -bc_slist_t* +char** bc_config_get_list(bc_config_t *config, const char *section) { if (config == NULL) @@ -321,7 +321,14 @@ bc_config_get_list(bc_config_t *config, const char *section) if (s->type != CONFIG_SECTION_TYPE_LIST) return NULL; - return s->data; + char **rv = bc_malloc(sizeof(char*) * (bc_slist_length(s->data) + 1)); + + size_t i = 0; + for (bc_slist_t *tmp = s->data; tmp != NULL; tmp = tmp->next, i++) + rv[i] = bc_strdup(tmp->data); + rv[i] = NULL; + + return rv; } diff --git a/src/common/config-parser.h b/src/common/config-parser.h index 0bf1706..01d66dc 100644 --- a/src/common/config-parser.h +++ b/src/common/config-parser.h @@ -25,7 +25,7 @@ const char* bc_config_get(bc_config_t *config, const char *section, const char *key); const char* bc_config_get_with_default(bc_config_t *config, const char *section, const char *key, const char *default_); -bc_slist_t* bc_config_get_list(bc_config_t *config, const char *section); +char** bc_config_get_list(bc_config_t *config, const char *section); void bc_config_free(bc_config_t *config); #endif /* _CONFIG_PARSER_H */ diff --git a/tests/common/check_config_parser.c b/tests/common/check_config_parser.c index b542c88..a04dc6d 100644 --- a/tests/common/check_config_parser.c +++ b/tests/common/check_config_parser.c @@ -388,11 +388,12 @@ test_config_section_list(void **state) assert_string_equal(bc_config_get(c, "foo", "asd"), "zxc"); assert_string_equal(bc_config_get(c, "foo", "qwe"), "rty"); assert_string_equal(bc_config_get(c, "foo", "zxc"), "vbn"); - bc_slist_t *foo = bc_config_get_list(c, "bar"); - assert_non_null(foo); - assert_string_equal(foo->data, "lol = hehe"); - assert_string_equal(foo->next->data, "asdasdadssad"); - assert_null(foo->next->next); + char **bar = bc_config_get_list(c, "bar"); + assert_non_null(bar); + assert_string_equal(bar[0], "lol = hehe"); + assert_string_equal(bar[1], "asdasdadssad"); + assert_null(bar[2]); + bc_strv_free(bar); char **k = bc_config_list_keys(c, "foo"); assert_non_null(k); assert_int_equal(bc_strv_length(k), 3); @@ -430,11 +431,12 @@ test_config_section_list(void **state) assert_string_equal(bc_config_get(c, "foo", "asd"), "zxc"); assert_string_equal(bc_config_get(c, "foo", "qwe"), "rty"); assert_string_equal(bc_config_get(c, "foo", "zxc"), "vbn"); - foo = bc_config_get_list(c, "bar"); - assert_non_null(foo); - assert_string_equal(foo->data, "lol = hehe"); - assert_string_equal(foo->next->data, "asdasdadssad"); - assert_null(foo->next->next); + bar = bc_config_get_list(c, "bar"); + assert_non_null(bar); + assert_string_equal(bar[0], "lol = hehe"); + assert_string_equal(bar[1], "asdasdadssad"); + assert_null(bar[2]); + bc_strv_free(bar); k = bc_config_list_keys(c, "foo"); assert_non_null(k); assert_int_equal(bc_strv_length(k), 3); @@ -472,11 +474,12 @@ test_config_section_list(void **state) assert_string_equal(bc_config_get(c, "foo", "asd"), "zxc"); assert_string_equal(bc_config_get(c, "foo", "qwe"), "rty"); assert_string_equal(bc_config_get(c, "foo", "zxc"), "vbn"); - foo = bc_config_get_list(c, "bar"); - assert_non_null(foo); - assert_string_equal(foo->data, "lol = hehe"); - assert_string_equal(foo->next->data, "asdasdadssad"); - assert_null(foo->next->next); + bar = bc_config_get_list(c, "bar"); + assert_non_null(bar); + assert_string_equal(bar[0], "lol = hehe"); + assert_string_equal(bar[1], "asdasdadssad"); + assert_null(bar[2]); + bc_strv_free(bar); k = bc_config_list_keys(c, "foo"); assert_non_null(k); assert_int_equal(bc_strv_length(k), 3); -- cgit v1.2.3-18-g5258