]> git.ipfire.org Git - pakfire.git/commitdiff
config: Add function to get bytes
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 Aug 2022 10:26:51 +0000 (10:26 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 Aug 2022 10:26:51 +0000 (10:26 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/config.c
src/libpakfire/include/pakfire/config.h
tests/libpakfire/config.c

index ac98be03487f2aea46d70dbd870fce9d61a6eaa2..e36b48685d31ef7670eb4db9640f6b492b4b94fc 100644 (file)
@@ -224,6 +224,17 @@ int pakfire_config_get_bool(struct pakfire_config* config,
        return _default;
 }
 
+size_t pakfire_config_get_bytes(struct pakfire_config* config,
+               const char* section, const char* key, const size_t _default) {
+       struct pakfire_config_entry* entry = pakfire_config_find(config, section, key);
+
+       if (entry && *entry->value)
+               return pakfire_string_parse_bytes(entry->value);
+
+       // Otherwise return the default
+       return _default;
+}
+
 static int pakfire_section_in_sections(char** sections, const char* section) {
        if (!sections)
                return 0;
index d424603a02608d203ac5e165ba9ea3bad3fd4d97..72b3c49393ba590b70d5fc03608b6492280e39bf 100644 (file)
@@ -40,6 +40,8 @@ long int pakfire_config_get_int(struct pakfire_config* config,
        const char* section, const char* key, long int _default);
 int pakfire_config_get_bool(struct pakfire_config* config,
        const char* section, const char* key, int _default);
+size_t pakfire_config_get_bytes(struct pakfire_config* config,
+       const char* section, const char* key, const size_t _default);
 
 char** pakfire_config_sections(struct pakfire_config* config);
 int pakfire_config_has_section(struct pakfire_config* config, const char* section);
index afb121bdc5d292f07667a20c5b6060bea3ba75da..9773224d31a67f0aa5f76cd79e7bc3df0f4e44b0 100644 (file)
@@ -84,7 +84,12 @@ static int test_parse(const struct test* t) {
                "key2 = value2\n"
                "[section2]\n"
                "key1 = value1\n"
-               "key2 = value2\n";
+               "key2 = value2\n"
+               "[bytes]\n"
+               "key0 = 0\n"
+               "key1 = 1\n"
+               "key1k = 1k\n"
+               "key1X = 1X\n";
 
        FILE* f = fmemopen(TEST_INPUT, strlen(TEST_INPUT), "r");
        ASSERT(f);
@@ -100,6 +105,17 @@ static int test_parse(const struct test* t) {
        ASSERT_STRING_EQUALS(pakfire_config_get(config, "section2", "key1", NULL), "value1");
        ASSERT_STRING_EQUALS(pakfire_config_get(config, "section2", "key2", NULL), "value2");
 
+       // Check bytes
+       ASSERT(pakfire_config_get_bytes(config, "bytes", "key0", 123) == 0);
+       ASSERT(pakfire_config_get_bytes(config, "bytes", "key1", 123) == 1);
+       ASSERT(pakfire_config_get_bytes(config, "bytes", "key1k", 123) == 1024);
+
+       // Check an invalid value for bytes
+       ASSERT_ERRNO(pakfire_config_get_bytes(config, "bytes", "key1X", 123) == 0, EINVAL);
+
+       // Check for a non-existant key
+       ASSERT(pakfire_config_get_bytes(config, "bytes", "keyXX", 123) == 123);
+
        // Get sections
        char** sections = pakfire_config_sections(config);
        ASSERT(sections);
@@ -109,12 +125,13 @@ static int test_parse(const struct test* t) {
        for (char** section = sections; *section; section++)
                counter++;
 
-       ASSERT(counter == 2);
+       ASSERT(counter == 3);
 
        // Check for section names
        ASSERT_STRING_EQUALS(sections[0], "section1");
        ASSERT_STRING_EQUALS(sections[1], "section2");
-       ASSERT_NULL(sections[2]);
+       ASSERT_STRING_EQUALS(sections[2], "bytes");
+       ASSERT_NULL(sections[3]);
 
        // Everything passed
        r = EXIT_SUCCESS;