Skip to content

Commit

Permalink
Add support for XDG base directory.
Browse files Browse the repository at this point in the history
Try to use $XDG_CONFIG_HOME, $XDG_DATA_HOME and $XDG_CACHE_HOME on
Linux before using $HOME/.config.
  • Loading branch information
c4llv07e committed May 17, 2024
1 parent ada3420 commit fc57c73
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ std::string Platform::FindResourceFolder() {
static bool found = false;
if (found) return found_path;

const auto data_env = std::getenv("XDG_DATA_HOME");
if (data_env != nullptr) {
const static std::string data_path = data_env + "/abaddon"s;

for (const auto &path : { "."s, data_path, std::string(ABADDON_DEFAULT_RESOURCE_DIR) }) {
if (util::IsFolder(path + "/res") && util::IsFolder(path + "/css")) {
found_path = path;
found = true;
return found_path;
}
}
}

const auto home_env = std::getenv("HOME");
if (home_env != nullptr) {
const static std::string home_path = home_env + "/.local/share/abaddon"s;
Expand Down Expand Up @@ -119,6 +132,19 @@ std::string Platform::FindConfigFile() {
if (util::IsFile("./abaddon.ini"))
return "./abaddon.ini";

if (const auto config_env = std::getenv("XDG_CONFIG_HOME")) {
if (auto config_path = config_env + "/abaddon/abaddon.ini"s; util::IsFile(config_path)) {
return config_path;
}
std::error_code ec;
const auto config_path = config_env + "/abaddon"s;
if (!util::IsFolder(config_path))
std::filesystem::create_directories(config_path, ec);
if (util::IsFolder(config_path))
return config_path + "/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)) {
Expand All @@ -140,6 +166,15 @@ std::string Platform::FindConfigFile() {
}

std::string Platform::FindStateCacheFolder() {
const auto cache_env = std::getenv("XDG_CACHE_HOME");
if (cache_env != nullptr) {
auto cache_path = cache_env + "/abaddon"s;
std::error_code ec;
if (!util::IsFolder(cache_path))
std::filesystem::create_directories(cache_path, ec);
if (util::IsFolder(cache_path))
return cache_path;
}
const auto home_env = std::getenv("HOME");
if (home_env != nullptr) {
auto home_path = home_env + "/.cache/abaddon"s;
Expand Down

0 comments on commit fc57c73

Please sign in to comment.