14
14
#include < vector>
15
15
#include < fstream>
16
16
17
+ #define TEST_LOGIC
18
+ #ifdef TEST_LOGIC
19
+ #define LOG_TEELN (FMT, ...) printf(FMT, __VA_ARGS__)
20
+ #else
17
21
#include " log.h"
18
-
22
+ # endif
19
23
20
24
std::map<std::string, std::map<std::string, std::string>> mapStrings = {};
21
25
std::map<std::string, std::map<std::string, bool >> mapBools = {};
@@ -40,23 +44,63 @@ bool sc_get_bool(std::string &group, std::string &key) {
40
44
return gm[key];
41
45
}
42
46
43
- std::string str_trim (std::string &sin) {
44
- std::string sout = sin;
45
- sout.erase (sout.find_last );
47
+ std::string str_trim (std::string sin) {
48
+ sin.erase (sin.find_last_not_of (" \t\n " )+1 );
49
+ sin.erase (0 , sin.find_first_not_of (" \t\n " ));
50
+ return sin;
46
51
}
47
52
48
53
bool sc_load (std::string &fname) {
49
54
std::ifstream f {fname};
50
55
if (!f) {
51
56
LOG_TEELN (" ERRR:%s:%s:failed to load..." , __func__, fname);
52
- exit (2 );
57
+ throw std::runtime_error { " ERRR:SimpCfg:File not found" };
58
+ } else {
59
+ LOG_TEELN (" DBUG:%s:%s" , __func__, fname);
53
60
}
61
+ std::string group;
62
+ int iLine = 0 ;
54
63
while (!f.eof ()) {
55
- std::string curl;
56
- getline (f, curl);
57
- if (curl.empty ()) {
64
+ iLine += 1 ;
65
+ std::string curL;
66
+ getline (f, curL);
67
+ if (curL.empty ()) {
58
68
continue ;
59
69
}
60
-
70
+ if (curL[0 ] == ' #' ) {
71
+ continue ;
72
+ }
73
+ bool bGroup = !isspace (curL[0 ]);
74
+ curL = str_trim (curL);
75
+ if (bGroup) {
76
+ group = curL;
77
+ LOG_TEELN (" DBUG:%s:%s:%s:%s" , __func__, group);
78
+ continue ;
79
+ }
80
+ auto dPos = curL.find (' :' );
81
+ if (dPos == std::string::npos) {
82
+ LOG_TEELN (" ERRR:%s:%d:invalid key value line:%s" , __func__, iLine, curL);
83
+ throw std::runtime_error { " ERRR:SimpCfg:Invalid key value line" };
84
+ }
85
+ auto dEnd = curL.length () - dPos;
86
+ if ((dPos == 0 ) || (dEnd < 2 )) {
87
+ LOG_TEELN (" ERRR:%s:%d:invalid key value line:%s" , __func__, iLine, curL);
88
+ throw std::runtime_error { " ERRR:SimpCfg:Invalid key value line" };
89
+ }
90
+ std::string key = curL.substr (0 , dPos);
91
+ key = str_trim (key);
92
+ std::string value = curL.substr (dPos+1 );
93
+ value = str_trim (value);
94
+ LOG_TEELN (" DBUG:%s:%s:%s:%s" , __func__, group, key, value);
95
+ sc_set_string (group, key, value);
61
96
}
62
97
}
98
+
99
+
100
+ #ifdef TEST_LOGIC
101
+ int main (int argc, char **argv) {
102
+ std::string fname {argv[1 ]};
103
+ sc_load (fname);
104
+ return 0 ;
105
+ }
106
+ #endif
0 commit comments