From 689b405d9d9eca20053ac7bc094bf15388c28a63 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Mon, 12 Feb 2024 15:13:27 +0000 Subject: [PATCH] internal: fixes to config path handling --- src/config.cpp | 28 +++++++++++++++------------- src/config.hpp | 3 ++- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index c249f11..2c39e4e 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -30,19 +30,12 @@ static std::string removeBeginEndSpacesTabs(std::string str) { } CConfig::CConfig(const char* path, const Hyprlang::SConfigOptions& options) { - impl = new CConfigImpl; - try { - impl->path = std::filesystem::canonical(path); - } catch (std::exception& e) { - if (!options.allowMissingConfig) - throw "Couldn't open file. File does not exist"; - } + impl = new CConfigImpl; + impl->path = path; if (!std::filesystem::exists(impl->path)) { if (!options.allowMissingConfig) throw "File does not exist"; - - impl->path = ""; } impl->envVariables.clear(); @@ -543,11 +536,20 @@ CParseResult CConfig::parse() { applyDefaultsToCat(*sc); } - // implies options.allowMissingConfig - if (impl->path.empty()) - return CParseResult{}; + bool fileExists = std::filesystem::exists(impl->path); - CParseResult fileParseResult = parseFile(impl->path.c_str()); + // implies options.allowMissingConfig + if (impl->configOptions.allowMissingConfig && !fileExists) + return CParseResult{}; + else if (!fileExists) { + CParseResult res; + res.setError("Config file is missing"); + return res; + } + + std::string canonical = std::filesystem::canonical(impl->path); + + CParseResult fileParseResult = parseFile(canonical.c_str()); return fileParseResult; } diff --git a/src/config.hpp b/src/config.hpp index d5feaac..81d9375 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -55,7 +55,8 @@ struct SSpecialCategory { class CConfigImpl { public: - std::string path = ""; + std::string path = ""; + std::string originalPath = ""; std::unordered_map values; std::unordered_map defaultValues;