SimpCfg: Convert to a class
This commit is contained in:
parent
28ae0c5b02
commit
1ecca5a7ec
1 changed files with 78 additions and 71 deletions
|
@ -21,28 +21,6 @@
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#endif
|
#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") {
|
std::string str_trim(std::string sin, std::string trimChars=" \t\n") {
|
||||||
sin.erase(sin.find_last_not_of(trimChars)+1);
|
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;
|
return sin;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sc_load(std::string &fname) {
|
|
||||||
std::ifstream f {fname};
|
class SimpCfg {
|
||||||
if (!f) {
|
private:
|
||||||
LOG_TEELN("ERRR:%s:%s:failed to load...", __func__, fname.c_str());
|
std::map<std::string, std::map<std::string, std::string>> mapStrings = {};
|
||||||
throw std::runtime_error { "ERRR:SimpCfg:File not found" };
|
std::map<std::string, std::map<std::string, bool>> mapBools = {};
|
||||||
} else {
|
public:
|
||||||
LOG_TEELN("DBUG:%s:%s", __func__, fname.c_str());
|
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;
|
void sc_set_bool(std::string &group, std::string &key, bool value) {
|
||||||
while(!f.eof()) {
|
auto gm = mapBools[group];
|
||||||
iLine += 1;
|
gm[key] = value;
|
||||||
std::string curL;
|
}
|
||||||
getline(f, curL);
|
|
||||||
if (curL.empty()) {
|
std::string sc_get_string(std::string &group, std::string &key) {
|
||||||
continue;
|
auto gm = mapStrings[group];
|
||||||
}
|
return gm[key];
|
||||||
if (curL[0] == '#') {
|
}
|
||||||
continue;
|
|
||||||
}
|
bool sc_get_bool(std::string &group, std::string &key) {
|
||||||
bool bGroup = !isspace(curL[0]);
|
auto gm = mapBools[group];
|
||||||
curL = str_trim(curL);
|
return gm[key];
|
||||||
if (bGroup) {
|
}
|
||||||
group = curL;
|
|
||||||
LOG_TEELN("DBUG:%s:group:%s", __func__, group.c_str());
|
void sc_load(std::string &fname) {
|
||||||
continue;
|
std::ifstream f {fname};
|
||||||
}
|
if (!f) {
|
||||||
auto dPos = curL.find(':');
|
LOG_TEELN("ERRR:%s:%s:failed to load...", __func__, fname.c_str());
|
||||||
if (dPos == std::string::npos) {
|
throw std::runtime_error { "ERRR:SimpCfg:File not found" };
|
||||||
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 {
|
} else {
|
||||||
vtype = "string";
|
LOG_TEELN("DBUG:%s:%s", __func__, fname.c_str());
|
||||||
sc_set_string(group, 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);
|
||||||
|
} 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
|
#ifdef TEST_LOGIC
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
std::string fname {argv[1]};
|
std::string fname {argv[1]};
|
||||||
sc_load(fname);
|
SimpCfg sc;
|
||||||
|
sc.sc_load(fname);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue