From 8158bd2195bab0dd8579eb6a95d692dfeb1edd2a Mon Sep 17 00:00:00 2001 From: Mihai Fufezan Date: Thu, 7 May 2026 12:43:02 +0300 Subject: [PATCH] path: add function overloads with extension param --- include/hyprutils/path/Path.hpp | 23 +++++++++++++++++++++++ src/path/Path.cpp | 28 ++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/include/hyprutils/path/Path.hpp b/include/hyprutils/path/Path.hpp index 1df0fed..fe8816c 100644 --- a/include/hyprutils/path/Path.hpp +++ b/include/hyprutils/path/Path.hpp @@ -13,12 +13,26 @@ namespace Hyprutils { */ bool checkConfigExists(const std::string basePath, const std::string programName); + /** Check whether a config in the form basePath/hypr/programName.{extension} exists. + @param basePath the path where the config will be searched + @param programName name of the program (and config file) to search for + @param extension extension of the config file + */ + bool checkConfigExists(const std::string basePath, const std::string programName, const std::string extension); + /** 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); + /** 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) + @param extension extension of the config file + */ + std::string fullConfigPath(const std::string basePath, const std::string programName, const std::string extension); + /** Retrieves the absolute path of the $HOME env variable. */ std::optional getHome(); @@ -40,6 +54,15 @@ namespace Hyprutils { using T = std::optional; std::pair findConfig(const std::string programName); + /** Searches for a config according to the XDG Base Directory specification. + 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) + @param extension extension of the config file + */ + + std::pair findConfig(const std::string programName, const std::string extension); + /** Resolves a path, expanding ~, ., and .. components relative to the given base. If base is empty, the current working directory is used as the base for relative paths. Returns the resolved absolute path, or an error string on failure. diff --git a/src/path/Path.cpp b/src/path/Path.cpp index af7211f..ae8fa99 100644 --- a/src/path/Path.cpp +++ b/src/path/Path.cpp @@ -10,10 +10,18 @@ namespace Hyprutils::Path { return basePath + "/hypr/" + programName + ".conf"; } + std::string fullConfigPath(std::string basePath, std::string programName, std::string extension) { + return basePath + "/hypr/" + programName + "." + extension; + } + bool checkConfigExists(std::string basePath, std::string programName) { return std::filesystem::exists(fullConfigPath(basePath, programName)); } + bool checkConfigExists(std::string basePath, std::string programName, std::string extension) { + return std::filesystem::exists(fullConfigPath(basePath, programName, extension)); + } + std::optional getHome() { static const auto homeDir = getenv("HOME"); @@ -45,32 +53,36 @@ namespace Hyprutils::Path { using T = std::optional; std::pair findConfig(std::string programName) { + return findConfig(programName, "conf"); + } + + std::pair findConfig(std::string programName, std::string extension) { bool xdgConfigHomeExists = false; static const auto xdgConfigHome = getXdgConfigHome(); if (xdgConfigHome.has_value()) { xdgConfigHomeExists = true; - if (checkConfigExists(xdgConfigHome.value(), programName)) - return std::make_pair(fullConfigPath(xdgConfigHome.value(), programName), xdgConfigHome); + if (checkConfigExists(xdgConfigHome.value(), programName, extension)) + return std::make_pair(fullConfigPath(xdgConfigHome.value(), programName, extension), xdgConfigHome); } bool homeExists = false; static const auto home = getHome(); if (home.has_value()) { homeExists = true; - if (checkConfigExists(home.value(), programName)) - return std::make_pair(fullConfigPath(home.value(), programName), home); + if (checkConfigExists(home.value(), programName, extension)) + return std::make_pair(fullConfigPath(home.value(), programName, extension), home); } static const auto xdgConfigDirs = getXdgConfigDirs(); if (xdgConfigDirs.has_value()) { for (auto& dir : xdgConfigDirs.value()) { - if (checkConfigExists(dir, programName)) - return std::make_pair(fullConfigPath(dir, programName), std::nullopt); + if (checkConfigExists(dir, programName, extension)) + return std::make_pair(fullConfigPath(dir, programName, extension), std::nullopt); } } - if (checkConfigExists("/etc/xdg", programName)) - return std::make_pair(fullConfigPath("/etc/xdg", programName), std::nullopt); + if (checkConfigExists("/etc/xdg", programName, extension)) + return std::make_pair(fullConfigPath("/etc/xdg", programName, extension), std::nullopt); if (xdgConfigHomeExists) return std::make_pair(std::nullopt, xdgConfigHome);