author | František Kučera <franta-hg@frantovo.cz> |
Thu, 26 Nov 2020 18:42:38 +0100 | |
branch | v_0 |
changeset 21 | b35baebf5005 |
parent 20 | 9187f0439ca9 |
child 22 | 29d673a54ecf |
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 |
|
21
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
85 |
/** |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
86 |
* This might be configurable. |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
87 |
* |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
88 |
* Classic INI uses „; comment“ syntax. |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
89 |
* But many existing files contain „# comment“ lines. |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
90 |
* |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
91 |
* Only single character separators are supported (works same as keyValueSeparators). |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
92 |
*/ |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
93 |
std::string commentSeparators = ";#"; |
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
94 |
|
16 | 95 |
int lineNumber = 1; |
96 |
int eventNumber = 0; |
|
97 |
||
98 |
/** |
|
99 |
* Should be always used instead of input.peek(). |
|
100 |
* Skips \r. |
|
101 |
*/ |
|
102 |
char peek() { |
|
103 |
// In 2020 there is no need to manually return the carriage. However some legacy systems still do it. |
|
104 |
char ch = input.peek(); |
|
105 |
if (ch == '\r') { |
|
106 |
input.get(); |
|
107 |
ch = input.peek(); |
|
108 |
} |
|
109 |
return ch; |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Should be always used instead of input.get(). |
|
114 |
* Counts the lines and skips \r. |
|
115 |
*/ |
|
116 |
char get() { |
|
117 |
char ch = input.get(); |
|
118 |
if (ch == '\n') lineNumber++; |
|
119 |
else if (ch == '\r') ch = get(); |
|
120 |
return ch; |
|
121 |
} |
|
122 |
||
123 |
std::string readSpacesAndTabs() { |
|
124 |
std::stringstream result; |
|
125 |
for (char ch = peek(); input.good() && (ch == ' ' || ch == '\t'); ch = peek()) result.put(get()); |
|
126 |
return result.str(); |
|
127 |
} |
|
128 |
||
129 |
std::string readAllWhitespace() { |
|
130 |
std::stringstream result; |
|
131 |
for (char ch = peek(); input.good() && (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); ch = peek()) result.put(get()); |
|
132 |
return result.str(); |
|
133 |
} |
|
134 |
||
135 |
void processContinuingLine(std::stringstream& result) { |
|
136 |
if (consumeLeadingSpacesOnContinuingLines) readSpacesAndTabs(); |
|
137 |
else result.put('\n'); |
|
138 |
} |
|
139 |
||
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
|
140 |
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
|
141 |
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
|
142 |
} |
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
|
143 |
|
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
|
144 |
std::string readUntil(const std::string& until, bool* found = nullptr) { |
16 | 145 |
std::stringstream result; |
146 |
||
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
|
147 |
for (char ch = peek(); input.good() && !oneOf(ch, until); ch = peek()) { |
16 | 148 |
if (ch == '\\') { |
149 |
get(); |
|
150 |
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
|
151 |
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
|
152 |
else if (oneOf(ch, until)) result.put(ch); |
16 | 153 |
else if (ch == std::istream::traits_type::eof()) break; |
154 |
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
|
155 |
// 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
|
156 |
// 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
|
157 |
// other escape sequences are leaved untouched and will be processed in later phases, see see UnescapingINIHandler |
16 | 158 |
} else { |
159 |
ch = get(); |
|
160 |
result.put(ch); |
|
161 |
} |
|
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 |
if (oneOf(peek(), until)) { |
16 | 165 |
get(); |
166 |
if (found) *found = true; |
|
167 |
} else { |
|
168 |
if (found) *found = false; |
|
169 |
} |
|
170 |
||
171 |
return result.str(); |
|
172 |
} |
|
173 |
||
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 |
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
|
175 |
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
|
176 |
} |
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
|
177 |
|
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
|
178 |
std::string readToken(const std::string& until, char* quote = nullptr, bool* found = nullptr) { |
16 | 179 |
std::string result; |
180 |
||
181 |
char ch = peek(); |
|
182 |
if (isQuote(ch)) { |
|
183 |
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
|
184 |
result = readUntil(std::string(1, get()), found); |
16 | 185 |
} else { |
186 |
if (quote) *quote = 0; |
|
187 |
result = readUntil(until, found); |
|
188 |
} |
|
189 |
||
190 |
return result; |
|
191 |
} |
|
192 |
||
193 |
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
|
194 |
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
|
195 |
} |
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
|
196 |
|
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
|
197 |
std::string readTokenAndEatTerminator(const std::string& until, char* quote = nullptr, bool* found = nullptr) { |
16 | 198 |
std::string result = readToken(until, quote, found); |
199 |
if (*quote) { |
|
200 |
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
|
201 |
if (!oneOf(get(), until)) throw std::logic_error(std::string("missing „") + until + "“ after quoted section name"); |
16 | 202 |
} |
203 |
return result; |
|
204 |
} |
|
205 |
||
206 |
bool isComment(char ch) { |
|
21
b35baebf5005
prepare for multiple and configurable comment separators (e.g. „; comment“ and „# comment“)
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
207 |
return oneOf(ch, commentSeparators); |
16 | 208 |
} |
209 |
||
210 |
bool isQuote(char ch) { |
|
211 |
return ch == '"' || ch == '\''; |
|
212 |
} |
|
213 |
||
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
|
214 |
/** |
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
|
215 |
* @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
|
216 |
* @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
|
217 |
* @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
|
218 |
*/ |
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
|
219 |
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
|
220 |
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
|
221 |
} |
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
|
222 |
|
16 | 223 |
std::string trim(std::string s) { |
224 |
return std::regex_replace(s, std::regex("^\\s+|\\s+$"), ""); |
|
225 |
} |
|
226 |
||
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
227 |
public: |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
228 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
229 |
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
|
230 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
231 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
232 |
void addHandler(INIContentHandler* handler) override { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
233 |
handlers.push_back(handler); |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
234 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
235 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
236 |
void process() override { |
1
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
237 |
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
|
238 |
|
16 | 239 |
bool inSection = false; |
240 |
||
241 |
while (input.good()) { // TODO: condition |
|
242 |
{ |
|
19
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
243 |
INIContentHandler::WhitespaceEvent event; |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
244 |
event.lineNumber = lineNumber; |
16 | 245 |
std::string whitespace = readAllWhitespace(); |
246 |
if (whitespace.size()) { |
|
247 |
event.eventNumber = ++eventNumber; |
|
248 |
event.whitespace = whitespace; |
|
249 |
for (INIContentHandler* handler : handlers) handler->whitespace(event); |
|
250 |
} |
|
251 |
} |
|
1
3876a9c56a66
simple INI parser based on regular expressions
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
252 |
|
16 | 253 |
bool found; |
254 |
char quote; |
|
255 |
||
256 |
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
|
257 |
|
16 | 258 |
if (ch == std::istream::traits_type::eof()) { |
259 |
break; |
|
260 |
} else if (ch == '[') { |
|
261 |
if (inSection) for (INIContentHandler* handler : handlers) handler->endSection(); |
|
262 |
inSection = true; |
|
263 |
INIContentHandler::SectionStartEvent event; |
|
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
264 |
event.lineNumber = lineNumber; |
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
265 |
event.eventNumber = ++eventNumber; |
19
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
266 |
get(); |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
267 |
readAllWhitespace(); |
16 | 268 |
event.name = readTokenAndEatTerminator(']', "e, &found); |
269 |
||
270 |
readSpacesAndTabs(); |
|
271 |
if (allowSectionTags && peek() == '[') { |
|
272 |
get(); |
|
273 |
event.tag = readTokenAndEatTerminator(']', "e, &found); |
|
274 |
} |
|
275 |
||
276 |
readSpacesAndTabs(); |
|
277 |
ch = peek(); |
|
278 |
if (isComment(ch)) { |
|
279 |
get(); |
|
280 |
readSpacesAndTabs(); |
|
281 |
event.comment = readUntil('\n', &found); |
|
282 |
} else if (ch == '\n') { |
|
283 |
get(); |
|
284 |
} else { |
|
285 |
throw std::logic_error(std::string("unexpected content after the section: '") + event.name + "'"); |
|
286 |
} |
|
287 |
||
288 |
for (INIContentHandler* handler : handlers) handler->startSection(event); |
|
289 |
} else if (isComment(ch)) { |
|
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
290 |
INIContentHandler::CommentEvent event; |
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
291 |
event.lineNumber = lineNumber; |
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
292 |
event.eventNumber = ++eventNumber; |
19
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
293 |
get(); |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
294 |
readSpacesAndTabs(); |
16 | 295 |
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
|
296 |
for (INIContentHandler* handler : handlers) handler->comment(event); |
16 | 297 |
} else { |
19
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
298 |
INIContentHandler::EntryEvent event; |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
299 |
event.lineNumber = lineNumber; |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
300 |
event.eventNumber = ++eventNumber; |
967f73af64a4
start event numbers with 1
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
301 |
|
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
|
302 |
std::string fullKey = readToken(keyValueSeparators, "e, &found); |
16 | 303 |
if (!found) throw std::logic_error(std::string("missing = after key: '") + fullKey + "'"); |
304 |
if (!quote) fullKey = trim(fullKey); |
|
305 |
readSpacesAndTabs(); |
|
306 |
||
307 |
if (quote) { |
|
308 |
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
|
309 |
if (oneOf(ch, keyValueSeparators)) readSpacesAndTabs(); |
16 | 310 |
else throw std::logic_error(std::string("missing = after quoted key: '") + fullKey + "'"); |
311 |
} |
|
312 |
||
313 |
std::string value = readToken('\n', "e, &found); |
|
314 |
if (!quote) value = trim(value); |
|
315 |
||
316 |
event.key = fullKey; |
|
317 |
event.fullKey = fullKey; |
|
318 |
event.value = value; |
|
14
ea431c469403
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
319 |
|
16 | 320 |
if (allowSubKeys) { |
321 |
std::smatch match; |
|
322 |
if (std::regex_match(fullKey, match, std::regex("([^\\[]+)\\[([^\\[]+)\\]"))) { |
|
323 |
event.key = match[1]; |
|
324 |
event.subKey = match[2]; |
|
325 |
event.fullKey = fullKey; |
|
326 |
} |
|
327 |
} |
|
328 |
||
329 |
if (quote) { |
|
330 |
readSpacesAndTabs(); |
|
331 |
ch = peek(); |
|
332 |
if (isComment(ch)) { |
|
333 |
get(); |
|
334 |
readSpacesAndTabs(); |
|
335 |
event.comment = readUntil('\n', &found); |
|
336 |
} else if (ch == '\n') { |
|
337 |
get(); |
|
338 |
} else { |
|
339 |
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
|
340 |
} |
ea431c469403
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
341 |
} |
ea431c469403
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
342 |
|
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
|
343 |
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
|
344 |
} |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
345 |
} |
16 | 346 |
// TODO: error at the end, catch premature/unexpected EOF |
347 |
// TODO: unescape + trim values + ignore \r |
|
348 |
// 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
|
349 |
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
|
350 |
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
|
351 |
} |
16 | 352 |
|
353 |
// General feautres: |
|
354 |
// TODO: warning/error handler |
|
355 |
// TODO: support also escaped characters |
|
356 |
// TODO: support also Java .properties and manifest.mf formats? |
|
357 |
// TODO: support also nested sections – hierarchy |
|
358 |
// TODO: support also nested keys e.g. key.sub.subsub.subsubsub=value – translate them to nested sections |
|
359 |
// TODO: support also option for alternative key-value separator (: instead of =) |
|
360 |
// TODO: support also other encodings (currently only UTF-8 is supported) |
|
361 |
// TODO: better exceptions |
|
362 |
||
363 |
// Lossless conversions: |
|
364 |
// TODO: emit also the quote style ('/"/) |
|
365 |
// TODO: emit also the comment style (;/#) ? |
|
366 |
// TODO: emit also the whitespace before key name, around =, after "values"/'values', around [sections] ? |
|
367 |
// TODO: emit also the line-end type (LF/CRLF) ? |
|
368 |
||
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
369 |
}; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
370 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
371 |
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
|
372 |
return new INIReaderImpl(input); |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
373 |
} |
16 | 374 |
|
375 |
} |
|
376 |
} |
|
377 |
} |
|
378 |
} |