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;
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);
"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);
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);
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;