mirror of
https://github.com/hyprwm/hyprutils.git
synced 2024-11-17 00:15:58 +01:00
findConfig: return pair
This commit is contained in:
parent
338e715a95
commit
e73ddfe051
2 changed files with 15 additions and 22 deletions
|
@ -2,6 +2,7 @@
|
||||||
#include "../string/VarList.hpp"
|
#include "../string/VarList.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace Hyprutils {
|
namespace Hyprutils {
|
||||||
namespace Path {
|
namespace Path {
|
||||||
|
@ -30,10 +31,12 @@ namespace Hyprutils {
|
||||||
std::optional<std::string> getXdgConfigHome();
|
std::optional<std::string> getXdgConfigHome();
|
||||||
|
|
||||||
/** Searches for a config according to the XDG Base Directory specification.
|
/** Searches for a config according to the XDG Base Directory specification.
|
||||||
Returns either the full path to a config if found, or
|
Returns a pair of the full path to a config and the base path.
|
||||||
$XDG_CONFIG_HOME/$HOME if no config could be found.
|
Returns std::nullopt in case of a non-existent value.
|
||||||
@param programName name of the program (and config file)
|
@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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,41 +42,31 @@ namespace Hyprutils::Path {
|
||||||
return xdgConfigHome;
|
return xdgConfigHome;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::string> findConfig(std::string programName) {
|
using T = std::optional<std::string>;
|
||||||
bool xdgConfigHomeExists = false;
|
std::pair<T, T> findConfig(std::string programName) {
|
||||||
static const auto xdgConfigHome = getXdgConfigHome();
|
static const auto xdgConfigHome = getXdgConfigHome();
|
||||||
if (xdgConfigHome.has_value()) {
|
if (xdgConfigHome.has_value()) {
|
||||||
if (checkConfigExists(xdgConfigHome.value(), programName))
|
if (checkConfigExists(xdgConfigHome.value(), programName))
|
||||||
return fullConfigPath(xdgConfigHome.value(), programName);
|
return std::make_pair(fullConfigPath(xdgConfigHome.value(), programName), xdgConfigHome);
|
||||||
else
|
|
||||||
xdgConfigHomeExists = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool homeExists = false;
|
static const auto home = getHome();
|
||||||
static const auto home = getHome();
|
|
||||||
if (home.has_value()) {
|
if (home.has_value()) {
|
||||||
if (checkConfigExists(home.value(), programName))
|
if (checkConfigExists(home.value(), programName))
|
||||||
return fullConfigPath(home.value(), programName);
|
return std::make_pair(fullConfigPath(home.value(), programName), home);
|
||||||
else
|
|
||||||
homeExists = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const auto xdgConfigDirs = getXdgConfigDirs();
|
static const auto xdgConfigDirs = getXdgConfigDirs();
|
||||||
if (xdgConfigDirs.has_value()) {
|
if (xdgConfigDirs.has_value()) {
|
||||||
for (auto dir : xdgConfigDirs.value()) {
|
for (auto dir : xdgConfigDirs.value()) {
|
||||||
if (checkConfigExists(dir, programName))
|
if (checkConfigExists(dir, programName))
|
||||||
return fullConfigPath(dir, programName);
|
return std::make_pair(fullConfigPath(dir, programName), std::nullopt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkConfigExists("/etc/xdg", programName))
|
if (checkConfigExists("/etc/xdg", programName))
|
||||||
return fullConfigPath("/etc/xdg", programName);
|
return std::make_pair(fullConfigPath("/etc/xdg", programName), std::nullopt);
|
||||||
|
|
||||||
if (xdgConfigHomeExists)
|
return std::make_pair(std::nullopt, std::nullopt);
|
||||||
return xdgConfigHome;
|
|
||||||
else if (homeExists)
|
|
||||||
return home;
|
|
||||||
else
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue