Util: Configuration should use FILE instead of fds to be more portable

This commit is contained in:
Jeffrey Pfau 2014-11-10 23:45:29 -08:00
parent fba659daa1
commit 0ef07f7c26
1 changed files with 13 additions and 13 deletions

View File

@ -24,13 +24,13 @@ static int _iniRead(void* configuration, const char* section, const char* key, c
} }
static void _keyHandler(const char* key, void* value, void* user) { static void _keyHandler(const char* key, void* value, void* user) {
dprintf((int) user, "%s=%s\n", key, value); fprintf(user, "%s=%s\n", key, (const char*) value);
} }
static void _sectionHandler(const char* key, void* section, void* user) { static void _sectionHandler(const char* key, void* section, void* user) {
dprintf((int) user, "[%s]\n", key); fprintf(user, "[%s]\n", key);
HashTableEnumerate(section, _keyHandler, user); HashTableEnumerate(section, _keyHandler, user);
dprintf((int) user, "\n"); fprintf(user, "\n");
} }
void ConfigurationInit(struct Configuration* configuration) { void ConfigurationInit(struct Configuration* configuration) {
@ -96,29 +96,29 @@ bool ConfigurationRead(struct Configuration* configuration, const char* path) {
} }
bool ConfigurationWrite(const struct Configuration* configuration, const char* path) { bool ConfigurationWrite(const struct Configuration* configuration, const char* path) {
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666); FILE* file = fopen(path, "w");
if (fd < 0) { if (!file) {
return false; return false;
} }
HashTableEnumerate(&configuration->root, _keyHandler, (void*) fd); HashTableEnumerate(&configuration->root, _keyHandler, file);
HashTableEnumerate(&configuration->sections, _sectionHandler, (void*) fd); HashTableEnumerate(&configuration->sections, _sectionHandler, file);
close(fd); fclose(file);
return true; return true;
} }
bool ConfigurationWriteSection(const struct Configuration* configuration, const char* path, const char* section) { bool ConfigurationWriteSection(const struct Configuration* configuration, const char* path, const char* section) {
const struct Table* currentSection = &configuration->root; const struct Table* currentSection = &configuration->root;
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666); FILE* file = fopen(path, "w");
if (fd < 0) { if (!file) {
return false; return false;
} }
if (section) { if (section) {
currentSection = HashTableLookup(&configuration->sections, section); currentSection = HashTableLookup(&configuration->sections, section);
dprintf(fd, "[%s]\n", section); fprintf(file, "[%s]\n", section);
} }
if (currentSection) { if (currentSection) {
HashTableEnumerate(currentSection, _sectionHandler, (void*) fd); HashTableEnumerate(currentSection, _sectionHandler, file);
} }
close(fd); fclose(file);
return true; return true;
} }