mirror of
https://github.com/hyprwm/hyprutils.git
synced 2024-11-17 02:35:58 +01:00
path: add findConfig and dir utils
This commit is contained in:
parent
a8c3a13570
commit
338e715a95
2 changed files with 121 additions and 0 deletions
39
include/hyprutils/path/Path.hpp
Normal file
39
include/hyprutils/path/Path.hpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
#pragma once
|
||||
#include "../string/VarList.hpp"
|
||||
#include <string>
|
||||
#include <optional>
|
||||
|
||||
namespace Hyprutils {
|
||||
namespace Path {
|
||||
/** Check whether a config in the form basePath/hypr/programName.conf exists.
|
||||
@param basePath the path where the config will be searched
|
||||
@param programName name of the program (and config file) to search for
|
||||
*/
|
||||
bool checkConfigExists(const std::string basePath, const std::string programName);
|
||||
|
||||
/** Constructs a full config path given the basePath and programName.
|
||||
@param basePath the path where the config hypr/programName.conf is located
|
||||
@param programName name of the program (and config file)
|
||||
*/
|
||||
std::string fullConfigPath(const std::string basePath, const std::string programName);
|
||||
|
||||
/** Retrieves the absolute path of the $HOME env variable.
|
||||
*/
|
||||
std::optional<std::string> getHome();
|
||||
|
||||
/** Retrieves a CVarList of paths from the $XDG_CONFIG_DIRS env variable.
|
||||
*/
|
||||
std::optional<String::CVarList> getXdgConfigDirs();
|
||||
|
||||
/** Retrieves the absolute path of the $XDG_CONFIG_HOME env variable.
|
||||
*/
|
||||
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.
|
||||
@param programName name of the program (and config file)
|
||||
*/
|
||||
std::optional<std::string> findConfig(const std::string programName);
|
||||
}
|
||||
}
|
82
src/path/Path.cpp
Normal file
82
src/path/Path.cpp
Normal file
|
@ -0,0 +1,82 @@
|
|||
#include <hyprutils/path/Path.hpp>
|
||||
#include <hyprutils/string/VarList.hpp>
|
||||
#include <filesystem>
|
||||
|
||||
using namespace Hyprutils;
|
||||
|
||||
namespace Hyprutils::Path {
|
||||
std::string fullConfigPath(std::string basePath, std::string programName) {
|
||||
return basePath + "/hypr/" + programName + ".conf";
|
||||
}
|
||||
|
||||
bool checkConfigExists(std::string basePath, std::string programName) {
|
||||
return std::filesystem::exists(fullConfigPath(basePath, programName));
|
||||
}
|
||||
|
||||
std::optional<std::string> getHome() {
|
||||
static const auto homeDir = getenv("HOME");
|
||||
|
||||
if (!homeDir || !std::filesystem::path(homeDir).is_absolute())
|
||||
return std::nullopt;
|
||||
|
||||
return std::string(homeDir).append("/.config");
|
||||
}
|
||||
|
||||
std::optional<String::CVarList> getXdgConfigDirs() {
|
||||
static const auto xdgConfigDirs = getenv("XDG_CONFIG_DIRS");
|
||||
|
||||
if (!xdgConfigDirs)
|
||||
return std::nullopt;
|
||||
|
||||
static const auto xdgConfigDirsList = String::CVarList(xdgConfigDirs, 0, ':');
|
||||
|
||||
return xdgConfigDirsList;
|
||||
}
|
||||
|
||||
std::optional<std::string> getXdgConfigHome() {
|
||||
static const auto xdgConfigHome = getenv("XDG_CONFIG_HOME");
|
||||
|
||||
if (!xdgConfigHome || !std::filesystem::path(xdgConfigHome).is_absolute())
|
||||
return std::nullopt;
|
||||
|
||||
return xdgConfigHome;
|
||||
}
|
||||
|
||||
std::optional<std::string> findConfig(std::string programName) {
|
||||
bool xdgConfigHomeExists = false;
|
||||
static const auto xdgConfigHome = getXdgConfigHome();
|
||||
if (xdgConfigHome.has_value()) {
|
||||
if (checkConfigExists(xdgConfigHome.value(), programName))
|
||||
return fullConfigPath(xdgConfigHome.value(), programName);
|
||||
else
|
||||
xdgConfigHomeExists = true;
|
||||
}
|
||||
|
||||
bool homeExists = false;
|
||||
static const auto home = getHome();
|
||||
if (home.has_value()) {
|
||||
if (checkConfigExists(home.value(), programName))
|
||||
return fullConfigPath(home.value(), programName);
|
||||
else
|
||||
homeExists = true;
|
||||
}
|
||||
|
||||
static const auto xdgConfigDirs = getXdgConfigDirs();
|
||||
if (xdgConfigDirs.has_value()) {
|
||||
for (auto dir : xdgConfigDirs.value()) {
|
||||
if (checkConfigExists(dir, programName))
|
||||
return fullConfigPath(dir, programName);
|
||||
}
|
||||
}
|
||||
|
||||
if (checkConfigExists("/etc/xdg", programName))
|
||||
return fullConfigPath("/etc/xdg", programName);
|
||||
|
||||
if (xdgConfigHomeExists)
|
||||
return xdgConfigHome;
|
||||
else if (homeExists)
|
||||
return home;
|
||||
else
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue