SimpCfg:Implement set_int64 and set_double

Also update the sample simpcfg file, to test for int and float
values.
This commit is contained in:
HanishKVC 2024-04-28 23:17:53 +05:30
parent fb9a7dc7fe
commit 4181164217
2 changed files with 32 additions and 3 deletions

View file

@ -63,7 +63,7 @@ class SimpCfg {
private:
std::map<std::string, std::map<std::string, std::string>> mapStrings = {};
std::map<std::string, std::map<std::string, bool>> mapBools = {};
std::map<std::string, std::map<std::string, int>> mapInts = {};
std::map<std::string, std::map<std::string, int64_t>> mapInt64s = {};
std::map<std::string, std::map<std::string, double>> 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, "\"");

View file

@ -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