Util: Add Configuration datatype

This commit is contained in:
Jeffrey Pfau 2014-11-01 03:02:40 -07:00
parent e66bcfb818
commit 5f440b6a02
2 changed files with 118 additions and 0 deletions

95
src/util/configuration.c Normal file
View File

@ -0,0 +1,95 @@
#include "configuration.h"
#include "util/vfs.h"
#include "inih/ini.h"
static void _sectionDeinit(void* string) {
free(string);
}
static int _iniRead(void* configuration, const char* section, const char* key, const char* value) {
if (section && !section[0]) {
section = 0;
}
ConfigurationSetValue(configuration, section, key, value);
return 1;
}
static void _keyHandler(const char* key, void* value, void* user) {
dprintf((int) user, "%s=%s\n", key, value);
}
static void _sectionHandler(const char* key, void* section, void* user) {
dprintf((int) user, "[%s]\n", key);
HashTableEnumerate(section, _keyHandler, user);
dprintf((int) user, "\n");
}
void ConfigurationInit(struct Configuration* configuration) {
HashTableInit(&configuration->sections, 0, (void (*)(void *)) TableDeinit);
HashTableInit(&configuration->root, 0, _sectionDeinit);
}
void ConfigurationDeinit(struct Configuration* configuration) {
HashTableDeinit(&configuration->sections);
HashTableDeinit(&configuration->root);
}
void ConfigurationSetValue(struct Configuration* configuration, const char* section, const char* key, const char* value) {
struct Table* currentSection = &configuration->root;
if (section) {
currentSection = HashTableLookup(&configuration->sections, section);
if (!currentSection) {
currentSection = malloc(sizeof(*currentSection));
HashTableInit(currentSection, 0, _sectionDeinit);
HashTableInsert(&configuration->sections, section, currentSection);
}
}
HashTableInsert(currentSection, key, strdup(value));
}
const char* ConfigurationGetValue(const struct Configuration* configuration, const char* section, const char* key) {
const struct Table* currentSection = &configuration->root;
if (section) {
currentSection = HashTableLookup(&configuration->sections, section);
if (!currentSection) {
return 0;
}
}
return HashTableLookup(currentSection, key);
}
bool ConfigurationRead(struct Configuration* configuration, const char* path) {
HashTableClear(&configuration->root);
HashTableClear(&configuration->sections);
return ini_parse(path, _iniRead, configuration) == 0;
}
bool ConfigurationWrite(const struct Configuration* configuration, const char* path) {
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0) {
return false;
}
HashTableEnumerate(&configuration->root, _keyHandler, (void*) fd);
HashTableEnumerate(&configuration->sections, _sectionHandler, (void*) fd);
close(fd);
return true;
}
bool ConfigurationWriteSection(const struct Configuration* configuration, const char* path, const char* section) {
struct Table* currentSection = &configuration->root;
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0) {
return false;
}
if (section) {
currentSection = HashTableLookup(&configuration->sections, section);
dprintf(fd, "[%s]\n", section);
}
if (currentSection) {
HashTableEnumerate(currentSection, _sectionHandler, (void*) fd);
}
close(fd);
return true;
}

23
src/util/configuration.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef CONFIGURATION_H
#define CONFIGURATION_H
#include "util/table.h"
struct VFile;
struct Configuration {
struct Table sections;
struct Table root;
};
void ConfigurationInit(struct Configuration*);
void ConfigurationDeinit(struct Configuration*);
void ConfigurationSetValue(struct Configuration*, const char* section, const char* key, const char* value);
const char* ConfigurationGetValue(const struct Configuration*, const char* section, const char* key);
bool ConfigurationRead(struct Configuration*, const char* path);
bool ConfigurationWrite(const struct Configuration*, const char* path);
bool ConfigurationWriteSection(const struct Configuration*, const char* path, const char* section);
#endif