author | František Kučera <franta-hg@frantovo.cz> |
Thu, 26 Nov 2020 18:28:27 +0100 | |
branch | v_0 |
changeset 20 | 9187f0439ca9 |
parent 19 | 967f73af64a4 |
child 21 | b35baebf5005 |
permissions | -rw-r--r-- |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
#include <vector> |
1
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
19 |
#include <regex> |
16 | 20 |
#include <sstream> |
21 |
#include <stdexcept> |
|
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include "INIReader.h" |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
|
16 | 25 |
namespace relpipe { |
26 |
namespace in { |
|
27 |
namespace ini { |
|
28 |
namespace lib { |
|
29 |
||
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
class INIReaderImpl : public INIReader { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
private: |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
std::istream& input; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
std::vector<INIContentHandler*> handlers; |
16 | 34 |
|
35 |
/** |
|
36 |
* This might be configurable. |
|
37 |
* |
|
38 |
* By default, we ignore all leading whitespace on continuing lines. |
|
39 |
* If there should be some spaces or tabs, they should be placed on the previous line before the „\“. |
|
40 |
* If a line break is desired, it should be written as \n (escaped) or the value should be quoted in " or '. |
|
41 |
* |
|
42 |
* Related specifications: |
|
43 |
* - https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Properties.html |
|
44 |
*/ |
|
45 |
bool consumeLeadingSpacesOnContinuingLines = true; |
|
46 |
||
47 |
/** |
|
48 |
* This might be configurable. |
|
49 |
* |
|
50 |
* KDE uses some weird INI dialect that allows [section][x] syntax where „x“ is kind of „tag“ that signalizes some properties of given section. |
|
51 |
* Line „[section_1][$i]“ means that the „section_1“ is „locked“. |
|
52 |
* We may emit this information somehow later, but for now, it is just ignored. |
|
53 |
* |
|
54 |
* TODO: Is „section tag“ right name? |
|
55 |
* |
|
56 |
* Related specifications: |
|
57 |
* - https://userbase.kde.org/KDE_System_Administration/Configuration_Files#Lock_Down |
|
58 |
*/ |
|
59 |
bool allowSectionTags = true; |
|
60 |
||
61 |
/** |
|
62 |
* This might be configurable. |
|
63 |
* |
|
64 |
* If whole key is „aaa[bbb]“ then „aaa“ is considered to be the key and „bbb“ the sub-key. |
|
65 |
* No \[ escaping is currently supported, so the key might not contain the bracket character. |
|
66 |
* |
|
67 |
* Related specifications: |
|
68 |
* - https://userbase.kde.org/KDE_System_Administration/Configuration_Files#Shell_Expansion |
|
69 |
* - https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s05.html |
|
70 |
*/ |
|
71 |
bool allowSubKeys = true; |
|
72 |
||
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
73 |
/** |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
74 |
* This might be configurable. |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
75 |
* |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
76 |
* Classic INI uses „key=value“ syntax. |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
77 |
* But some other formats/dialects might use key:value. |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
78 |
* |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
79 |
* Only single character separators are supported. |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
80 |
* If multiple separators should be recognized (e.g. both „=“ and „:“), this string will contain all of them, |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
81 |
* i.e. „:=“ does not mean that the „key:=value“ syntax, but „key=value“ or „key:value“. |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
82 |
*/ |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
83 |
std::string keyValueSeparators = "="; |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
84 |
|
16 | 85 |
int lineNumber = 1; |
86 |
int eventNumber = 0; |
|
87 |
||
88 |
/** |
|
89 |
* Should be always used instead of input.peek(). |
|
90 |
* Skips \r. |
|
91 |
*/ |
|
92 |
char peek() { |
|
93 |
// In 2020 there is no need to manually return the carriage. However some legacy systems still do it. |
|
94 |
char ch = input.peek(); |
|
95 |
if (ch == '\r') { |
|
96 |
input.get(); |
|
97 |
ch = input.peek(); |
|
98 |
} |
|
99 |
return ch; |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Should be always used instead of input.get(). |
|
104 |
* Counts the lines and skips \r. |
|
105 |
*/ |
|
106 |
char get() { |
|
107 |
char ch = input.get(); |
|
108 |
if (ch == '\n') lineNumber++; |
|
109 |
else if (ch == '\r') ch = get(); |
|
110 |
return ch; |
|
111 |
} |
|
112 |
||
113 |
std::string readSpacesAndTabs() { |
|
114 |
std::stringstream result; |
|
115 |
for (char ch = peek(); input.good() && (ch == ' ' || ch == '\t'); ch = peek()) result.put(get()); |
|
116 |
return result.str(); |
|
117 |
} |
|
118 |
||
119 |
std::string readAllWhitespace() { |
|
120 |
std::stringstream result; |
|
121 |
for (char ch = peek(); input.good() && (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); ch = peek()) result.put(get()); |
|
122 |
return result.str(); |
|
123 |
} |
|
124 |
||
125 |
void processContinuingLine(std::stringstream& result) { |
|
126 |
if (consumeLeadingSpacesOnContinuingLines) readSpacesAndTabs(); |
|
127 |
else result.put('\n'); |
|
128 |
} |
|
129 |
||
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
130 |
std::string readUntil(const char until, bool* found = nullptr) { |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
131 |
return readUntil(std::string(1, until), found); |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
132 |
} |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
133 |
|
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
134 |
std::string readUntil(const std::string& until, bool* found = nullptr) { |
16 | 135 |
std::stringstream result; |
136 |
||
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
137 |
for (char ch = peek(); input.good() && !oneOf(ch, until); ch = peek()) { |
16 | 138 |
if (ch == '\\') { |
139 |
get(); |
|
140 |
ch = get(); |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
141 |
if (oneOf(ch, until) && ch == '\n') processContinuingLine(result); |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
142 |
else if (oneOf(ch, until)) result.put(ch); |
16 | 143 |
else if (ch == std::istream::traits_type::eof()) break; |
144 |
else result.put('\\').put(ch); |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
145 |
// unescaping is done in two phases: |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
146 |
// here we unescape just the \n (LF) |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
147 |
// other escape sequences are leaved untouched and will be processed in later phases, see see UnescapingINIHandler |
16 | 148 |
} else { |
149 |
ch = get(); |
|
150 |
result.put(ch); |
|
151 |
} |
|
152 |
} |
|
153 |
||
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
154 |
if (oneOf(peek(), until)) { |
16 | 155 |
get(); |
156 |
if (found) *found = true; |
|
157 |
} else { |
|
158 |
if (found) *found = false; |
|
159 |
} |
|
160 |
||
161 |
return result.str(); |
|
162 |
} |
|
163 |
||
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
164 |
std::string readToken(const char until, char* quote = nullptr, bool* found = nullptr) { |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
165 |
return readToken(std::string(1, until), quote, found); |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
166 |
} |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
167 |
|
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
168 |
std::string readToken(const std::string& until, char* quote = nullptr, bool* found = nullptr) { |
16 | 169 |
std::string result; |
170 |
||
171 |
char ch = peek(); |
|
172 |
if (isQuote(ch)) { |
|
173 |
if (quote) *quote = ch; |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
174 |
result = readUntil(std::string(1, get()), found); |
16 | 175 |
} else { |
176 |
if (quote) *quote = 0; |
|
177 |
result = readUntil(until, found); |
|
178 |
} |
|
179 |
||
180 |
return result; |
|
181 |
} |
|
182 |
||
183 |
std::string readTokenAndEatTerminator(char until, char* quote = nullptr, bool* found = nullptr) { |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
184 |
return readTokenAndEatTerminator(std::string(1, until), quote, found); |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
185 |
} |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
186 |
|
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
187 |
std::string readTokenAndEatTerminator(const std::string& until, char* quote = nullptr, bool* found = nullptr) { |
16 | 188 |
std::string result = readToken(until, quote, found); |
189 |
if (*quote) { |
|
190 |
readAllWhitespace(); |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
191 |
if (!oneOf(get(), until)) throw std::logic_error(std::string("missing „") + until + "“ after quoted section name"); |
16 | 192 |
} |
193 |
return result; |
|
194 |
} |
|
195 |
||
196 |
bool isComment(char ch) { |
|
197 |
return ch == '#' || ch == ';'; |
|
198 |
} |
|
199 |
||
200 |
bool isQuote(char ch) { |
|
201 |
return ch == '"' || ch == '\''; |
|
202 |
} |
|
203 |
||
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
204 |
/** |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
205 |
* @param ch character to be evaluated |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
206 |
* @param options list of options (characters) |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
207 |
* @return whether ch is one of options |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
208 |
*/ |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
209 |
bool oneOf(char ch, const std::string& options) { |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
210 |
return options.find(ch) != std::string::npos; |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
211 |
} |
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
212 |
|
16 | 213 |
std::string trim(std::string s) { |
214 |
return std::regex_replace(s, std::regex("^\\s+|\\s+$"), ""); |
|
215 |
} |
|
216 |
||
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
217 |
public: |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
218 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
219 |
INIReaderImpl(std::istream& input) : input(input) { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
220 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
221 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
222 |
void addHandler(INIContentHandler* handler) override { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
223 |
handlers.push_back(handler); |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
224 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
225 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
226 |
void process() override { |
1
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
227 |
for (INIContentHandler* handler : handlers) handler->startDocument(); |
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
228 |
|
16 | 229 |
bool inSection = false; |
230 |
||
231 |
while (input.good()) { // TODO: condition |
|
232 |
{ |
|
19
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
233 |
INIContentHandler::WhitespaceEvent event; |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
234 |
event.lineNumber = lineNumber; |
16 | 235 |
std::string whitespace = readAllWhitespace(); |
236 |
if (whitespace.size()) { |
|
237 |
event.eventNumber = ++eventNumber; |
|
238 |
event.whitespace = whitespace; |
|
239 |
for (INIContentHandler* handler : handlers) handler->whitespace(event); |
|
240 |
} |
|
241 |
} |
|
1
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
242 |
|
16 | 243 |
bool found; |
244 |
char quote; |
|
245 |
||
246 |
char ch = peek(); |
|
2
f031a4dc7c52
add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
247 |
|
16 | 248 |
if (ch == std::istream::traits_type::eof()) { |
249 |
break; |
|
250 |
} else if (ch == '[') { |
|
251 |
if (inSection) for (INIContentHandler* handler : handlers) handler->endSection(); |
|
252 |
inSection = true; |
|
253 |
INIContentHandler::SectionStartEvent event; |
|
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
254 |
event.lineNumber = lineNumber; |
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
255 |
event.eventNumber = ++eventNumber; |
19
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
256 |
get(); |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
257 |
readAllWhitespace(); |
16 | 258 |
event.name = readTokenAndEatTerminator(']', "e, &found); |
259 |
||
260 |
readSpacesAndTabs(); |
|
261 |
if (allowSectionTags && peek() == '[') { |
|
262 |
get(); |
|
263 |
event.tag = readTokenAndEatTerminator(']', "e, &found); |
|
264 |
} |
|
265 |
||
266 |
readSpacesAndTabs(); |
|
267 |
ch = peek(); |
|
268 |
if (isComment(ch)) { |
|
269 |
get(); |
|
270 |
readSpacesAndTabs(); |
|
271 |
event.comment = readUntil('\n', &found); |
|
272 |
} else if (ch == '\n') { |
|
273 |
get(); |
|
274 |
} else { |
|
275 |
throw std::logic_error(std::string("unexpected content after the section: '") + event.name + "'"); |
|
276 |
} |
|
277 |
||
278 |
for (INIContentHandler* handler : handlers) handler->startSection(event); |
|
279 |
} else if (isComment(ch)) { |
|
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
280 |
INIContentHandler::CommentEvent event; |
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
281 |
event.lineNumber = lineNumber; |
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
282 |
event.eventNumber = ++eventNumber; |
19
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
283 |
get(); |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
284 |
readSpacesAndTabs(); |
16 | 285 |
event.comment = readUntil('\n', &found); |
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
286 |
for (INIContentHandler* handler : handlers) handler->comment(event); |
16 | 287 |
} else { |
19
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
288 |
INIContentHandler::EntryEvent event; |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
289 |
event.lineNumber = lineNumber; |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
290 |
event.eventNumber = ++eventNumber; |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
291 |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
292 |
std::string fullKey = readToken(keyValueSeparators, "e, &found); |
16 | 293 |
if (!found) throw std::logic_error(std::string("missing = after key: '") + fullKey + "'"); |
294 |
if (!quote) fullKey = trim(fullKey); |
|
295 |
readSpacesAndTabs(); |
|
296 |
||
297 |
if (quote) { |
|
298 |
ch = get(); |
|
20
9187f0439ca9
prepare for multiple and configurable key-value separators (e.g. key=value and key:value)
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
299 |
if (oneOf(ch, keyValueSeparators)) readSpacesAndTabs(); |
16 | 300 |
else throw std::logic_error(std::string("missing = after quoted key: '") + fullKey + "'"); |
301 |
} |
|
302 |
||
303 |
std::string value = readToken('\n', "e, &found); |
|
304 |
if (!quote) value = trim(value); |
|
305 |
||
306 |
event.key = fullKey; |
|
307 |
event.fullKey = fullKey; |
|
308 |
event.value = value; |
|
14
ea431c469403
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
309 |
|
16 | 310 |
if (allowSubKeys) { |
311 |
std::smatch match; |
|
312 |
if (std::regex_match(fullKey, match, std::regex("([^\\[]+)\\[([^\\[]+)\\]"))) { |
|
313 |
event.key = match[1]; |
|
314 |
event.subKey = match[2]; |
|
315 |
event.fullKey = fullKey; |
|
316 |
} |
|
317 |
} |
|
318 |
||
319 |
if (quote) { |
|
320 |
readSpacesAndTabs(); |
|
321 |
ch = peek(); |
|
322 |
if (isComment(ch)) { |
|
323 |
get(); |
|
324 |
readSpacesAndTabs(); |
|
325 |
event.comment = readUntil('\n', &found); |
|
326 |
} else if (ch == '\n') { |
|
327 |
get(); |
|
328 |
} else { |
|
329 |
throw std::logic_error(std::string("unexpected content after the quoted value: key='") + fullKey + "' value='" + event.value + "'"); |
|
14
ea431c469403
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
330 |
} |
ea431c469403
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
331 |
} |
ea431c469403
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
332 |
|
2
f031a4dc7c52
add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
333 |
for (INIContentHandler* handler : handlers) handler->entry(event); |
1
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
334 |
} |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
335 |
} |
16 | 336 |
// TODO: error at the end, catch premature/unexpected EOF |
337 |
// TODO: unescape + trim values + ignore \r |
|
338 |
// TODO: count lines |
|
2
f031a4dc7c52
add options: --enable-sections --enable-subkeys --enable-comments --enable-line-numbers --enable-event-numbers
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
339 |
if (inSection) for (INIContentHandler* handler : handlers) handler->endSection(); |
1
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
340 |
for (INIContentHandler* handler : handlers) handler->endDocument(); |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
341 |
} |
16 | 342 |
|
343 |
// General feautres: |
|
344 |
// TODO: warning/error handler |
|
345 |
// TODO: support also escaped characters |
|
346 |
// TODO: support also Java .properties and manifest.mf formats? |
|
347 |
// TODO: support also nested sections – hierarchy |
|
348 |
// TODO: support also nested keys e.g. key.sub.subsub.subsubsub=value – translate them to nested sections |
|
349 |
// TODO: support also option for alternative key-value separator (: instead of =) |
|
350 |
// TODO: support also other encodings (currently only UTF-8 is supported) |
|
351 |
// TODO: better exceptions |
|
352 |
||
353 |
// Lossless conversions: |
|
354 |
// TODO: emit also the quote style ('/"/) |
|
355 |
// TODO: emit also the comment style (;/#) ? |
|
356 |
// TODO: emit also the whitespace before key name, around =, after "values"/'values', around [sections] ? |
|
357 |
// TODO: emit also the line-end type (LF/CRLF) ? |
|
358 |
||
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
359 |
}; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
360 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
361 |
INIReader* INIReader::create(std::istream& input) { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
362 |
return new INIReaderImpl(input); |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
363 |
} |
16 | 364 |
|
365 |
} |
|
366 |
} |
|
367 |
} |
|
368 |
} |