Util: Add enumeration over a configuration section
Vicki Pfau vi@endrift.com
Sun, 05 Mar 2017 17:23:08 -0800
2 files changed,
22 insertions(+),
0 deletions(-)
M
include/mgba-util/configuration.h
→
include/mgba-util/configuration.h
@@ -38,6 +38,7 @@ bool ConfigurationWrite(const struct Configuration*, const char* path);
bool ConfigurationWriteSection(const struct Configuration*, const char* path, const char* section); void ConfigurationEnumerateSections(const struct Configuration* configuration, void (*handler)(const char* sectionName, void* user), void* user); +void ConfigurationEnumerate(const struct Configuration* configuration, const char* section, void (*handler)(const char* key, const char* value, void* user), void* user); CXX_GUARD_END
M
src/util/configuration.c
→
src/util/configuration.c
@@ -18,6 +18,11 @@ void (*handler)(const char* section, void* data);
void* data; }; +struct ConfigurationHandlerData { + void (*handler)(const char* key, const char* value, void* data); + void* data; +}; + static void _tableDeinit(void* table) { TableDeinit(table); free(table);@@ -61,6 +66,11 @@ static void _sectionEnumHandler(const char* key, void* section, void* user) {
struct ConfigurationSectionHandlerData* data = user; UNUSED(section); data->handler(key, data->data); +} + +static void _enumHandler(const char* key, void* value, void* user) { + struct ConfigurationHandlerData* data = user; + data->handler(key, value, data->data); } void ConfigurationInit(struct Configuration* configuration) {@@ -199,3 +209,14 @@ void ConfigurationEnumerateSections(const struct Configuration* configuration, void (*handler)(const char* sectionName, void* user), void* user) {
struct ConfigurationSectionHandlerData handlerData = { handler, user }; HashTableEnumerate(&configuration->sections, _sectionEnumHandler, &handlerData); } + +void ConfigurationEnumerate(const struct Configuration* configuration, const char* section, void (*handler)(const char* key, const char* value, void* user), void* user) { + struct ConfigurationHandlerData handlerData = { handler, user }; + const struct Table* currentSection = &configuration->root; + if (section) { + currentSection = HashTableLookup(&configuration->sections, section); + } + if (currentSection) { + HashTableEnumerate(currentSection, _enumHandler, &handlerData); + } +}