SimpCfg: Convert to a class

This commit is contained in:
HanishKVC 2024-04-28 13:32:00 +05:30
parent 28ae0c5b02
commit 1ecca5a7ec

View file

@ -21,28 +21,6 @@
#include "log.h"
#endif
std::map<std::string, std::map<std::string, std::string>> mapStrings = {};
std::map<std::string, std::map<std::string, bool>> mapBools = {};
void sc_set_string(std::string &group, std::string &key, std::string &value) {
auto gm = mapStrings[group];
gm[key] = value;
}
void sc_set_bool(std::string &group, std::string &key, bool value) {
auto gm = mapBools[group];
gm[key] = value;
}
std::string sc_get_string(std::string &group, std::string &key) {
auto gm = mapStrings[group];
return gm[key];
}
bool sc_get_bool(std::string &group, std::string &key) {
auto gm = mapBools[group];
return gm[key];
}
std::string str_trim(std::string sin, std::string trimChars=" \t\n") {
sin.erase(sin.find_last_not_of(trimChars)+1);
@ -50,64 +28,93 @@ std::string str_trim(std::string sin, std::string trimChars=" \t\n") {
return sin;
}
void sc_load(std::string &fname) {
std::ifstream f {fname};
if (!f) {
LOG_TEELN("ERRR:%s:%s:failed to load...", __func__, fname.c_str());
throw std::runtime_error { "ERRR:SimpCfg:File not found" };
} else {
LOG_TEELN("DBUG:%s:%s", __func__, fname.c_str());
class SimpCfg {
private:
std::map<std::string, std::map<std::string, std::string>> mapStrings = {};
std::map<std::string, std::map<std::string, bool>> mapBools = {};
public:
void sc_set_string(std::string &group, std::string &key, std::string &value) {
auto gm = mapStrings[group];
gm[key] = value;
}
std::string group;
int iLine = 0;
while(!f.eof()) {
iLine += 1;
std::string curL;
getline(f, curL);
if (curL.empty()) {
continue;
}
if (curL[0] == '#') {
continue;
}
bool bGroup = !isspace(curL[0]);
curL = str_trim(curL);
if (bGroup) {
group = curL;
LOG_TEELN("DBUG:%s:group:%s", __func__, group.c_str());
continue;
}
auto dPos = curL.find(':');
if (dPos == std::string::npos) {
LOG_TEELN("ERRR:%s:%d:invalid key value line:%s", __func__, iLine, curL.c_str());
throw std::runtime_error { "ERRR:SimpCfg:Invalid key value line" };
}
auto dEnd = curL.length() - dPos;
if ((dPos == 0) || (dEnd < 2)) {
LOG_TEELN("ERRR:%s:%d:invalid key value line:%s", __func__, iLine, curL.c_str());
throw std::runtime_error { "ERRR:SimpCfg:Invalid key value line" };
}
std::string key = curL.substr(0, dPos);
key = str_trim(key);
std::string value = curL.substr(dPos+1);
value = str_trim(value);
value = str_trim(value, ",");
std::string vtype = "bool";
if ((value == "true") || (value == "false")) {
sc_set_bool(group, key, value == "true" ? true : false);
void sc_set_bool(std::string &group, std::string &key, bool value) {
auto gm = mapBools[group];
gm[key] = value;
}
std::string sc_get_string(std::string &group, std::string &key) {
auto gm = mapStrings[group];
return gm[key];
}
bool sc_get_bool(std::string &group, std::string &key) {
auto gm = mapBools[group];
return gm[key];
}
void sc_load(std::string &fname) {
std::ifstream f {fname};
if (!f) {
LOG_TEELN("ERRR:%s:%s:failed to load...", __func__, fname.c_str());
throw std::runtime_error { "ERRR:SimpCfg:File not found" };
} else {
vtype = "string";
sc_set_string(group, key, value);
LOG_TEELN("DBUG:%s:%s", __func__, fname.c_str());
}
std::string group;
int iLine = 0;
while(!f.eof()) {
iLine += 1;
std::string curL;
getline(f, curL);
if (curL.empty()) {
continue;
}
if (curL[0] == '#') {
continue;
}
bool bGroup = !isspace(curL[0]);
curL = str_trim(curL);
if (bGroup) {
group = curL;
LOG_TEELN("DBUG:%s:group:%s", __func__, group.c_str());
continue;
}
auto dPos = curL.find(':');
if (dPos == std::string::npos) {
LOG_TEELN("ERRR:%s:%d:invalid key value line:%s", __func__, iLine, curL.c_str());
throw std::runtime_error { "ERRR:SimpCfg:Invalid key value line" };
}
auto dEnd = curL.length() - dPos;
if ((dPos == 0) || (dEnd < 2)) {
LOG_TEELN("ERRR:%s:%d:invalid key value line:%s", __func__, iLine, curL.c_str());
throw std::runtime_error { "ERRR:SimpCfg:Invalid key value line" };
}
std::string key = curL.substr(0, dPos);
key = str_trim(key);
std::string value = curL.substr(dPos+1);
value = str_trim(value);
value = str_trim(value, ",");
std::string vtype = "bool";
if ((value == "true") || (value == "false")) {
sc_set_bool(group, key, value == "true" ? true : false);
} else {
vtype = "string";
sc_set_string(group, key, value);
}
LOG_TEELN("DBUG:%s:kv:%s:%s:%s:%s", __func__, group.c_str(), key.c_str(), vtype.c_str(), value.c_str());
}
LOG_TEELN("DBUG:%s:kv:%s:%s:%s:%s", __func__, group.c_str(), key.c_str(), vtype.c_str(), value.c_str());
}
}
};
#ifdef TEST_LOGIC
int main(int argc, char **argv) {
std::string fname {argv[1]};
sc_load(fname);
SimpCfg sc;
sc.sc_load(fname);
return 0;
}
#endif