From: Michael Tremer Date: Fri, 22 Nov 2024 11:17:31 +0000 (+0000) Subject: config: Add helper function to parse strings X-Git-Tag: 0.9.30~790 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2ccf6992a4817877c0bab0d515f771fedf411520;p=pakfire.git config: Add helper function to parse strings That helps us to avoid all sorts of weird type casting in the test cases. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/config.c b/src/libpakfire/config.c index 012044be8..0a5e43a17 100644 --- a/src/libpakfire/config.c +++ b/src/libpakfire/config.c @@ -517,6 +517,32 @@ ERROR: return r; } +int pakfire_config_parse(struct pakfire_config* config, const char* s, ssize_t length) { + FILE* f = NULL; + int r; + + // Check inputs + if (!s) + return -EINVAL; + + // Automatically determine the length + if (length < 0) + length = strlen(s); + + // Open a memory stream + f = fmemopen((char*)s, length, "r"); + if (!f) + return -errno; + + // Read the buffer + r = pakfire_config_read(config, f); + + // Close the buffer + fclose(f); + + return r; +} + static int pakfire_config_dump_multiline_entry(struct pakfire_config_entry* entry, FILE* f) { char* value = NULL; char* p = NULL; diff --git a/src/libpakfire/include/pakfire/config.h b/src/libpakfire/include/pakfire/config.h index c3ac55b08..2468bdd00 100644 --- a/src/libpakfire/include/pakfire/config.h +++ b/src/libpakfire/include/pakfire/config.h @@ -57,6 +57,7 @@ int pakfire_config_walk_sections(struct pakfire_config* config, int pakfire_config_has_section(struct pakfire_config* config, const char* section); int pakfire_config_read(struct pakfire_config* config, FILE* f); +int pakfire_config_parse(struct pakfire_config* config, const char* s, ssize_t length); int pakfire_config_dump(struct pakfire_config* config, FILE* f); FILE* pakfire_config_fdump(struct pakfire_config* config); diff --git a/tests/libpakfire/config.c b/tests/libpakfire/config.c index 04bd83fa2..372580734 100644 --- a/tests/libpakfire/config.c +++ b/tests/libpakfire/config.c @@ -77,8 +77,10 @@ FAIL: } static int test_parse(const struct test* t) { + struct pakfire_config* config = NULL; int r = EXIT_FAILURE; - char* TEST_INPUT = + + const char* TEST_INPUT = "key1 = value1\n" "\n" "# This is a comment\n" @@ -94,13 +96,8 @@ static int test_parse(const struct test* t) { "key1k = 1k\n" "key1X = 1X\n"; - FILE* f = fmemopen(TEST_INPUT, strlen(TEST_INPUT), "r"); - ASSERT(f); - - struct pakfire_config* config = NULL; - ASSERT_SUCCESS(pakfire_config_create(&config)); - ASSERT_SUCCESS(pakfire_config_read(config, f)); + ASSERT_SUCCESS(pakfire_config_parse(config, TEST_INPUT, -1)); // Dump the configuration file ASSERT_SUCCESS(pakfire_config_dump(config, stdout)); @@ -145,8 +142,6 @@ static int test_parse(const struct test* t) { r = EXIT_SUCCESS; FAIL: - if (f) - fclose(f); if (config) pakfire_config_unref(config); @@ -154,22 +149,18 @@ FAIL: } static int test_parse_multiline(const struct test* t) { + struct pakfire_config* config = NULL; int r = EXIT_FAILURE; - char* TEST_INPUT = + const char* TEST_INPUT = "key1 =\n" " value1\n" " value2\n" "\n" "key2 = value1\n"; - FILE* f = fmemopen(TEST_INPUT, strlen(TEST_INPUT), "r"); - ASSERT(f); - - struct pakfire_config* config = NULL; - ASSERT_SUCCESS(pakfire_config_create(&config)); - ASSERT_SUCCESS(pakfire_config_read(config, f)); + ASSERT_SUCCESS(pakfire_config_parse(config, TEST_INPUT, -1)); // Dump the configuration file ASSERT_SUCCESS(pakfire_config_dump(config, stdout)); @@ -181,8 +172,6 @@ static int test_parse_multiline(const struct test* t) { r = EXIT_SUCCESS; FAIL: - if (f) - fclose(f); if (config) pakfire_config_unref(config);