GBA Config: Expose emulator config directory

This commit is contained in:
Jeffrey Pfau 2014-12-20 18:41:41 -08:00
parent 8fc3942891
commit 2fb098cb01
2 changed files with 19 additions and 17 deletions

View File

@ -13,6 +13,9 @@
#include <windows.h> #include <windows.h>
#include <shlobj.h> #include <shlobj.h>
#include <strsafe.h> #include <strsafe.h>
#define PATH_SEP "\\"
#else
#define PATH_SEP "/"
#endif #endif
#define SECTION_NAME_MAX 128 #define SECTION_NAME_MAX 128
@ -111,34 +114,31 @@ void GBAConfigDeinit(struct GBAConfig* config) {
bool GBAConfigLoad(struct GBAConfig* config) { bool GBAConfigLoad(struct GBAConfig* config) {
char path[PATH_MAX]; char path[PATH_MAX];
#ifndef _WIN32 GBAConfigDirectory(path, PATH_MAX);
char* home = getenv("HOME"); strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
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
return ConfigurationRead(&config->configTable, path); return ConfigurationRead(&config->configTable, path);
} }
bool GBAConfigSave(const struct GBAConfig* config) { bool GBAConfigSave(const struct GBAConfig* config) {
char path[PATH_MAX]; 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 #ifndef _WIN32
char* home = getenv("HOME"); char* home = getenv("HOME");
snprintf(path, PATH_MAX, "%s/.config", home); snprintf(out, outLength, "%s/.config", home);
mkdir(path, 0755); mkdir(out, 0755);
snprintf(path, PATH_MAX, "%s/.config/%s", home, BINARY_NAME); snprintf(out, outLength, "%s/.config/%s", home, BINARY_NAME);
mkdir(path, 0755); mkdir(out, 0755);
snprintf(path, PATH_MAX, "%s/.config/%s/config.ini", home, BINARY_NAME);
#else #else
char home[MAX_PATH]; char home[MAX_PATH];
SHGetFolderPath(0, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, home); SHGetFolderPath(0, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, home);
snprintf(path, PATH_MAX, "%s/%s", home, PROJECT_NAME); snprintf(out, outLength, "%s\\%s", home, PROJECT_NAME);
CreateDirectoryA(path, NULL); CreateDirectoryA(out, NULL);
snprintf(path, PATH_MAX, "%s/%s/config.ini", home, PROJECT_NAME);
#endif #endif
return ConfigurationWrite(&config->configTable, path);
} }
const char* GBAConfigGetValue(const struct GBAConfig* config, const char* key) { const char* GBAConfigGetValue(const struct GBAConfig* config, const char* key) {

View File

@ -40,6 +40,8 @@ void GBAConfigDeinit(struct GBAConfig*);
bool GBAConfigLoad(struct GBAConfig*); bool GBAConfigLoad(struct GBAConfig*);
bool GBAConfigSave(const struct GBAConfig*); bool GBAConfigSave(const struct GBAConfig*);
void GBAConfigDirectory(char* out, size_t outLength);
const char* GBAConfigGetValue(const struct GBAConfig*, const char* key); const char* GBAConfigGetValue(const struct GBAConfig*, const char* key);
void GBAConfigSetValue(struct GBAConfig*, const char* key, const char* value); void GBAConfigSetValue(struct GBAConfig*, const char* key, const char* value);