From 2fb098cb014a0412fe2949be9259873e3df2bae8 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sat, 20 Dec 2014 18:41:41 -0800 Subject: [PATCH] GBA Config: Expose emulator config directory --- src/gba/gba-config.c | 34 +++++++++++++++++----------------- src/gba/gba-config.h | 2 ++ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/gba/gba-config.c b/src/gba/gba-config.c index 8ff5bd346..a59ecc47a 100644 --- a/src/gba/gba-config.c +++ b/src/gba/gba-config.c @@ -13,6 +13,9 @@ #include #include #include +#define PATH_SEP "\\" +#else +#define PATH_SEP "/" #endif #define SECTION_NAME_MAX 128 @@ -111,34 +114,31 @@ void GBAConfigDeinit(struct GBAConfig* config) { bool GBAConfigLoad(struct GBAConfig* config) { char path[PATH_MAX]; -#ifndef _WIN32 - char* home = getenv("HOME"); - snprintf(path, PATH_MAX, "%s/.config/%s/config.ini", home, BINARY_NAME); -#else - char home[MAX_PATH]; - SHGetFolderPath(0, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, home); - snprintf(path, PATH_MAX, "%s/%s/config.ini", home, PROJECT_NAME); -#endif + GBAConfigDirectory(path, PATH_MAX); + strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path)); return ConfigurationRead(&config->configTable, path); } bool GBAConfigSave(const struct GBAConfig* config) { char path[PATH_MAX]; + GBAConfigDirectory(path, PATH_MAX); + strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path)); + return ConfigurationWrite(&config->configTable, path); +} + +void GBAConfigDirectory(char* out, size_t outLength) { #ifndef _WIN32 char* home = getenv("HOME"); - snprintf(path, PATH_MAX, "%s/.config", home); - mkdir(path, 0755); - snprintf(path, PATH_MAX, "%s/.config/%s", home, BINARY_NAME); - mkdir(path, 0755); - snprintf(path, PATH_MAX, "%s/.config/%s/config.ini", home, BINARY_NAME); + snprintf(out, outLength, "%s/.config", home); + mkdir(out, 0755); + snprintf(out, outLength, "%s/.config/%s", home, BINARY_NAME); + mkdir(out, 0755); #else char home[MAX_PATH]; SHGetFolderPath(0, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, home); - snprintf(path, PATH_MAX, "%s/%s", home, PROJECT_NAME); - CreateDirectoryA(path, NULL); - snprintf(path, PATH_MAX, "%s/%s/config.ini", home, PROJECT_NAME); + snprintf(out, outLength, "%s\\%s", home, PROJECT_NAME); + CreateDirectoryA(out, NULL); #endif - return ConfigurationWrite(&config->configTable, path); } const char* GBAConfigGetValue(const struct GBAConfig* config, const char* key) { diff --git a/src/gba/gba-config.h b/src/gba/gba-config.h index 59daf9ce8..5ed3aa7f0 100644 --- a/src/gba/gba-config.h +++ b/src/gba/gba-config.h @@ -40,6 +40,8 @@ void GBAConfigDeinit(struct GBAConfig*); bool GBAConfigLoad(struct GBAConfig*); bool GBAConfigSave(const struct GBAConfig*); +void GBAConfigDirectory(char* out, size_t outLength); + const char* GBAConfigGetValue(const struct GBAConfig*, const char* key); void GBAConfigSetValue(struct GBAConfig*, const char* key, const char* value);