Browse Source

allowing value override

Patrick Brosi 6 years ago
parent
commit
63fcb1d54e
2 changed files with 31 additions and 8 deletions
  1. 15 8
      ConfigFileParser.cpp
  2. 16 0
      ConfigFileParser.h

+ 15 - 8
ConfigFileParser.cpp

@@ -24,7 +24,7 @@ void ConfigFileParser::parse(const std::string& path) {
24
   std::ifstream is(path);
24
   std::ifstream is(path);
25
 
25
 
26
   if (!is.good()) {
26
   if (!is.good()) {
27
-    throw ParseExc(0, 0, "valid file", "file I could not open", path);
27
+    throw ParseFileExc(path);
28
   }
28
   }
29
 
29
 
30
   char c;
30
   char c;
@@ -45,12 +45,12 @@ void ConfigFileParser::parse(const std::string& path) {
45
       case NONE:
45
       case NONE:
46
         if (std::isspace(c)) continue;
46
         if (std::isspace(c)) continue;
47
         if (c == '[') {
47
         if (c == '[') {
48
-          if (tmp.size()) curKV[tmp] = Val{trim(tmp2), valLine, valPos};
48
+          if (tmp.size()) curKV[tmp] = Val{trim(tmp2), valLine, valPos, path};
49
           tmp.clear();
49
           tmp.clear();
50
           tmp2.clear();
50
           tmp2.clear();
51
           for (auto h : headers) {
51
           for (auto h : headers) {
52
             if (_secs.find(h) != _secs.end()) {
52
             if (_secs.find(h) != _secs.end()) {
53
-              _kvs[_secs[h]].insert(curKV.begin(), curKV.end());
53
+              updateVals(_secs[h], curKV);
54
             } else {
54
             } else {
55
               _secs[h] = _kvs.size();
55
               _secs[h] = _kvs.size();
56
               _kvs.push_back(curKV);
56
               _kvs.push_back(curKV);
@@ -139,12 +139,12 @@ void ConfigFileParser::parse(const std::string& path) {
139
           continue;
139
           continue;
140
         }
140
         }
141
         if (c == '[') {
141
         if (c == '[') {
142
-          curKV[tmp] = Val{trim(tmp2), valLine, valPos};
142
+          curKV[tmp] = Val{trim(tmp2), valLine, valPos, path};
143
           tmp.clear();
143
           tmp.clear();
144
           tmp2.clear();
144
           tmp2.clear();
145
           for (auto h : headers) {
145
           for (auto h : headers) {
146
             if (_secs.find(h) != _secs.end()) {
146
             if (_secs.find(h) != _secs.end()) {
147
-              _kvs[_secs[h]].insert(curKV.begin(), curKV.end());
147
+              updateVals(_secs[h], curKV);
148
             } else {
148
             } else {
149
               _secs[h] = _kvs.size();
149
               _secs[h] = _kvs.size();
150
               _kvs.push_back(curKV);
150
               _kvs.push_back(curKV);
@@ -156,7 +156,7 @@ void ConfigFileParser::parse(const std::string& path) {
156
           continue;
156
           continue;
157
         }
157
         }
158
         if (isKeyChar(c)) {
158
         if (isKeyChar(c)) {
159
-          curKV[tmp] = Val{trim(tmp2), valLine, valPos};
159
+          curKV[tmp] = Val{trim(tmp2), valLine, valPos, path};
160
           tmp.clear();
160
           tmp.clear();
161
           tmp2.clear();
161
           tmp2.clear();
162
           tmp.push_back(c);
162
           tmp.push_back(c);
@@ -186,12 +186,12 @@ void ConfigFileParser::parse(const std::string& path) {
186
     throw ParseExc(l, pos, "character", "<EOF>", path);
186
     throw ParseExc(l, pos, "character", "<EOF>", path);
187
   }
187
   }
188
 
188
 
189
-  if (tmp.size() && tmp2.size()) curKV[tmp] = Val{tmp2, valLine, valPos};
189
+  if (tmp.size() && tmp2.size()) curKV[tmp] = Val{tmp2, valLine, valPos, path};
190
   tmp.clear();
190
   tmp.clear();
191
   tmp2.clear();
191
   tmp2.clear();
192
   for (auto h : headers) {
192
   for (auto h : headers) {
193
     if (_secs.find(h) != _secs.end()) {
193
     if (_secs.find(h) != _secs.end()) {
194
-      _kvs[_secs[h]].insert(curKV.begin(), curKV.end());
194
+      updateVals(_secs[h], curKV);
195
     } else {
195
     } else {
196
       _secs[h] = _kvs.size();
196
       _secs[h] = _kvs.size();
197
       _kvs.push_back(curKV);
197
       _kvs.push_back(curKV);
@@ -327,3 +327,10 @@ bool ConfigFileParser::hasKey(Sec section, Key key) const {
327
 
327
 
328
   return true;
328
   return true;
329
 }
329
 }
330
+
331
+// _____________________________________________________________________________
332
+void ConfigFileParser::updateVals(size_t sec, const KeyVals& kvs) {
333
+  for (auto& kv : kvs) {
334
+    _kvs[sec][kv.first] = kv.second;
335
+  }
336
+}

+ 16 - 0
ConfigFileParser.h

@@ -16,6 +16,7 @@ struct Val {
16
   std::string val;
16
   std::string val;
17
   size_t line;
17
   size_t line;
18
   size_t pos;
18
   size_t pos;
19
+  std::string file;
19
 };
20
 };
20
 
21
 
21
 typedef std::string Key;
22
 typedef std::string Key;
@@ -34,6 +35,19 @@ enum State {
34
   IN_KEY_VAL_VAL_HANG_END
35
   IN_KEY_VAL_VAL_HANG_END
35
 };
36
 };
36
 
37
 
38
+class ParseFileExc : public std::exception {
39
+ public:
40
+  ParseFileExc(const std::string file) : _file(file) {
41
+    std::stringstream ss;
42
+    ss << _file << ": Could not open file.";
43
+    _msg = ss.str();
44
+  };
45
+  virtual const char* what() const throw() { return _msg.c_str(); }
46
+
47
+ private:
48
+  std::string _file, _msg;
49
+};
50
+
37
 class ParseExc : public std::exception {
51
 class ParseExc : public std::exception {
38
  public:
52
  public:
39
   ParseExc(size_t l, size_t p, std::string exc, std::string f, std::string file)
53
   ParseExc(size_t l, size_t p, std::string exc, std::string f, std::string file)
@@ -81,5 +95,7 @@ class ConfigFileParser {
81
   bool isKeyChar(char t) const;
95
   bool isKeyChar(char t) const;
82
   std::string trim(const std::string& str) const;
96
   std::string trim(const std::string& str) const;
83
   bool toBool(std::string str) const;
97
   bool toBool(std::string str) const;
98
+
99
+  void updateVals(size_t sec, const KeyVals& kvs);
84
 };
100
 };
85
 }
101
 }