mirror of
https://github.com/hyprwm/hyprlang.git
synced 2024-12-22 09:59:48 +01:00
core: add parseFile
This commit is contained in:
parent
cc05e782b3
commit
710bf1276a
4 changed files with 45 additions and 12 deletions
|
@ -474,14 +474,22 @@ CParseResult CConfig::parse() {
|
||||||
sc->applyDefaults();
|
sc->applyDefaults();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ifstream iffile(impl->path);
|
CParseResult fileParseResult = parseFile(impl->path);
|
||||||
if (!iffile.good())
|
|
||||||
throw "Config file failed to open";
|
|
||||||
|
|
||||||
std::string line = "";
|
return fileParseResult;
|
||||||
int linenum = 1;
|
}
|
||||||
|
|
||||||
CParseResult fileParseResult;
|
CParseResult CConfig::parseFile(std::string file) {
|
||||||
|
CParseResult result;
|
||||||
|
|
||||||
|
std::ifstream iffile(file);
|
||||||
|
if (!iffile.good()) {
|
||||||
|
result.setError("File failed to open");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string line = "";
|
||||||
|
int linenum = 1;
|
||||||
|
|
||||||
while (std::getline(iffile, line)) {
|
while (std::getline(iffile, line)) {
|
||||||
|
|
||||||
|
@ -489,7 +497,7 @@ CParseResult CConfig::parse() {
|
||||||
|
|
||||||
if (RET.error && impl->parseError.empty()) {
|
if (RET.error && impl->parseError.empty()) {
|
||||||
impl->parseError = RET.getError();
|
impl->parseError = RET.getError();
|
||||||
fileParseResult.setError(std::format("Config error at line {}: {}", linenum, RET.errorStdString));
|
result.setError(std::format("Config error in file {} at line {}: {}", file, linenum, RET.errorStdString));
|
||||||
}
|
}
|
||||||
|
|
||||||
++linenum;
|
++linenum;
|
||||||
|
@ -498,12 +506,11 @@ CParseResult CConfig::parse() {
|
||||||
iffile.close();
|
iffile.close();
|
||||||
|
|
||||||
if (!impl->categories.empty()) {
|
if (!impl->categories.empty()) {
|
||||||
fileParseResult.setError("Unclosed category at EOF");
|
result.setError("Unclosed category at EOF");
|
||||||
impl->categories.clear();
|
impl->categories.clear();
|
||||||
return fileParseResult;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileParseResult;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
CParseResult CConfig::parseDynamic(const char* line) {
|
CParseResult CConfig::parseDynamic(const char* line) {
|
||||||
|
|
|
@ -132,6 +132,10 @@ namespace Hyprlang {
|
||||||
/* Parse the config. Refresh the values. */
|
/* Parse the config. Refresh the values. */
|
||||||
CParseResult parse();
|
CParseResult parse();
|
||||||
|
|
||||||
|
/* Same as parse(), but parse a specific file, without any refreshing.
|
||||||
|
recommended to use for stuff like source = path.conf */
|
||||||
|
CParseResult parseFile(std::string file);
|
||||||
|
|
||||||
/* Parse a single "line", dynamically.
|
/* Parse a single "line", dynamically.
|
||||||
Values set by this are temporary and will be overwritten
|
Values set by this are temporary and will be overwritten
|
||||||
by default / config on the next parse() */
|
by default / config on the next parse() */
|
||||||
|
|
|
@ -11,6 +11,8 @@ testVar = $MY_VAR$MY_VAR_2
|
||||||
|
|
||||||
testEnv = $SHELL
|
testEnv = $SHELL
|
||||||
|
|
||||||
|
source = ./colors.conf
|
||||||
|
|
||||||
testCategory {
|
testCategory {
|
||||||
testValueInt = 123456
|
testValueInt = 123456
|
||||||
testValueHex = 0xF
|
testValueHex = 0xF
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
#include <hyprlang.hpp>
|
#include <hyprlang.hpp>
|
||||||
|
|
||||||
|
@ -21,8 +22,10 @@ namespace Colors {
|
||||||
}
|
}
|
||||||
|
|
||||||
// globals for testing
|
// globals for testing
|
||||||
bool barrelRoll = false;
|
bool barrelRoll = false;
|
||||||
std::string flagsFound = "";
|
std::string flagsFound = "";
|
||||||
|
Hyprlang::CConfig* pConfig = nullptr;
|
||||||
|
std::string currentPath = "";
|
||||||
|
|
||||||
static Hyprlang::CParseResult handleDoABarrelRoll(const char* COMMAND, const char* VALUE) {
|
static Hyprlang::CParseResult handleDoABarrelRoll(const char* COMMAND, const char* VALUE) {
|
||||||
if (std::string(VALUE) == "woohoo, some, params")
|
if (std::string(VALUE) == "woohoo, some, params")
|
||||||
|
@ -40,6 +43,11 @@ static Hyprlang::CParseResult handleFlagsTest(const char* COMMAND, const char* V
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Hyprlang::CParseResult handleSource(const char* COMMAND, const char* VALUE) {
|
||||||
|
std::string PATH = std::filesystem::canonical(currentPath + "/" + VALUE);
|
||||||
|
return pConfig->parseFile(PATH);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv, char** envp) {
|
int main(int argc, char** argv, char** envp) {
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
|
@ -47,6 +55,8 @@ int main(int argc, char** argv, char** envp) {
|
||||||
std::cout << "Starting test\n";
|
std::cout << "Starting test\n";
|
||||||
|
|
||||||
Hyprlang::CConfig config("./config/config.conf");
|
Hyprlang::CConfig config("./config/config.conf");
|
||||||
|
pConfig = &config;
|
||||||
|
currentPath = std::filesystem::canonical("./config/");
|
||||||
|
|
||||||
// setup config
|
// setup config
|
||||||
config.addConfigValue("testInt", 0L);
|
config.addConfigValue("testInt", 0L);
|
||||||
|
@ -64,9 +74,13 @@ int main(int argc, char** argv, char** envp) {
|
||||||
config.addConfigValue("testCategory:testColor1", 0L);
|
config.addConfigValue("testCategory:testColor1", 0L);
|
||||||
config.addConfigValue("testCategory:testColor2", 0L);
|
config.addConfigValue("testCategory:testColor2", 0L);
|
||||||
config.addConfigValue("testCategory:testColor3", 0L);
|
config.addConfigValue("testCategory:testColor3", 0L);
|
||||||
|
config.addConfigValue("myColors:pink", 0L);
|
||||||
|
config.addConfigValue("myColors:green", 0L);
|
||||||
|
config.addConfigValue("myColors:random", 0L);
|
||||||
|
|
||||||
config.registerHandler(&handleDoABarrelRoll, "doABarrelRoll", {false});
|
config.registerHandler(&handleDoABarrelRoll, "doABarrelRoll", {false});
|
||||||
config.registerHandler(&handleFlagsTest, "flags", {true});
|
config.registerHandler(&handleFlagsTest, "flags", {true});
|
||||||
|
config.registerHandler(&handleSource, "source", {false});
|
||||||
|
|
||||||
config.addSpecialCategory("special", {"key"});
|
config.addSpecialCategory("special", {"key"});
|
||||||
config.addSpecialConfigValue("special", "value", 0L);
|
config.addSpecialConfigValue("special", "value", 0L);
|
||||||
|
@ -136,6 +150,12 @@ int main(int argc, char** argv, char** envp) {
|
||||||
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("specialGeneric:one", "value")), 1);
|
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("specialGeneric:one", "value")), 1);
|
||||||
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("specialGeneric:two", "value")), 2);
|
EXPECT(std::any_cast<int64_t>(config.getSpecialConfigValue("specialGeneric:two", "value")), 2);
|
||||||
|
|
||||||
|
// test sourcing
|
||||||
|
std::cout << " → Testing sourcing\n";
|
||||||
|
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:pink")), 0xFFc800c8L);
|
||||||
|
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:green")), 0xFF14f014L);
|
||||||
|
EXPECT(std::any_cast<int64_t>(config.getConfigValue("myColors:random")), 0xFFFF1337L);
|
||||||
|
|
||||||
} catch (const char* e) {
|
} catch (const char* e) {
|
||||||
std::cout << Colors::RED << "Error: " << Colors::RESET << e << "\n";
|
std::cout << Colors::RED << "Error: " << Colors::RESET << e << "\n";
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Reference in a new issue