findConfig: return pair

This commit is contained in:
Mihai Fufezan 2024-07-09 18:01:03 +03:00
parent 338e715a95
commit e73ddfe051
Signed by: fufexan
SSH Key Fingerprint: SHA256:SdnKmEpJrDu1+2UO1QpB/Eg4HKcdDi6n+xSRqFNJVpg
2 changed files with 15 additions and 22 deletions

View File

@ -2,6 +2,7 @@
#include "../string/VarList.hpp"
#include <string>
#include <optional>
#include <utility>
namespace Hyprutils {
namespace Path {
@ -30,10 +31,12 @@ namespace Hyprutils {
std::optional<std::string> 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<std::string> findConfig(const std::string programName);
using T = std::optional<std::string>;
std::pair<T, T> findConfig(const std::string programName);
}
}

View File

@ -42,41 +42,31 @@ namespace Hyprutils::Path {
return xdgConfigHome;
}
std::optional<std::string> findConfig(std::string programName) {
bool xdgConfigHomeExists = false;
static const auto xdgConfigHome = getXdgConfigHome();
using T = std::optional<std::string>;
std::pair<T, T> 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);
}
}