From 41811642177c2159e9ce572218def5b1525e71c2 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Sun, 28 Apr 2024 23:17:53 +0530 Subject: [PATCH] SimpCfg:Implement set_int64 and set_double Also update the sample simpcfg file, to test for int and float values. --- common/simpcfg.hpp | 28 +++++++++++++++++++++++++--- examples/chaton_meta.simpcfg | 7 +++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/common/simpcfg.hpp b/common/simpcfg.hpp index abecf1352..587b9dbb0 100644 --- a/common/simpcfg.hpp +++ b/common/simpcfg.hpp @@ -63,7 +63,7 @@ class SimpCfg { private: std::map> mapStrings = {}; std::map> mapBools = {}; - std::map> mapInts = {}; + std::map> mapInt64s = {}; std::map> mapDoubles = {}; std::regex rInt {R"(^[-+]?\d+$)"}; std::regex rFloat {R"(^[-+]?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$)"}; @@ -80,6 +80,28 @@ public: LDBUG_LN("DBUG:SC:%s:%s:%s:%d", __func__, group.c_str(), key.c_str(), value); } + void set_int64(const std::string &group, const std::string &key, int64_t value) { + auto &gm = mapInt64s[group]; + gm[key] = value; + LDBUG_LN("DBUG:SC:%s:%s:%s:%lld", __func__, group.c_str(), key.c_str(), value); + } + + void set_int64(const std::string &group, const std::string &key, std::string &value) { + auto ivalue = strtoll(value.c_str(), nullptr, 0); + set_int64(group, key, ivalue); + } + + void set_double(const std::string &group, const std::string &key, double value) { + auto &gm = mapDoubles[group]; + gm[key] = value; + LDBUG_LN("DBUG:SC:%s:%s:%s:%f", __func__, group.c_str(), key.c_str(), value); + } + + void set_double(const std::string &group, const std::string &key, std::string &value) { + auto dvalue = strtod(value.c_str(), nullptr); + set_double(group, key, dvalue); + } + std::string get_string(const std::string &group, const std::string &key, const std::string &defaultValue) { auto gm = mapStrings[group]; #ifdef SC_DEBUG @@ -161,10 +183,10 @@ public: set_bool(group, key, value == "true" ? true : false); } else if (std::regex_match(value, rInt)) { vtype = "int"; - set_int(group, key, value); + set_int64(group, key, value); } else if (std::regex_match(value, rFloat)) { vtype = "float"; - set_float(group, key, value); + set_double(group, key, value); } else { vtype = "string"; value = str_trim_single(value, "\""); diff --git a/examples/chaton_meta.simpcfg b/examples/chaton_meta.simpcfg index 2a209fd21..1a3b2dcaa 100644 --- a/examples/chaton_meta.simpcfg +++ b/examples/chaton_meta.simpcfg @@ -223,3 +223,10 @@ "systemuser-1st-user-has-begin": false, "systemuser-1st-user-has-prefix": false +"testme" + "int": 1234 + "sint": -9876543210 + "float": 12.34 + "double1": +12.0e-123 + "double2": -12.0e-123 +