API: add pathIsStream to SConfigOptions

This commit is contained in:
Vaxry 2024-03-07 18:14:02 +00:00
parent f1db1a7e1f
commit 66c099e097
3 changed files with 74 additions and 17 deletions

View file

@ -107,6 +107,13 @@ namespace Hyprlang {
Don't throw on a missing config file. Carry on as if nothing happened. Don't throw on a missing config file. Carry on as if nothing happened.
*/ */
bool allowMissingConfig = false; bool allowMissingConfig = false;
/*!
\since 0.4.2
Treat configPath as a raw config stream.
*/
bool pathIsStream = false;
}; };
/*! /*!
@ -429,6 +436,7 @@ namespace Hyprlang {
void clearState(); void clearState();
void applyDefaultsToCat(SSpecialCategory& cat); void applyDefaultsToCat(SSpecialCategory& cat);
void retrieveKeysForCat(const char* category, const char*** out, size_t* len); void retrieveKeysForCat(const char* category, const char*** out, size_t* len);
CParseResult parseRawStream(const std::string& stream);
}; };
}; };
#endif #endif

View file

@ -6,6 +6,7 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <expected> #include <expected>
#include <sstream>
using namespace Hyprlang; using namespace Hyprlang;
extern "C" char** environ; extern "C" char** environ;
@ -35,9 +36,13 @@ static std::string removeBeginEndSpacesTabs(std::string str) {
CConfig::CConfig(const char* path, const Hyprlang::SConfigOptions& options) { CConfig::CConfig(const char* path, const Hyprlang::SConfigOptions& options) {
impl = new CConfigImpl; impl = new CConfigImpl;
if (options.pathIsStream)
impl->rawConfigString = path;
else
impl->path = path; impl->path = path;
if (!std::filesystem::exists(impl->path)) { if (!options.pathIsStream && !std::filesystem::exists(impl->path)) {
if (!options.allowMissingConfig) if (!options.allowMissingConfig)
throw "File does not exist"; throw "File does not exist";
} }
@ -593,6 +598,9 @@ CParseResult CConfig::parse() {
applyDefaultsToCat(*sc); applyDefaultsToCat(*sc);
} }
CParseResult fileParseResult;
if (impl->rawConfigString.empty()) {
bool fileExists = std::filesystem::exists(impl->path); bool fileExists = std::filesystem::exists(impl->path);
// implies options.allowMissingConfig // implies options.allowMissingConfig
@ -606,11 +614,49 @@ CParseResult CConfig::parse() {
std::string canonical = std::filesystem::canonical(impl->path); std::string canonical = std::filesystem::canonical(impl->path);
CParseResult fileParseResult = parseFile(canonical.c_str()); fileParseResult = parseFile(canonical.c_str());
} else {
fileParseResult = parseRawStream(impl->rawConfigString);
}
return fileParseResult; return fileParseResult;
} }
CParseResult CConfig::parseRawStream(const std::string& stream) {
CParseResult result;
std::string line = "";
int linenum = 1;
std::stringstream str(stream);
while (std::getline(str, line)) {
const auto RET = parseLine(line);
if (RET.error && (impl->parseError.empty() || impl->configOptions.throwAllErrors)) {
if (!impl->parseError.empty())
impl->parseError += "\n";
impl->parseError += std::format("Config error at line {}: {}", linenum, RET.errorStdString);
result.setError(impl->parseError);
}
++linenum;
}
if (!impl->categories.empty()) {
if (impl->parseError.empty() || impl->configOptions.throwAllErrors) {
if (!impl->parseError.empty())
impl->parseError += "\n";
impl->parseError += std::format("Config error: Unclosed category at EOF");
result.setError(impl->parseError);
}
impl->categories.clear();
}
return result;
}
CParseResult CConfig::parseFile(const char* file) { CParseResult CConfig::parseFile(const char* file) {
CParseResult result; CParseResult result;

View file

@ -69,6 +69,9 @@ class CConfigImpl {
std::string path = ""; std::string path = "";
std::string originalPath = ""; std::string originalPath = "";
// if not-empty, used instead of path
std::string rawConfigString = "";
std::unordered_map<std::string, Hyprlang::CConfigValue> values; std::unordered_map<std::string, Hyprlang::CConfigValue> values;
std::unordered_map<std::string, SConfigDefaultValue> defaultValues; std::unordered_map<std::string, SConfigDefaultValue> defaultValues;
std::vector<SHandler> handlers; std::vector<SHandler> handlers;