diff --git a/include/hyprutils/path/Path.hpp b/include/hyprutils/path/Path.hpp index d95a211..8634f20 100644 --- a/include/hyprutils/path/Path.hpp +++ b/include/hyprutils/path/Path.hpp @@ -2,6 +2,7 @@ #include "../string/VarList.hpp" #include #include +#include namespace Hyprutils { namespace Path { @@ -30,10 +31,12 @@ namespace Hyprutils { std::optional getXdgConfigHome(); /** Searches for a config according to the XDG Base Directory specification. - Returns either the full path to a config if found, or - $XDG_CONFIG_HOME/$HOME if no config could be found. + Returns a pair of the full path to a config and the base path. + Returns std::nullopt in case of a non-existent value. @param programName name of the program (and config file) */ - std::optional findConfig(const std::string programName); + + using T = std::optional; + std::pair findConfig(const std::string programName); } } diff --git a/src/path/Path.cpp b/src/path/Path.cpp index 923aba5..02a49c0 100644 --- a/src/path/Path.cpp +++ b/src/path/Path.cpp @@ -42,41 +42,31 @@ namespace Hyprutils::Path { return xdgConfigHome; } - std::optional findConfig(std::string programName) { - bool xdgConfigHomeExists = false; - static const auto xdgConfigHome = getXdgConfigHome(); + using T = std::optional; + std::pair findConfig(std::string programName) { + static const auto xdgConfigHome = getXdgConfigHome(); if (xdgConfigHome.has_value()) { if (checkConfigExists(xdgConfigHome.value(), programName)) - return fullConfigPath(xdgConfigHome.value(), programName); - else - xdgConfigHomeExists = true; + return std::make_pair(fullConfigPath(xdgConfigHome.value(), programName), xdgConfigHome); } - bool homeExists = false; - static const auto home = getHome(); + static const auto home = getHome(); if (home.has_value()) { if (checkConfigExists(home.value(), programName)) - return fullConfigPath(home.value(), programName); - else - homeExists = true; + return std::make_pair(fullConfigPath(home.value(), programName), home); } static const auto xdgConfigDirs = getXdgConfigDirs(); if (xdgConfigDirs.has_value()) { for (auto dir : xdgConfigDirs.value()) { if (checkConfigExists(dir, programName)) - return fullConfigPath(dir, programName); + return std::make_pair(fullConfigPath(dir, programName), std::nullopt); } } if (checkConfigExists("/etc/xdg", programName)) - return fullConfigPath("/etc/xdg", programName); + return std::make_pair(fullConfigPath("/etc/xdg", programName), std::nullopt); - if (xdgConfigHomeExists) - return xdgConfigHome; - else if (homeExists) - return home; - else - return std::nullopt; + return std::make_pair(std::nullopt, std::nullopt); } }