Browse Source

include mechanism

Patrick Brosi 5 years ago
parent
commit
afacc8bc77
2 changed files with 23 additions and 2 deletions
  1. 21 2
      ConfigFileParser.cpp
  2. 2 0
      ConfigFileParser.h

+ 21 - 2
ConfigFileParser.cpp

@@ -44,7 +44,7 @@ void ConfigFileParser::parse(const std::string& path) {
44
     switch (s) {
44
     switch (s) {
45
       case NONE:
45
       case NONE:
46
         if (std::isspace(c)) continue;
46
         if (std::isspace(c)) continue;
47
-        if (c == '[') {
47
+        if (c == '[' || c == '<') {
48
           if (tmp.size()) curKV[tmp] = Val{trim(tmp2), valLine, valPos, path};
48
           if (tmp.size()) curKV[tmp] = Val{trim(tmp2), valLine, valPos, path};
49
           tmp.clear();
49
           tmp.clear();
50
           tmp2.clear();
50
           tmp2.clear();
@@ -58,7 +58,8 @@ void ConfigFileParser::parse(const std::string& path) {
58
           }
58
           }
59
           headers.clear();
59
           headers.clear();
60
           curKV.clear();
60
           curKV.clear();
61
-          s = IN_HEAD;
61
+          if (c == '[') s = IN_HEAD;
62
+          if (c == '<') s = IN_INC;
62
           continue;
63
           continue;
63
         }
64
         }
64
         if (isKeyChar(c)) {
65
         if (isKeyChar(c)) {
@@ -69,6 +70,19 @@ void ConfigFileParser::parse(const std::string& path) {
69
         }
70
         }
70
         throw ParseExc(l, pos, "header or key", std::string("'") + c + "'",
71
         throw ParseExc(l, pos, "header or key", std::string("'") + c + "'",
71
                        path);
72
                        path);
73
+
74
+      case IN_INC:
75
+        if (c == '\n') {
76
+          throw ParseExc(l, pos, ">", "<newline>", path);
77
+        } else if (c == '>') {
78
+          parse(tmp);
79
+          tmp.clear();
80
+          s = NONE;
81
+        } else {
82
+          tmp += c;
83
+        }
84
+        continue;
85
+
72
       case IN_HEAD:
86
       case IN_HEAD:
73
         if (std::isspace(c)) continue;
87
         if (std::isspace(c)) continue;
74
         if (isKeyChar(c)) {
88
         if (isKeyChar(c)) {
@@ -319,6 +333,11 @@ const Val& ConfigFileParser::getVal(Sec section, Key key) const {
319
 }
333
 }
320
 
334
 
321
 // _____________________________________________________________________________
335
 // _____________________________________________________________________________
336
+const KeyVals& ConfigFileParser::getKeyVals(Sec section) const {
337
+  return _kvs[_secs.find(section)->second];
338
+}
339
+
340
+// _____________________________________________________________________________
322
 bool ConfigFileParser::hasKey(Sec section, Key key) const {
341
 bool ConfigFileParser::hasKey(Sec section, Key key) const {
323
   if (_secs.find(section) == _secs.end()) return false;
342
   if (_secs.find(section) == _secs.end()) return false;
324
   if (_kvs[_secs.find(section)->second].find(key) ==
343
   if (_kvs[_secs.find(section)->second].find(key) ==

+ 2 - 0
ConfigFileParser.h

@@ -26,6 +26,7 @@ typedef std::unordered_map<std::string, Val> KeyVals;
26
 enum State {
26
 enum State {
27
   NONE,
27
   NONE,
28
   IN_HEAD,
28
   IN_HEAD,
29
+  IN_INC,
29
   IN_HEAD_KEY,
30
   IN_HEAD_KEY,
30
   IN_HEAD_AW_COM_OR_END,
31
   IN_HEAD_AW_COM_OR_END,
31
   IN_KEY_VAL_KEY,
32
   IN_KEY_VAL_KEY,
@@ -87,6 +88,7 @@ class ConfigFileParser {
87
   bool hasKey(Sec section, Key key) const;
88
   bool hasKey(Sec section, Key key) const;
88
 
89
 
89
   const Val& getVal(Sec section, Key key) const;
90
   const Val& getVal(Sec section, Key key) const;
91
+  const KeyVals& getKeyVals(Sec section) const;
90
 
92
 
91
  private:
93
  private:
92
   std::unordered_map<std::string, size_t> _secs;
94
   std::unordered_map<std::string, size_t> _secs;