]> git.ipfire.org Git - pakfire.git/commitdiff
config: Add helper function to parse strings
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 22 Nov 2024 11:17:31 +0000 (11:17 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 22 Nov 2024 11:18:22 +0000 (11:18 +0000)
That helps us to avoid all sorts of weird type casting in the test
cases.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/config.c
src/libpakfire/include/pakfire/config.h
tests/libpakfire/config.c

index 012044be835d411cfd9cfb1fa30204012f3e5859..0a5e43a17f54395058165711615a8468cf88e105 100644 (file)
@@ -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;
index c3ac55b081a8d8eb27664c9d5e323d01337a3ad3..2468bdd00bb83a3d839076e8ed8e0cc0dae7a4eb 100644 (file)
@@ -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);
index 04bd83fa2e485669424f5bbec9885bc34d2de72a..3725807342d9e6deec101c2b7074c52333e901be 100644 (file)
@@ -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);