author | František Kučera <franta-hg@frantovo.cz> |
Sat, 28 Nov 2020 18:14:15 +0100 | |
branch | v_0 |
changeset 28 | 0e7c57d48d1e |
parent 26 | 80e129ec3408 |
child 29 | 06aaad12c207 |
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 |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
35 |
class ConfiguredUnescapingProcessor { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
36 |
public: |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
37 |
std::shared_ptr<UnescapingProcessor> processor; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
38 |
const std::string uri; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
39 |
bool enbaled; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
40 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
41 |
ConfiguredUnescapingProcessor(std::shared_ptr<UnescapingProcessor> processor, const std::string uri, bool enbaled) : processor(processor), uri(uri), enbaled(enbaled) { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
42 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
43 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
44 |
}; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
45 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
46 |
std::vector<ConfiguredUnescapingProcessor> unescapingProcessors; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
47 |
|
26 | 48 |
/** |
49 |
* By default, we ignore all leading whitespace on continuing lines. |
|
50 |
* If there should be some spaces or tabs, they should be placed on the previous line before the „\“. |
|
51 |
* If a line break is desired, it should be written as \n (escaped) or the value should be quoted in " or '. |
|
52 |
* |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
53 |
* TODO: several options: |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
54 |
* - enabled, disabled |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
55 |
* - if disabled, then: keep backslash, trim backslash, escape backslash |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
56 |
* (keep requires support in some further unescaping phase, or it will cause an error) |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
57 |
* - keep or trim the line end |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
58 |
* - keep or trim the leading spaces |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
59 |
* - allow comments interleaved with continuing lines (the freaky systemd syntax) |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
60 |
* |
26 | 61 |
* Related specifications: |
62 |
* - https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Properties.html |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
63 |
* - https://www.freedesktop.org/software/systemd/man/systemd.syntax.html |
26 | 64 |
*/ |
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
65 |
bool trimLeadingSpacesOnContinuingLines = true; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
66 |
|
26 | 67 |
|
68 |
/** |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
69 |
* Some dialects or configuration files in general does not support sections. |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
70 |
* Then a line, that looks like an INI section, should be interpreted as a key |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
71 |
* (or error, if does not have a proper key-value separator). |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
72 |
*/ |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
73 |
bool allowSections = true; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
74 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
75 |
/** |
26 | 76 |
* KDE uses some weird INI dialect that allows [section][x] syntax where „x“ is kind of „tag“ that signalizes some properties of given section. |
77 |
* Line „[section_1][$i]“ means that the „section_1“ is „locked“. |
|
78 |
* We may emit this information somehow later, but for now, it is just ignored. |
|
79 |
* |
|
80 |
* TODO: Is „section tag“ right name? |
|
81 |
* |
|
82 |
* Related specifications: |
|
83 |
* - https://userbase.kde.org/KDE_System_Administration/Configuration_Files#Lock_Down |
|
84 |
*/ |
|
85 |
bool allowSectionTags = true; |
|
86 |
||
87 |
/** |
|
88 |
* If whole key is „aaa[bbb]“ then „aaa“ is considered to be the key and „bbb“ the sub-key. |
|
89 |
* No \[ escaping is currently supported, so the key might not contain the bracket character. |
|
90 |
* |
|
91 |
* Related specifications: |
|
92 |
* - https://userbase.kde.org/KDE_System_Administration/Configuration_Files#Shell_Expansion |
|
93 |
* - https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s05.html |
|
94 |
*/ |
|
95 |
bool allowSubKeys = true; |
|
96 |
||
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
97 |
/** |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
98 |
* Classic INI uses „key=value“ syntax. |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
99 |
* But some other formats/dialects might use key:value. |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
100 |
* |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
101 |
* Only single character separators are supported. |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
102 |
* If multiple separators should be recognized (e.g. both „=“ and „:“), this string will contain all of them, |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
103 |
* i.e. „:=“ does not mean that the „key:=value“ syntax, but „key=value“ or „key:value“. |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
104 |
*/ |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
105 |
std::string keyValueSeparators = "="; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
106 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
107 |
/** |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
108 |
* Classic INI uses „; comment“ syntax. |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
109 |
* But many existing files contain „# comment“ lines. |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
110 |
* |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
111 |
* Only single character separators are supported (works same as keyValueSeparators). |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
112 |
*/ |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
113 |
std::string commentSeparators = ";#"; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
114 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
115 |
/** |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
116 |
* INI often support both "quotes" and 'apostrophes' styles. |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
117 |
* But some dialects may support only one of them or not support quoting at all. |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
118 |
* |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
119 |
* In such case e.g. „key="some value"“ would mean that the value is „"value"“ (including the quotes). |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
120 |
* Thus it is important to allow disabling quote recognizing (which is done by setting this parameter to empty string). |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
121 |
* |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
122 |
* Only single character quotes are supported (works same as keyValueSeparators). |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
123 |
*/ |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
124 |
std::string quotes = "\"'"; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
125 |
|
26 | 126 |
int lineNumber = 1; |
127 |
int eventNumber = 0; |
|
128 |
||
129 |
/** |
|
130 |
* Should be always used instead of input.peek(). |
|
131 |
* Skips \r. |
|
132 |
*/ |
|
133 |
char peek() { |
|
134 |
// In 2020 there is no need to manually return the carriage. However some legacy systems still do it. |
|
135 |
char ch = input.peek(); |
|
136 |
if (ch == '\r') { |
|
137 |
input.get(); |
|
138 |
ch = input.peek(); |
|
139 |
} |
|
140 |
return ch; |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* Should be always used instead of input.get(). |
|
145 |
* Counts the lines and skips \r. |
|
146 |
*/ |
|
147 |
char get() { |
|
148 |
char ch = input.get(); |
|
149 |
if (ch == '\n') lineNumber++; |
|
150 |
else if (ch == '\r') ch = get(); |
|
151 |
return ch; |
|
152 |
} |
|
153 |
||
154 |
std::string readSpacesAndTabs() { |
|
155 |
std::stringstream result; |
|
156 |
for (char ch = peek(); input.good() && (ch == ' ' || ch == '\t'); ch = peek()) result.put(get()); |
|
157 |
return result.str(); |
|
158 |
} |
|
159 |
||
160 |
std::string readAllWhitespace() { |
|
161 |
std::stringstream result; |
|
162 |
for (char ch = peek(); input.good() && (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); ch = peek()) result.put(get()); |
|
163 |
return result.str(); |
|
164 |
} |
|
165 |
||
166 |
void processContinuingLine(std::stringstream& result) { |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
167 |
if (trimLeadingSpacesOnContinuingLines) readSpacesAndTabs(); |
26 | 168 |
else result.put('\n'); |
169 |
} |
|
170 |
||
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
171 |
std::string readUntil(const char until, bool* found = nullptr) { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
172 |
return readUntil(std::string(1, until), found); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
173 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
174 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
175 |
std::string readUntil(const std::string& until, bool* found = nullptr) { |
26 | 176 |
std::stringstream result; |
177 |
||
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
178 |
for (char ch = peek(); input.good() && !oneOf(ch, until); ch = peek()) { |
26 | 179 |
if (ch == '\\') { |
180 |
get(); |
|
181 |
ch = get(); |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
182 |
if (oneOf(ch, until) && ch == '\n') processContinuingLine(result); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
183 |
else if (oneOf(ch, until)) result.put(ch); |
26 | 184 |
else if (ch == std::istream::traits_type::eof()) break; |
185 |
else result.put('\\').put(ch); |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
186 |
// unescaping is done in two phases: |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
187 |
// here we unescape just the \n (LF) |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
188 |
// other escape sequences are leaved untouched and will be processed in later phases, see see UnescapingINIHandler |
26 | 189 |
} else { |
190 |
ch = get(); |
|
191 |
result.put(ch); |
|
192 |
} |
|
193 |
} |
|
194 |
||
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
195 |
if (oneOf(peek(), until)) { |
26 | 196 |
get(); |
197 |
if (found) *found = true; |
|
198 |
} else { |
|
199 |
if (found) *found = false; |
|
200 |
} |
|
201 |
||
202 |
return result.str(); |
|
203 |
} |
|
204 |
||
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
205 |
std::string readToken(const char until, char* quote = nullptr, bool* found = nullptr) { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
206 |
return readToken(std::string(1, until), quote, found); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
207 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
208 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
209 |
std::string readToken(const std::string& until, char* quote = nullptr, bool* found = nullptr) { |
26 | 210 |
std::string result; |
211 |
||
212 |
char ch = peek(); |
|
213 |
if (isQuote(ch)) { |
|
214 |
if (quote) *quote = ch; |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
215 |
result = readUntil(std::string(1, get()), found); |
26 | 216 |
} else { |
217 |
if (quote) *quote = 0; |
|
218 |
result = readUntil(until, found); |
|
219 |
} |
|
220 |
||
221 |
return result; |
|
222 |
} |
|
223 |
||
224 |
std::string readTokenAndEatTerminator(char until, char* quote = nullptr, bool* found = nullptr) { |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
225 |
return readTokenAndEatTerminator(std::string(1, until), quote, found); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
226 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
227 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
228 |
std::string readTokenAndEatTerminator(const std::string& until, char* quote = nullptr, bool* found = nullptr) { |
26 | 229 |
std::string result = readToken(until, quote, found); |
230 |
if (*quote) { |
|
231 |
readAllWhitespace(); |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
232 |
if (!oneOf(get(), until)) throw std::logic_error(std::string("missing „") + until + "“ after quoted section name"); |
26 | 233 |
} |
234 |
return result; |
|
235 |
} |
|
236 |
||
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
237 |
std::string unescape(const std::string& value, UnescapingProcessor::TextType type) { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
238 |
std::string result = value; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
239 |
for (ConfiguredUnescapingProcessor p : unescapingProcessors) if (p.enbaled) result = p.processor->unescape(result, type); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
240 |
return result; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
241 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
242 |
|
26 | 243 |
bool isComment(char ch) { |
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
244 |
return oneOf(ch, commentSeparators); |
26 | 245 |
} |
246 |
||
247 |
bool isQuote(char ch) { |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
248 |
return oneOf(ch, quotes); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
249 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
250 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
251 |
/** |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
252 |
* @param ch character to be evaluated |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
253 |
* @param options list of options (characters) |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
254 |
* @return whether ch is one of options |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
255 |
*/ |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
256 |
bool oneOf(char ch, const std::string& options) { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
257 |
return options.find(ch) != std::string::npos; |
26 | 258 |
} |
259 |
||
260 |
std::string trim(std::string s) { |
|
261 |
return std::regex_replace(s, std::regex("^\\s+|\\s+$"), ""); |
|
262 |
} |
|
263 |
||
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
264 |
/** |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
265 |
* TODO: use a common method |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
266 |
*/ |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
267 |
bool parseBoolean(const std::string& value) { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
268 |
if (value == "true") return true; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
269 |
else if (value == "false") return false; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
270 |
else throw std::invalid_argument(std::string("Unable to parse boolean value: ") + value + " (expecting true or false)"); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
271 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
272 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
273 |
void setDialect(const std::string& name) { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
274 |
if (name == "default-ini") { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
275 |
// already set |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
276 |
} else if (name == "java-properties") { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
277 |
trimLeadingSpacesOnContinuingLines = true; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
278 |
allowSections = false; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
279 |
allowSectionTags = false; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
280 |
allowSubKeys = false; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
281 |
commentSeparators = "#"; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
282 |
keyValueSeparators = "=:"; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
283 |
quotes = ""; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
284 |
// TODO: enable unicode unescaping |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
285 |
} else { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
286 |
throw std::invalid_argument(std::string("Unsupported INI dialect: ") + name); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
287 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
288 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
289 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
290 |
bool setUnescaping(const std::string& uri, const std::string& value) { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
291 |
for (ConfiguredUnescapingProcessor& p : unescapingProcessors) { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
292 |
if (p.uri == uri) { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
293 |
p.enbaled = parseBoolean(value); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
294 |
return true; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
295 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
296 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
297 |
return false; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
298 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
299 |
|
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
|
300 |
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
|
301 |
|
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
|
302 |
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
|
303 |
} |
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 |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
305 |
void setOption(const std::string& uri, const std::string& value) override { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
306 |
if (uri == "trim-continuing-lines") trimLeadingSpacesOnContinuingLines = parseBoolean(value); // TODO: continuing lines modes (enum), not just boolean |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
307 |
else if (uri == "allow-sections") allowSections = parseBoolean(value); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
308 |
else if (uri == "allow-section-tags") allowSectionTags = parseBoolean(value); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
309 |
else if (uri == "allow-sub-keys") allowSubKeys = parseBoolean(value); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
310 |
else if (uri == "comment-separators") commentSeparators = value; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
311 |
else if (uri == "key-value-separators") keyValueSeparators = value; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
312 |
else if (uri == "quotes") quotes = value; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
313 |
else if (uri == "dialect") setDialect(value); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
314 |
else if (setUnescaping(uri, value)); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
315 |
else throw std::invalid_argument(std::string("Invalid parser option: „") + uri + "“ with value: „" + value + "“"); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
316 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
317 |
|
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
|
318 |
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
|
319 |
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
|
320 |
} |
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
|
321 |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
322 |
void addUnescapingProcessor(std::shared_ptr<UnescapingProcessor> processor, const std::string uri, bool enabledByDefault) override { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
323 |
unescapingProcessors.push_back({processor, uri, enabledByDefault}); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
324 |
} |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
325 |
|
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
|
326 |
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
|
327 |
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
|
328 |
|
26 | 329 |
bool inSection = false; |
330 |
||
331 |
while (input.good()) { // TODO: condition |
|
332 |
{ |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
333 |
INIContentHandler::WhitespaceEvent event; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
334 |
event.lineNumber = lineNumber; |
26 | 335 |
std::string whitespace = readAllWhitespace(); |
336 |
if (whitespace.size()) { |
|
337 |
event.eventNumber = ++eventNumber; |
|
338 |
event.whitespace = whitespace; |
|
339 |
for (INIContentHandler* handler : handlers) handler->whitespace(event); |
|
340 |
} |
|
341 |
} |
|
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
|
342 |
|
26 | 343 |
bool found; |
344 |
char quote; |
|
345 |
||
346 |
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
|
347 |
|
26 | 348 |
if (ch == std::istream::traits_type::eof()) { |
349 |
break; |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
350 |
} else if (ch == '[' && allowSections) { |
26 | 351 |
if (inSection) for (INIContentHandler* handler : handlers) handler->endSection(); |
352 |
inSection = true; |
|
353 |
INIContentHandler::SectionStartEvent event; |
|
19
90f2b8ca32bf
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
354 |
event.lineNumber = lineNumber; |
90f2b8ca32bf
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
355 |
event.eventNumber = ++eventNumber; |
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
356 |
get(); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
357 |
readAllWhitespace(); |
26 | 358 |
event.name = readTokenAndEatTerminator(']', "e, &found); |
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
359 |
if (!quote) event.name = trim(event.name); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
360 |
event.name = unescape(event.name, UnescapingProcessor::TextType::SectionName); |
26 | 361 |
|
362 |
readSpacesAndTabs(); |
|
363 |
if (allowSectionTags && peek() == '[') { |
|
364 |
get(); |
|
365 |
event.tag = readTokenAndEatTerminator(']', "e, &found); |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
366 |
event.tag = unescape(event.tag, UnescapingProcessor::TextType::SectionTag); |
26 | 367 |
} |
368 |
||
369 |
readSpacesAndTabs(); |
|
370 |
ch = peek(); |
|
371 |
if (isComment(ch)) { |
|
372 |
get(); |
|
373 |
readSpacesAndTabs(); |
|
374 |
event.comment = readUntil('\n', &found); |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
375 |
event.comment = unescape(event.comment, UnescapingProcessor::TextType::SectionComment); |
26 | 376 |
} else if (ch == '\n') { |
377 |
get(); |
|
378 |
} else { |
|
379 |
throw std::logic_error(std::string("unexpected content after the section: '") + event.name + "'"); |
|
380 |
} |
|
381 |
||
382 |
for (INIContentHandler* handler : handlers) handler->startSection(event); |
|
383 |
} else if (isComment(ch)) { |
|
19
90f2b8ca32bf
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
384 |
INIContentHandler::CommentEvent event; |
90f2b8ca32bf
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
385 |
event.lineNumber = lineNumber; |
90f2b8ca32bf
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
386 |
event.eventNumber = ++eventNumber; |
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
387 |
get(); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
388 |
readSpacesAndTabs(); |
26 | 389 |
event.comment = readUntil('\n', &found); |
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
390 |
event.comment = unescape(event.comment, UnescapingProcessor::TextType::Comment); |
19
90f2b8ca32bf
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
391 |
for (INIContentHandler* handler : handlers) handler->comment(event); |
26 | 392 |
} else { |
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
393 |
INIContentHandler::EntryEvent event; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
394 |
event.lineNumber = lineNumber; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
395 |
event.eventNumber = ++eventNumber; |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
396 |
|
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
397 |
std::string fullKey = readToken(keyValueSeparators, "e, &found); |
26 | 398 |
if (!found) throw std::logic_error(std::string("missing = after key: '") + fullKey + "'"); |
399 |
if (!quote) fullKey = trim(fullKey); |
|
400 |
readSpacesAndTabs(); |
|
401 |
||
402 |
if (quote) { |
|
403 |
ch = get(); |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
404 |
if (oneOf(ch, keyValueSeparators)) readSpacesAndTabs(); |
26 | 405 |
else throw std::logic_error(std::string("missing = after quoted key: '") + fullKey + "'"); |
406 |
} |
|
407 |
||
408 |
std::string value = readToken('\n', "e, &found); |
|
409 |
if (!quote) value = trim(value); |
|
410 |
||
411 |
event.key = fullKey; |
|
412 |
event.fullKey = fullKey; |
|
413 |
event.value = value; |
|
25
ee70b17950bd
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
414 |
|
26 | 415 |
if (allowSubKeys) { |
416 |
std::smatch match; |
|
417 |
if (std::regex_match(fullKey, match, std::regex("([^\\[]+)\\[([^\\[]+)\\]"))) { |
|
418 |
event.key = match[1]; |
|
419 |
event.subKey = match[2]; |
|
420 |
event.fullKey = fullKey; |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
421 |
event.subKey = unescape(event.subKey, UnescapingProcessor::TextType::EntryKey); |
26 | 422 |
} |
423 |
} |
|
424 |
||
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
425 |
event.key = unescape(event.key, UnescapingProcessor::TextType::EntryKey); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
426 |
event.fullKey = unescape(event.fullKey, UnescapingProcessor::TextType::EntryKey); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
427 |
event.value = unescape(event.value, UnescapingProcessor::TextType::EntryValue); |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
428 |
|
26 | 429 |
if (quote) { |
430 |
readSpacesAndTabs(); |
|
431 |
ch = peek(); |
|
432 |
if (isComment(ch)) { |
|
433 |
get(); |
|
434 |
readSpacesAndTabs(); |
|
435 |
event.comment = readUntil('\n', &found); |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
436 |
event.comment = unescape(event.comment, UnescapingProcessor::TextType::EntryComment); |
26 | 437 |
} else if (ch == '\n') { |
438 |
get(); |
|
439 |
} else { |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
440 |
// TODO: optional support for multiple tokens in a single entry? |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
441 |
// modes: array, concatenate |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
442 |
// some-array-1 = "item 1" "item 2" 'item 3' item 4 |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
443 |
// some-array-2 = "item 1" "item 2" 'item 3' item_4 item_5 |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
444 |
// some-bash-style-string-value = "this "will' be' concatenated → this will be concatenated |
26 | 445 |
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
|
446 |
} |
ee70b17950bd
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
447 |
} |
ee70b17950bd
multi-line support: quoted and apostrophed
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
448 |
|
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
|
449 |
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
|
450 |
} |
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
|
451 |
} |
26 | 452 |
// TODO: error at the end, catch premature/unexpected EOF |
453 |
// TODO: unescape + trim values + ignore \r |
|
454 |
// 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
|
455 |
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
|
456 |
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
|
457 |
} |
26 | 458 |
|
459 |
// General feautres: |
|
460 |
// TODO: warning/error handler |
|
461 |
// TODO: support also escaped characters |
|
462 |
// TODO: support also Java .properties and manifest.mf formats? |
|
463 |
// TODO: support also nested sections – hierarchy |
|
464 |
// TODO: support also nested keys e.g. key.sub.subsub.subsubsub=value – translate them to nested sections |
|
465 |
// TODO: support also option for alternative key-value separator (: instead of =) |
|
466 |
// TODO: support also other encodings (currently only UTF-8 is supported) |
|
467 |
// TODO: better exceptions |
|
468 |
||
469 |
// Lossless conversions: |
|
470 |
// TODO: emit also the quote style ('/"/) |
|
471 |
// TODO: emit also the comment style (;/#) ? |
|
472 |
// TODO: emit also the whitespace before key name, around =, after "values"/'values', around [sections] ? |
|
473 |
// TODO: emit also the line-end type (LF/CRLF) ? |
|
474 |
||
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
|
475 |
}; |
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
|
476 |
|
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
|
477 |
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
|
478 |
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
|
479 |
} |
26 | 480 |
|
481 |
} |
|
482 |
} |
|
483 |
} |
|
484 |
} |