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,27 +45,35 @@ class Reader : public BasicParser {
if (is_digit(c)) {
number_u num;
parseNumber(1, num);
handler.setValue(name, *dynamic::Value::of(num));
if (handler.has(name)) {
handler.setValue(name, *dynamic::Value::of(num));
}
} else if (c == '-' || c == '+') {
int sign = c == '-' ? -1 : 1;
pos++;
number_u num;
parseNumber(sign, num);
handler.setValue(name, *dynamic::Value::of(num));
if (handler.has(name)) {
handler.setValue(name, *dynamic::Value::of(num));
}
} else if (is_identifier_start(c)) {
std::string identifier = parseName();
if (identifier == "true" || identifier == "false") {
bool flag = identifier == "true";
handler.setValue(name, *dynamic::Value::boolean(flag));
} else if (identifier == "inf") {
handler.setValue(name, *dynamic::Value::of(INFINITY));
} else if (identifier == "nan") {
handler.setValue(name, *dynamic::Value::of(NAN));
if (handler.has(name)) {
if (identifier == "true" || identifier == "false") {
bool flag = identifier == "true";
handler.setValue(name, *dynamic::Value::boolean(flag));
} else if (identifier == "inf") {
handler.setValue(name, *dynamic::Value::of(INFINITY));
} else if (identifier == "nan") {
handler.setValue(name, *dynamic::Value::of(NAN));
}
}
} else if (c == '"' || c == '\'') {
pos++;
std::string str = parseString(c);
handler.setValue(name, *dynamic::Value::of(str));
if (handler.has(name)) {
handler.setValue(name, *dynamic::Value::of(str));
}
} else {
throw error("feature is not supported");
}

View File

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

View File

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