Skip to content

Commit

Permalink
config: streamlined absolutePath impl
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricekraus committed Feb 21, 2024
1 parent c3198d3 commit e59d8ae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
26 changes: 14 additions & 12 deletions src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ static Hyprlang::CParseResult handleSource(const char* c, const char* v) {
return result;
}


static std::string getConfigDir() {
static const char* xdgConfigHome = getenv("XDG_CONFIG_HOME");

Expand Down Expand Up @@ -151,7 +150,6 @@ std::vector<CConfigManager::SWidgetConfig> CConfigManager::getWidgetConfigs() {
return result;
}


std::optional<std::string> CConfigManager::handleSource(const std::string& command, const std::string& rawpath) {
if (rawpath.length() < 2) {
Debug::log(ERR, "source= path garbage");
Expand All @@ -160,34 +158,38 @@ std::optional<std::string> CConfigManager::handleSource(const std::string& comma
std::unique_ptr<glob_t, void (*)(glob_t*)> glob_buf{new glob_t, [](glob_t* g) { globfree(g); }};
memset(glob_buf.get(), 0, sizeof(glob_t));

if (auto r = glob(absolutePath(rawpath, configCurrentPath).c_str(), GLOB_TILDE, nullptr, glob_buf.get()); r != 0) {
if (auto r = glob(absolutePath(rawpath, configCurrentPath)->c_str(), GLOB_TILDE, nullptr, glob_buf.get()); r != 0) {
std::string err = std::format("source= globbing error: {}", r == GLOB_NOMATCH ? "found no match" : GLOB_ABORTED ? "read error" : "out of memory");
Debug::log(ERR, "{}", err);
return err;
}

for (size_t i = 0; i < glob_buf->gl_pathc; i++) {
auto value = absolutePath(glob_buf->gl_pathv[i], configCurrentPath);
auto pathValueOpt = absolutePath(glob_buf->gl_pathv[i], configCurrentPath);
if (pathValueOpt->empty()) {
Debug::log(WARN, "source= skipping invalid path");
continue;
}

if (!std::filesystem::is_regular_file(value)) {
if (std::filesystem::exists(value)) {
Debug::log(WARN, "source= skipping non-file {}", value);
auto pathValue = pathValueOpt.value();

if (!std::filesystem::is_regular_file(pathValue)) {
if (std::filesystem::exists(pathValue)) {
Debug::log(WARN, "source= skipping non-file {}", pathValue);
continue;
}

Debug::log(ERR, "source= file doesnt exist");
return "source file " + value + " doesn't exist!";
return "source file " + pathValue + " doesn't exist!";
}

// allow for nested config parsing
auto backupConfigPath = configCurrentPath;
configCurrentPath = value;
configCurrentPath = pathValue;

m_config.parseFile(value.c_str());
m_config.parseFile(pathValue.c_str());

configCurrentPath = backupConfigPath;


}

return {};
Expand Down
5 changes: 3 additions & 2 deletions src/config/ConfigManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <unordered_map>

class CConfigManager {
public:
public:
CConfigManager();
void init();
void* const* getValuePtr(const std::string& name);
Expand All @@ -25,7 +25,8 @@ class CConfigManager {
std::optional<std::string> handleSource(const std::string&, const std::string&);

std::string configCurrentPath;
private:

private:
Hyprlang::CConfig m_config;
};

Expand Down
34 changes: 16 additions & 18 deletions src/helpers/MiscFunctions.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
#include <filesystem>
#include "MiscFunctions.hpp"

std::string absolutePath(const std::string& rawpath, const std::string& currentPath) {
auto value = rawpath;
std::optional<std::string> absolutePath(const std::string& rawpath, const std::string& rawcurrentpath) {
std::filesystem::path path(rawpath);

if (value[0] == '~') {
// Handling where rawpath starts with '~'
if (!rawpath.empty() && rawpath[0] == '~') {
static const char* const ENVHOME = getenv("HOME");
value.replace(0, 1, std::string(ENVHOME));
} else if (value[0] != '/') {
auto currentDir = currentPath.substr(0, currentPath.find_last_of('/'));

if (value[0] == '.') {
if (value[1] == '.' && value[2] == '/') {
auto parentDir = currentDir.substr(0, currentDir.find_last_of('/'));
value.replace(0, 2 + currentPath.empty(), parentDir);
} else if (value[1] == '/')
value.replace(0, 1 + currentPath.empty(), currentDir);
else
value = currentDir + '/' + value;
} else
value = currentDir + '/' + value;
return std::filesystem::path(ENVHOME) / path.relative_path().string().substr(2);
}
// Handling e.g. ./, ../
else if (path.is_relative()) {
const std::filesystem::path currentDir = std::filesystem::path(rawcurrentpath).parent_path();

return value;
auto finalPath = currentDir / path;
if (exists(finalPath))
return std::filesystem::canonical(currentDir / path);
return {};
} else {
return std::filesystem::canonical(path);
}
}
4 changes: 2 additions & 2 deletions src/helpers/MiscFunctions.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <string>
#include <optional>

std::string absolutePath(const std::string&, const std::string&);

std::optional<std::string> absolutePath(const std::string&, const std::string&);

0 comments on commit e59d8ae

Please sign in to comment.