From b576bd0fcc15dc2b4390320301fefbc28c9d3c65 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Wed, 15 Dec 2021 17:43:11 -0500 Subject: make fallback for config file go in home directory if possible (#52) --- src/platform.cpp | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/platform.cpp b/src/platform.cpp index ce744d7..dc64a26 100644 --- a/src/platform.cpp +++ b/src/platform.cpp @@ -1,18 +1,18 @@ #include "platform.hpp" #include "util.hpp" -#include -#include -#include #include +#include +#include +#include using namespace std::literals::string_literals; #if defined(_WIN32) && defined(_MSC_VER) - #include - #include - #include #include #include + #include + #include + #include #pragma comment(lib, "Shlwapi.lib") bool Platform::SetupFonts() { using namespace std::string_literals; @@ -107,17 +107,29 @@ std::string Platform::FindResourceFolder() { } std::string Platform::FindConfigFile() { - const auto x = std::getenv("ABADDON_CONFIG"); - if (x != nullptr) - return x; + const auto cfg = std::getenv("ABADDON_CONFIG"); + if (cfg != nullptr) return cfg; - const auto home_env = std::getenv("HOME"); - if (home_env != nullptr) { - const auto home_path = home_env + "/.config/abaddon/abaddon.ini"s; - for (auto path : { "./abaddon.ini"s, home_path }) { - if (util::IsFile(path)) return path; + // use config present in cwd first + if (util::IsFile("./abaddon.ini")) + return "./abaddon.ini"; + + if (const auto home_env = std::getenv("HOME")) { + // use ~/.config if present + if (auto home_path = home_env + "/.config/abaddon/abaddon.ini"s; util::IsFile(home_path)) { + return home_path; } + + // fallback to ~/.config if the directory exists/can be created + std::error_code ec; + const auto home_path = home_env + "/.config/abaddon"s; + if (!util::IsFolder(home_path)) + std::filesystem::create_directories(home_path, ec); + if (util::IsFolder(home_path)) + return home_path + "/abaddon.ini"; } + + // fallback to cwd if cant find + cant make in ~/.config puts("can't find configuration file!"); return "./abaddon.ini"; } -- cgit v1.2.3