From: Michael Tremer Date: Fri, 19 Aug 2022 10:26:51 +0000 (+0000) Subject: config: Add function to get bytes X-Git-Tag: 0.9.28~438 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=887dee6884fe78ba3d679f25a811f4dd79f00c70;p=pakfire.git config: Add function to get bytes Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/config.c b/src/libpakfire/config.c index ac98be034..e36b48685 100644 --- a/src/libpakfire/config.c +++ b/src/libpakfire/config.c @@ -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; diff --git a/src/libpakfire/include/pakfire/config.h b/src/libpakfire/include/pakfire/config.h index d424603a0..72b3c4939 100644 --- a/src/libpakfire/include/pakfire/config.h +++ b/src/libpakfire/include/pakfire/config.h @@ -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); diff --git a/tests/libpakfire/config.c b/tests/libpakfire/config.c index afb121bdc..9773224d3 100644 --- a/tests/libpakfire/config.c +++ b/tests/libpakfire/config.c @@ -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;