Преглед на файлове

allowing value override

Patrick Brosi преди 6 години
родител
ревизия
63fcb1d54e
променени са 2 файла, в които са добавени 31 реда и са изтрити 8 реда
  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 24
   std::ifstream is(path);
25 25
 
26 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 30
   char c;
@@ -45,12 +45,12 @@ void ConfigFileParser::parse(const std::string& path) {
45 45
       case NONE:
46 46
         if (std::isspace(c)) continue;
47 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 49
           tmp.clear();
50 50
           tmp2.clear();
51 51
           for (auto h : headers) {
52 52
             if (_secs.find(h) != _secs.end()) {
53
-              _kvs[_secs[h]].insert(curKV.begin(), curKV.end());
53
+              updateVals(_secs[h], curKV);
54 54
             } else {
55 55
               _secs[h] = _kvs.size();
56 56
               _kvs.push_back(curKV);
@@ -139,12 +139,12 @@ void ConfigFileParser::parse(const std::string& path) {
139 139
           continue;
140 140
         }
141 141
         if (c == '[') {
142
-          curKV[tmp] = Val{trim(tmp2), valLine, valPos};
142
+          curKV[tmp] = Val{trim(tmp2), valLine, valPos, path};
143 143
           tmp.clear();
144 144
           tmp2.clear();
145 145
           for (auto h : headers) {
146 146
             if (_secs.find(h) != _secs.end()) {
147
-              _kvs[_secs[h]].insert(curKV.begin(), curKV.end());
147
+              updateVals(_secs[h], curKV);
148 148
             } else {
149 149
               _secs[h] = _kvs.size();
150 150
               _kvs.push_back(curKV);
@@ -156,7 +156,7 @@ void ConfigFileParser::parse(const std::string& path) {
156 156
           continue;
157 157
         }
158 158
         if (isKeyChar(c)) {
159
-          curKV[tmp] = Val{trim(tmp2), valLine, valPos};
159
+          curKV[tmp] = Val{trim(tmp2), valLine, valPos, path};
160 160
           tmp.clear();
161 161
           tmp2.clear();
162 162
           tmp.push_back(c);
@@ -186,12 +186,12 @@ void ConfigFileParser::parse(const std::string& path) {
186 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 190
   tmp.clear();
191 191
   tmp2.clear();
192 192
   for (auto h : headers) {
193 193
     if (_secs.find(h) != _secs.end()) {
194
-      _kvs[_secs[h]].insert(curKV.begin(), curKV.end());
194
+      updateVals(_secs[h], curKV);
195 195
     } else {
196 196
       _secs[h] = _kvs.size();
197 197
       _kvs.push_back(curKV);
@@ -327,3 +327,10 @@ bool ConfigFileParser::hasKey(Sec section, Key key) const {
327 327
 
328 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 16
   std::string val;
17 17
   size_t line;
18 18
   size_t pos;
19
+  std::string file;
19 20
 };
20 21
 
21 22
 typedef std::string Key;
@@ -34,6 +35,19 @@ enum State {
34 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 51
 class ParseExc : public std::exception {
38 52
  public:
39 53
   ParseExc(size_t l, size_t p, std::string exc, std::string f, std::string file)
@@ -81,5 +95,7 @@ class ConfigFileParser {
81 95
   bool isKeyChar(char t) const;
82 96
   std::string trim(const std::string& str) const;
83 97
   bool toBool(std::string str) const;
98
+
99
+  void updateVals(size_t sec, const KeyVals& kvs);
84 100
 };
85 101
 }