From 179d2b7a25ea66a66d1b9ad7b1a8a5fd1a37734b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 5 May 2024 16:04:00 +0300 Subject: [PATCH] settings reader fix --- src/coders/toml.cpp | 28 ++++++++++++++++++---------- src/files/settings_io.cpp | 4 ++++ src/files/settings_io.hpp | 1 + 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/coders/toml.cpp b/src/coders/toml.cpp index 89f2e91a..3902cec0 100644 --- a/src/coders/toml.cpp +++ b/src/coders/toml.cpp @@ -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"); } diff --git a/src/files/settings_io.cpp b/src/files/settings_io.cpp index 44932aff..6bd03c24 100644 --- a/src/files/settings_io.cpp +++ b/src/files/settings_io.cpp @@ -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 static void set_numeric_value(T* setting, const dynamic::Value& value) { switch (value.type) { diff --git a/src/files/settings_io.hpp b/src/files/settings_io.hpp index 9a04d065..7630028c 100644 --- a/src/files/settings_io.hpp +++ b/src/files/settings_io.hpp @@ -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
& getSections(); };