Util: Add enumeration over a configuration section

This commit is contained in:
Vicki Pfau 2017-03-05 17:23:08 -08:00
parent 726986e447
commit 6363a08178
2 changed files with 22 additions and 0 deletions

View File

@ -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

View File

@ -18,6 +18,11 @@ struct ConfigurationSectionHandlerData {
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);
@ -63,6 +68,11 @@ static void _sectionEnumHandler(const char* key, void* section, void* user) {
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) {
HashTableInit(&configuration->sections, 0, _tableDeinit);
HashTableInit(&configuration->root, 0, _sectionDeinit);
@ -199,3 +209,14 @@ void ConfigurationEnumerateSections(const struct Configuration* configuration, v
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);
}
}