From 1210de188c8ee5bbaf8c16b43a8a7a1ffaa4a18a Mon Sep 17 00:00:00 2001 From: vaxerski Date: Sun, 31 Dec 2023 01:59:37 +0100 Subject: [PATCH] configOptions: add allowMissingConfig --- include/hyprlang.hpp | 6 ++++++ src/config.cpp | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/hyprlang.hpp b/include/hyprlang.hpp index 8dd7c4c..e9be3f0 100644 --- a/include/hyprlang.hpp +++ b/include/hyprlang.hpp @@ -69,6 +69,12 @@ namespace Hyprlang { Return all errors instead of just the first */ bool throwAllErrors = false; + + /*! + @since 0.2.0 + Don't throw on a missing config file. Carry on as if nothing happened. + */ + bool allowMissingConfig = false; }; /*! diff --git a/src/config.cpp b/src/config.cpp index 4665ac2..bada693 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -32,10 +32,17 @@ CConfig::CConfig(const char* path, const Hyprlang::SConfigOptions& options) { impl = new CConfigImpl; try { impl->path = std::filesystem::canonical(path); - } catch (std::exception& e) { throw "Couldn't open file. File does not exist"; } + } catch (std::exception& e) { + if (!options.allowMissingConfig) + throw "Couldn't open file. File does not exist"; + } - if (!std::filesystem::exists(impl->path)) - throw "File does not exist"; + if (!std::filesystem::exists(impl->path)) { + if (!options.allowMissingConfig) + throw "File does not exist"; + + impl->path = ""; + } impl->envVariables.clear(); for (char** env = environ; *env; ++env) { @@ -496,6 +503,10 @@ CParseResult CConfig::parse() { applyDefaultsToCat(*sc); } + // implies options.allowMissingConfig + if (impl->path.empty()) + return CParseResult{}; + CParseResult fileParseResult = parseFile(impl->path); return fileParseResult;