From 1e98372e080406fa7d17e9f6cde303e86a736104 Mon Sep 17 00:00:00 2001 From: ericek111 Date: Tue, 31 Mar 2026 11:37:16 +0200 Subject: [PATCH] config: platform-agnostic path handling --- src/core/Config.cpp | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index be8f154..8259db0 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -2,6 +2,9 @@ #include #include #include +#ifndef __EMSCRIPTEN__ +#include +#endif #ifdef __EMSCRIPTEN__ #include @@ -22,7 +25,6 @@ EM_JS(void, js_saveSettings, (const char* data), { }); #else -#include #endif namespace baudmine { @@ -30,16 +32,20 @@ namespace baudmine { std::string Config::defaultPath() { #ifdef __EMSCRIPTEN__ return ""; +#elif defined(_WIN32) + const char* appdata = std::getenv("APPDATA"); + auto base = std::filesystem::path(appdata ? appdata : "."); + return (base / "baudmine" / "settings.ini").string(); #else const char* xdg = std::getenv("XDG_CONFIG_HOME"); - std::string base; + std::filesystem::path base; if (xdg && xdg[0]) { base = xdg; } else { const char* home = std::getenv("HOME"); - base = home ? std::string(home) + "/.config" : "."; + base = home ? std::filesystem::path(home) / ".config" : std::filesystem::path("."); } - return base + "/baudmine/settings.ini"; + return (base / "baudmine" / "settings.ini").string(); #endif } @@ -77,19 +83,9 @@ bool Config::load(const std::string& path) { #ifndef __EMSCRIPTEN__ static void ensureDir(const std::string& path) { - // Create parent directories. - auto lastSlash = path.rfind('/'); - if (lastSlash == std::string::npos) return; - std::string dir = path.substr(0, lastSlash); - // Simple recursive mkdir. - for (size_t i = 1; i < dir.size(); ++i) { - if (dir[i] == '/') { - dir[i] = '\0'; - mkdir(dir.c_str(), 0755); - dir[i] = '/'; - } - } - mkdir(dir.c_str(), 0755); + std::filesystem::path p(path); + if (p.has_parent_path()) + std::filesystem::create_directories(p.parent_path()); } #endif