all repos — mgba @ 42a2a5737b62b020cf577af4abb20ffa4419b8ea

mGBA Game Boy Advance Emulator

Util: Use VFile for configuration
Jeffrey Pfau jeffrey@endrift.com
Sun, 01 Nov 2015 16:50:05 -0800
commit

42a2a5737b62b020cf577af4abb20ffa4419b8ea

parent

8eb4f3ca4d1210e654248605a5fa48546b1d4ee5

2 files changed, 44 insertions(+), 14 deletions(-)

jump to
M CHANGESCHANGES

@@ -27,6 +27,7 @@ - SDL: Allow GBASDLAudio to be used without a thread context

- All: Improved PowerPC support - All: Fix some undefined behavior warnings - GBA Audio: Implement missing flags on SOUNDCNT_X register + - Util: Use VFile for configuration 0.3.1: (2015-10-24) Bugfixes:
M src/util/configuration.csrc/util/configuration.c

@@ -31,13 +31,25 @@ return 1;

} static void _keyHandler(const char* key, void* value, void* user) { - fprintf(user, "%s=%s\n", key, (const char*) value); + char line[256]; + struct VFile* vf = user; + size_t len = snprintf(line, sizeof(line), "%s=%s\n", key, (const char*) value); + if (len >= sizeof(line)) { + len = sizeof(line) - 1; + } + vf->write(vf, line, len); } static void _sectionHandler(const char* key, void* section, void* user) { - fprintf(user, "[%s]\n", key); + char line[256]; + struct VFile* vf = user; + size_t len = snprintf(line, sizeof(line), "[%s]\n", key); + if (len >= sizeof(line)) { + len = sizeof(line) - 1; + } + vf->write(vf, line, len); HashTableEnumerate(section, _keyHandler, user); - fprintf(user, "\n"); + vf->write(vf, "\n", 1); } void ConfigurationInit(struct Configuration* configuration) {

@@ -115,36 +127,53 @@ }

return HashTableLookup(currentSection, key); } +static char* _vfgets(char* stream, int size, void* user) { + struct VFile* vf = user; + if (vf->readline(vf, stream, size) > 0) { + return stream; + } + return 0; +} + bool ConfigurationRead(struct Configuration* configuration, const char* path) { HashTableClear(&configuration->root); HashTableClear(&configuration->sections); - return ini_parse(path, _iniRead, configuration) == 0; + struct VFile* vf = VFileOpen(path, O_RDONLY); + if (!vf) { + return false; + } + return ini_parse_stream(_vfgets, vf, _iniRead, configuration) == 0; } bool ConfigurationWrite(const struct Configuration* configuration, const char* path) { - FILE* file = fopen(path, "w"); - if (!file) { + struct VFile* vf = VFileOpen(path, O_WRONLY | O_CREAT | O_TRUNC); + if (!vf) { return false; } - HashTableEnumerate(&configuration->root, _keyHandler, file); - HashTableEnumerate(&configuration->sections, _sectionHandler, file); - fclose(file); + HashTableEnumerate(&configuration->root, _keyHandler, vf); + HashTableEnumerate(&configuration->sections, _sectionHandler, vf); + vf->close(vf); return true; } bool ConfigurationWriteSection(const struct Configuration* configuration, const char* path, const char* section) { const struct Table* currentSection = &configuration->root; - FILE* file = fopen(path, "w"); - if (!file) { + struct VFile* vf = VFileOpen(path, O_WRONLY | O_CREAT | O_APPEND); + if (!vf) { return false; } if (section) { currentSection = HashTableLookup(&configuration->sections, section); - fprintf(file, "[%s]\n", section); + char line[256]; + size_t len = snprintf(line, sizeof(line), "[%s]\n", section); + if (len >= sizeof(line)) { + len = sizeof(line) - 1; + } + vf->write(vf, line, len); } if (currentSection) { - HashTableEnumerate(currentSection, _sectionHandler, file); + HashTableEnumerate(currentSection, _sectionHandler, vf); } - fclose(file); + vf->close(vf); return true; }