settings reader fix

This commit is contained in:
MihailRis 2024-05-05 16:04:00 +03:00
parent ba885e4e08
commit 179d2b7a25
3 changed files with 23 additions and 10 deletions

View File

@ -45,15 +45,20 @@ class Reader : public BasicParser {
if (is_digit(c)) { if (is_digit(c)) {
number_u num; number_u num;
parseNumber(1, num); parseNumber(1, num);
if (handler.has(name)) {
handler.setValue(name, *dynamic::Value::of(num)); handler.setValue(name, *dynamic::Value::of(num));
}
} else if (c == '-' || c == '+') { } else if (c == '-' || c == '+') {
int sign = c == '-' ? -1 : 1; int sign = c == '-' ? -1 : 1;
pos++; pos++;
number_u num; number_u num;
parseNumber(sign, num); parseNumber(sign, num);
if (handler.has(name)) {
handler.setValue(name, *dynamic::Value::of(num)); handler.setValue(name, *dynamic::Value::of(num));
}
} else if (is_identifier_start(c)) { } else if (is_identifier_start(c)) {
std::string identifier = parseName(); std::string identifier = parseName();
if (handler.has(name)) {
if (identifier == "true" || identifier == "false") { if (identifier == "true" || identifier == "false") {
bool flag = identifier == "true"; bool flag = identifier == "true";
handler.setValue(name, *dynamic::Value::boolean(flag)); handler.setValue(name, *dynamic::Value::boolean(flag));
@ -62,10 +67,13 @@ class Reader : public BasicParser {
} else if (identifier == "nan") { } else if (identifier == "nan") {
handler.setValue(name, *dynamic::Value::of(NAN)); handler.setValue(name, *dynamic::Value::of(NAN));
} }
}
} else if (c == '"' || c == '\'') { } else if (c == '"' || c == '\'') {
pos++; pos++;
std::string str = parseString(c); std::string str = parseString(c);
if (handler.has(name)) {
handler.setValue(name, *dynamic::Value::of(str)); handler.setValue(name, *dynamic::Value::of(str));
}
} else { } else {
throw error("feature is not supported"); throw error("feature is not supported");
} }

View File

@ -113,6 +113,10 @@ Setting* SettingsHandler::getSetting(const std::string& name) const {
return found->second; return found->second;
} }
bool SettingsHandler::has(const std::string& name) const {
return map.find(name) != map.end();
}
template<class T> template<class T>
static void set_numeric_value(T* setting, const dynamic::Value& value) { static void set_numeric_value(T* setting, const dynamic::Value& value) {
switch (value.type) { switch (value.type) {

View File

@ -26,6 +26,7 @@ public:
void setValue(const std::string& name, const dynamic::Value& value); void setValue(const std::string& name, const dynamic::Value& value);
std::string toString(const std::string& name) const; std::string toString(const std::string& name) const;
Setting* getSetting(const std::string& name) const; Setting* getSetting(const std::string& name) const;
bool has(const std::string& name) const;
std::vector<Section>& getSections(); std::vector<Section>& getSections();
}; };