internal: fixes to config path handling

This commit is contained in:
Vaxry 2024-02-12 15:13:27 +00:00
parent dbe5835573
commit 689b405d9d
2 changed files with 17 additions and 14 deletions

View file

@ -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;
}

View file

@ -55,7 +55,8 @@ struct SSpecialCategory {
class CConfigImpl {
public:
std::string path = "";
std::string path = "";
std::string originalPath = "";
std::unordered_map<std::string, Hyprlang::CConfigValue> values;
std::unordered_map<std::string, SConfigDefaultValue> defaultValues;