author | František Kučera <franta-hg@frantovo.cz> |
Wed, 25 Nov 2020 21:50:26 +0100 | |
branch | v_0 |
changeset 26 | 80e129ec3408 |
parent 25 | ee70b17950bd |
child 28 | 0e7c57d48d1e |
permissions | -rw-r--r-- |
16
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
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 |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
|
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
#include <vector> |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <regex> |
26 | 20 |
#include <sstream> |
21 |
#include <stdexcept> |
|
16
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
|
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include "INIReader.h" |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
|
26 | 25 |
namespace relpipe { |
26 |
namespace in { |
|
27 |
namespace ini { |
|
28 |
namespace lib { |
|
29 |
||
16
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
class INIReaderImpl : public INIReader { |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
private: |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
std::istream& input; |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
std::vector<INIContentHandler*> handlers; |
26 | 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 |
||
73 |
int lineNumber = 1; |
|
74 |
int eventNumber = 0; |
|
75 |
||
76 |
/** |
|
77 |
* Should be always used instead of input.peek(). |
|
78 |
* Skips \r. |
|
79 |
*/ |
|
80 |
char peek() { |
|
81 |
// In 2020 there is no need to manually return the carriage. However some legacy systems still do it. |
|
82 |
char ch = input.peek(); |
|
83 |
if (ch == '\r') { |
|
84 |
input.get(); |
|
85 |
ch = input.peek(); |
|
86 |
} |
|
87 |
return ch; |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Should be always used instead of input.get(). |
|
92 |
* Counts the lines and skips \r. |
|
93 |
*/ |
|
94 |
char get() { |
|
95 |
char ch = input.get(); |
|
96 |
if (ch == '\n') lineNumber++; |
|
97 |
else if (ch == '\r') ch = get(); |
|
98 |
return ch; |
|
99 |
} |
|
100 |
||
101 |
std::string readSpacesAndTabs() { |
|
102 |
std::stringstream result; |
|
103 |
for (char ch = peek(); input.good() && (ch == ' ' || ch == '\t'); ch = peek()) result.put(get()); |
|
104 |
return result.str(); |
|
105 |
} |
|
106 |
||
107 |
std::string readAllWhitespace() { |
|
108 |
std::stringstream result; |
|
109 |
for (char ch = peek(); input.good() && (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); ch = peek()) result.put(get()); |
|
110 |
return result.str(); |
|
111 |
} |
|
112 |
||
113 |
void processContinuingLine(std::stringstream& result) { |
|
114 |
if (consumeLeadingSpacesOnContinuingLines) readSpacesAndTabs(); |
|
115 |
else result.put('\n'); |
|
116 |
} |
|
117 |
||
118 |
std::string readUntil(char until, bool* found = nullptr) { |
|
119 |
std::stringstream result; |
|
120 |
||
121 |
for (char ch = peek(); input.good() && ch != until; ch = peek()) { |
|
122 |
if (ch == '\\') { |
|
123 |
get(); |
|
124 |
ch = get(); |
|
125 |
if (ch == until && ch == '\n') processContinuingLine(result); |
|
126 |
else if (ch == until) result.put(ch); |
|
127 |
else if (ch == std::istream::traits_type::eof()) break; |
|
128 |
else result.put('\\').put(ch); |
|
129 |
// TODO: two-stage and modular unescaping: here unescape only \+LF or more genereally: unescape only the until character and rest leave untouched |
|
130 |
// second escaping stage move to separate class/wrapper (similar to hierarchical wrappers) |
|
131 |
} else { |
|
132 |
ch = get(); |
|
133 |
result.put(ch); |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
if (peek() == until) { |
|
138 |
get(); |
|
139 |
if (found) *found = true; |
|
140 |
} else { |
|
141 |
if (found) *found = false; |
|
142 |
} |
|
143 |
||
144 |
return result.str(); |
|
145 |
} |
|
146 |
||
147 |
std::string readToken(char until, char* quote = nullptr, bool* found = nullptr) { |
|
148 |
std::string result; |
|
149 |
||
150 |
char ch = peek(); |
|
151 |
if (isQuote(ch)) { |
|
152 |
if (quote) *quote = ch; |
|
153 |
result = readUntil(get(), found); |
|
154 |
} else { |
|
155 |
if (quote) *quote = 0; |
|
156 |
result = readUntil(until, found); |
|
157 |
} |
|
158 |
||
159 |
return result; |
|
160 |
} |
|
161 |
||
162 |
std::string readTokenAndEatTerminator(char until, char* quote = nullptr, bool* found = nullptr) { |
|
163 |
std::string result = readToken(until, quote, found); |
|
164 |
if (*quote) { |
|
165 |
readAllWhitespace(); |
|
166 |
if (get() != until) throw std::logic_error(std::string("missing „") + std::string(1, until) + "“ after quoted section name"); |
|
167 |
} |
|
168 |
return result; |
|
169 |
} |
|
170 |
||
171 |
bool isComment(char ch) { |
|
172 |
return ch == '#' || ch == ';'; |
|
173 |
} |
|
174 |
||
175 |
bool isQuote(char ch) { |
|
176 |
return ch == '"' || ch == '\''; |
|
177 |
} |
|
178 |
||
179 |
std::string trim(std::string s) { |
|
180 |
return std::regex_replace(s, std::regex("^\\s+|\\s+$"), ""); |
|
181 |
} |
|
182 |
||
16
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
183 |
public: |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
184 |
|
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
185 |
INIReaderImpl(std::istream& input) : input(input) { |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
186 |
} |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
187 |
|
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
188 |
void addHandler(INIContentHandler* handler) override { |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
189 |
handlers.push_back(handler); |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
190 |
} |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
191 |
|
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
192 |
void process() override { |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
193 |
for (INIContentHandler* handler : handlers) handler->startDocument(); |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
194 |
|
26 | 195 |
bool inSection = false; |
196 |
||
197 |
while (input.good()) { // TODO: condition |
|
198 |
{ |
|
199 |
std::string whitespace = readAllWhitespace(); |
|
200 |
if (whitespace.size()) { |
|
201 |
INIContentHandler::WhitespaceEvent event; |
|
202 |
event.lineNumber = lineNumber; |
|
203 |
event.eventNumber = ++eventNumber; |
|
204 |
event.whitespace = whitespace; |
|
205 |
for (INIContentHandler* handler : handlers) handler->whitespace(event); |
|
206 |
} |
|
207 |
} |
|
16
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
208 |
|
26 | 209 |
bool found; |
210 |
char quote; |
|
211 |
||
212 |
char ch = peek(); |
|
16
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
213 |
|
26 | 214 |
if (ch == std::istream::traits_type::eof()) { |
215 |
break; |
|
216 |
} else if (ch == '[') { |
|
217 |
if (inSection) for (INIContentHandler* handler : handlers) handler->endSection(); |
|
218 |
inSection = true; |
|
219 |
get(); |
|
220 |
readAllWhitespace(); |
|
221 |
INIContentHandler::SectionStartEvent event; |
|
19
90f2b8ca32bf
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
222 |
event.lineNumber = lineNumber; |
90f2b8ca32bf
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
223 |
event.eventNumber = ++eventNumber; |
26 | 224 |
event.name = readTokenAndEatTerminator(']', "e, &found); |
225 |
||
226 |
readSpacesAndTabs(); |
|
227 |
if (allowSectionTags && peek() == '[') { |
|
228 |
get(); |
|
229 |
event.tag = readTokenAndEatTerminator(']', "e, &found); |
|
230 |
} |
|
231 |
||
232 |
readSpacesAndTabs(); |
|
233 |
ch = peek(); |
|
234 |
if (isComment(ch)) { |
|
235 |
get(); |
|
236 |
readSpacesAndTabs(); |
|
237 |
event.comment = readUntil('\n', &found); |
|
238 |
} else if (ch == '\n') { |
|
239 |
get(); |
|
240 |
} else { |
|
241 |
throw std::logic_error(std::string("unexpected content after the section: '") + event.name + "'"); |
|
242 |
} |
|
243 |
||
244 |
for (INIContentHandler* handler : handlers) handler->startSection(event); |
|
245 |
} else if (isComment(ch)) { |
|
246 |
get(); |
|
247 |
readSpacesAndTabs(); |
|
19
90f2b8ca32bf
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
248 |
INIContentHandler::CommentEvent event; |
90f2b8ca32bf
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
249 |
event.lineNumber = lineNumber; |
90f2b8ca32bf
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
250 |
event.eventNumber = ++eventNumber; |
26 | 251 |
event.comment = readUntil('\n', &found); |
19
90f2b8ca32bf
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
252 |
for (INIContentHandler* handler : handlers) handler->comment(event); |
26 | 253 |
} else { |
254 |
std::string fullKey = readToken('=', "e, &found); |
|
255 |
if (!found) throw std::logic_error(std::string("missing = after key: '") + fullKey + "'"); |
|
256 |
if (!quote) fullKey = trim(fullKey); |
|
257 |
readSpacesAndTabs(); |
|
258 |
||
259 |
if (quote) { |
|
260 |
ch = get(); |
|
261 |
if (ch == '=') readSpacesAndTabs(); |
|
262 |
else throw std::logic_error(std::string("missing = after quoted key: '") + fullKey + "'"); |
|
263 |
} |
|
264 |
||
265 |
std::string value = readToken('\n', "e, &found); |
|
266 |
if (!quote) value = trim(value); |
|
267 |
||
16
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
268 |
INIContentHandler::EntryEvent event; |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
269 |
event.lineNumber = lineNumber; |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
270 |
event.eventNumber = ++eventNumber; |
26 | 271 |
event.key = fullKey; |
272 |
event.fullKey = fullKey; |
|
273 |
event.value = value; |
|
25
ee70b17950bd
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
274 |
|
26 | 275 |
if (allowSubKeys) { |
276 |
std::smatch match; |
|
277 |
if (std::regex_match(fullKey, match, std::regex("([^\\[]+)\\[([^\\[]+)\\]"))) { |
|
278 |
event.key = match[1]; |
|
279 |
event.subKey = match[2]; |
|
280 |
event.fullKey = fullKey; |
|
281 |
} |
|
282 |
} |
|
283 |
||
284 |
if (quote) { |
|
285 |
readSpacesAndTabs(); |
|
286 |
ch = peek(); |
|
287 |
if (isComment(ch)) { |
|
288 |
get(); |
|
289 |
readSpacesAndTabs(); |
|
290 |
event.comment = readUntil('\n', &found); |
|
291 |
} else if (ch == '\n') { |
|
292 |
get(); |
|
293 |
} else { |
|
294 |
throw std::logic_error(std::string("unexpected content after the quoted value: key='") + fullKey + "' value='" + event.value + "'"); |
|
25
ee70b17950bd
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
295 |
} |
ee70b17950bd
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
296 |
} |
ee70b17950bd
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
297 |
|
16
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
298 |
for (INIContentHandler* handler : handlers) handler->entry(event); |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
299 |
} |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
300 |
} |
26 | 301 |
// TODO: error at the end, catch premature/unexpected EOF |
302 |
// TODO: unescape + trim values + ignore \r |
|
303 |
// TODO: count lines |
|
16
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
304 |
if (inSection) for (INIContentHandler* handler : handlers) handler->endSection(); |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
305 |
for (INIContentHandler* handler : handlers) handler->endDocument(); |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
306 |
} |
26 | 307 |
|
308 |
// General feautres: |
|
309 |
// TODO: warning/error handler |
|
310 |
// TODO: support also escaped characters |
|
311 |
// TODO: support also Java .properties and manifest.mf formats? |
|
312 |
// TODO: support also nested sections – hierarchy |
|
313 |
// TODO: support also nested keys e.g. key.sub.subsub.subsubsub=value – translate them to nested sections |
|
314 |
// TODO: support also option for alternative key-value separator (: instead of =) |
|
315 |
// TODO: support also other encodings (currently only UTF-8 is supported) |
|
316 |
// TODO: better exceptions |
|
317 |
||
318 |
// Lossless conversions: |
|
319 |
// TODO: emit also the quote style ('/"/) |
|
320 |
// TODO: emit also the comment style (;/#) ? |
|
321 |
// TODO: emit also the whitespace before key name, around =, after "values"/'values', around [sections] ? |
|
322 |
// TODO: emit also the line-end type (LF/CRLF) ? |
|
323 |
||
16
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
324 |
}; |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
325 |
|
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
326 |
INIReader* INIReader::create(std::istream& input) { |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
327 |
return new INIReaderImpl(input); |
b9a3c806468a
temporary copy INIReader.h, INIReader.cpp, INIContentHandler.h from relpipe-in-ini + XMLNameCodec.h from relpipe-in-yamltable (will be moved to alt2xml and shared)
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
328 |
} |
26 | 329 |
|
330 |
} |
|
331 |
} |
|
332 |
} |
|
333 |
} |