author | František Kučera <franta-hg@frantovo.cz> |
Sun, 13 Dec 2020 17:34:26 +0100 | |
branch | v_0 |
changeset 33 | 3b81fbeb5f3b |
parent 29 | f0bf2755249f |
child 35 | 930f17f16fd7 |
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 |
#include <cstdlib> |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
#include <vector> |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <sstream> |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <memory> |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <relpipe/writer/RelationalWriter.h> |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <relpipe/writer/RelpipeWriterException.h> |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <relpipe/writer/AttributeMetadata.h> |
7
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
25 |
#include <relpipe/common/type/typedefs.h> |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
#include <relpipe/cli/CLI.h> |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
#include "INICommand.h" |
28
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
30 |
#include "lib/uri.h" |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
#include "lib/INIReader.h" |
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
32 |
#include "lib/BasicUnescapingProcessor.h" |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
33 |
#include "lib/BackspaceUnescapingProcessor.h" |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
34 |
#include "lib/JavaPropertiesUnescapingProcessor.h" |
28
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
35 |
#include "lib/JavaPropertiesDialect.h" |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
using namespace std; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
using namespace relpipe::writer; |
16 | 39 |
using namespace relpipe::in::ini::lib; |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
namespace relpipe { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
namespace in { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
namespace ini { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
class FlatINIContentHandler : public INIContentHandler { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
private: |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
wstring_convert < codecvt_utf8<wchar_t>> convertor; // INI parser works with UTF-8 |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
std::shared_ptr<writer::RelationalWriter> writer; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
Configuration& configuration; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
std::vector<std::string> currentSection; |
15
0cf8bcb9c8fc
enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
51 |
bool inDocument = false; |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
std::string getCurrentSectionFullName() { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
std::stringstream result; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
// TODO: configurable hierarchy delimiter |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
// TODO: escape delimiter characters that are part of the section names |
16 | 58 |
// TODO: hierarchical sections should be returned as an array/sequence when such data type is supported in the Relational pipes format and API |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
for (int i = 0; i < currentSection.size(); i++) { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
if (i > 0)result << "/"; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
result << currentSection[i]; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
return result.str(); |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
|
7
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
67 |
class Record { |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
68 |
public: |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
69 |
relpipe::common::type::Integer lineNumber = -1; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
70 |
relpipe::common::type::Integer eventNumber = -1; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
71 |
std::string key; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
72 |
std::string subKey; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
73 |
std::string value; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
74 |
std::string comment; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
75 |
std::string whitespace; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
76 |
|
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
77 |
Record(const Event * const event) : lineNumber(event->lineNumber), eventNumber(event->eventNumber) { |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
78 |
} |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
79 |
|
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
80 |
}; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
81 |
|
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
82 |
void write(const Record& record) { |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
83 |
if (configuration.enableLineNumbers) writer->writeAttribute(&record.lineNumber, typeid (record.lineNumber)); |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
84 |
if (configuration.enableEventNumbers) writer->writeAttribute(&record.eventNumber, typeid (record.eventNumber)); |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
85 |
|
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
86 |
std::string section = getCurrentSectionFullName(); |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
87 |
std::string key = record.key; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
88 |
if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section)); |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
89 |
else if (section.size()) key = section + "/" + record.key; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
90 |
writer->writeAttribute(convertor.from_bytes(key)); |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
91 |
if (configuration.enableSubKeys) writer->writeAttribute(convertor.from_bytes(record.subKey)); |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
92 |
|
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
93 |
writer->writeAttribute(convertor.from_bytes(record.value)); |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
94 |
if (configuration.enableComments) writer->writeAttribute(convertor.from_bytes(record.comment)); |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
95 |
if (configuration.enableWhitespace) writer->writeAttribute(convertor.from_bytes(record.whitespace)); |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
96 |
} |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
97 |
|
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
98 |
public: |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
FlatINIContentHandler(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) : writer(writer), configuration(configuration) { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
103 |
virtual ~FlatINIContentHandler() { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
104 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
105 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
106 |
void startDocument() override { |
15
0cf8bcb9c8fc
enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
107 |
if (inDocument) throw std::out_of_range("Lunatic INI parser tried to start a document without ending the previous one."); |
0cf8bcb9c8fc
enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
108 |
inDocument = true; |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
109 |
vector<AttributeMetadata> metadata; |
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
|
110 |
if (configuration.enableLineNumbers) metadata.push_back({L"line", TypeId::INTEGER}); |
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
|
111 |
if (configuration.enableEventNumbers) metadata.push_back({L"event", TypeId::INTEGER}); |
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
|
112 |
if (configuration.enableSections) metadata.push_back({L"section", TypeId::STRING}); |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
113 |
metadata.push_back({L"key", TypeId::STRING}); |
5
d70ea23682aa
rename subkey to sub_key/sub-key
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
114 |
if (configuration.enableSubKeys) metadata.push_back({L"sub_key", TypeId::STRING}); |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
115 |
metadata.push_back({L"value", TypeId::STRING}); |
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
|
116 |
if (configuration.enableComments) metadata.push_back({L"comment", TypeId::STRING}); |
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
117 |
if (configuration.enableWhitespace) metadata.push_back({L"whitespace", TypeId::STRING}); |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
118 |
writer->startRelation(configuration.relation, metadata, true); |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
119 |
}; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
120 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
121 |
void endDocument() override { |
15
0cf8bcb9c8fc
enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
122 |
if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to end a document without starting it before."); |
0cf8bcb9c8fc
enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
123 |
inDocument = false; |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
124 |
currentSection.clear(); |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
125 |
}; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
126 |
|
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
|
127 |
void startSection(const SectionStartEvent& event) override { |
15
0cf8bcb9c8fc
enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
128 |
if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to start a section without starting a document."); |
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
|
129 |
currentSection.push_back(event.name); |
8
83b23480d41f
emit also the section comments: e.g. „[section] ; comment“
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
130 |
|
83b23480d41f
emit also the section comments: e.g. „[section] ; comment“
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
131 |
if (configuration.enableComments && event.comment.size()) { |
83b23480d41f
emit also the section comments: e.g. „[section] ; comment“
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
132 |
Record record(&event); |
83b23480d41f
emit also the section comments: e.g. „[section] ; comment“
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
133 |
record.comment = event.comment; |
83b23480d41f
emit also the section comments: e.g. „[section] ; comment“
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
134 |
write(record); |
83b23480d41f
emit also the section comments: e.g. „[section] ; comment“
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
135 |
} |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
136 |
}; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
137 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
138 |
void endSection() override { |
15
0cf8bcb9c8fc
enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
139 |
if (currentSection.empty()) throw std::out_of_range("Lunatic INI parser tried to end a section without starting it before."); |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
140 |
currentSection.pop_back(); |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
141 |
}; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
142 |
|
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
|
143 |
void entry(const EntryEvent& event) override { |
15
0cf8bcb9c8fc
enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
144 |
if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to emit an entry without starting a document."); |
7
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
145 |
Record record(&event); |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
146 |
record.comment = event.comment; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
147 |
record.key = configuration.enableSubKeys ? event.key : event.fullKey; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
148 |
record.subKey = event.subKey; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
149 |
record.value = event.value; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
150 |
write(record); |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
151 |
}; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
152 |
|
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
153 |
void comment(const CommentEvent& event) override { |
15
0cf8bcb9c8fc
enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
154 |
if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to emit a comment without starting a document."); |
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
155 |
if (configuration.enableComments) { |
7
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
156 |
Record record(&event); |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
157 |
record.comment = event.comment; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
158 |
write(record); |
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
159 |
} |
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
160 |
} |
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
161 |
|
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
162 |
void whitespace(const WhitespaceEvent& event) override { |
15
0cf8bcb9c8fc
enforce the parser protocol – throw exception if violated by the parser
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
163 |
if (!inDocument) throw std::out_of_range("Lunatic INI parser tried to emit a whitespace without starting a document."); |
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
164 |
if (configuration.enableWhitespace) { |
7
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
165 |
Record record(&event); |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
166 |
record.whitespace = event.whitespace; |
95b21edc9519
simplify FlatINIContentHandler
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
167 |
write(record); |
6
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
168 |
} |
fb717cfbfea1
improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
169 |
} |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
170 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
171 |
}; |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
172 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
173 |
void INICommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) { |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
174 |
FlatINIContentHandler handler(writer, configuration); |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
175 |
std::shared_ptr<INIReader> reader(INIReader::create(input)); |
28
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
176 |
reader->addUnescapingProcessor(std::make_shared<BasicUnescapingProcessor>(), unescaping::Basic, true); |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
177 |
reader->addUnescapingProcessor(std::make_shared<JavaPropertiesUnescapingProcessor>(), unescaping::JavaProperties, false); |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
178 |
reader->addUnescapingProcessor(std::make_shared<BackspaceUnescapingProcessor>(), unescaping::Backspace, true); |
596a724fbb83
configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
179 |
reader->addDialect(std::make_shared<JavaPropertiesDialect>(), dialect::JavaProperties, false); |
33
3b81fbeb5f3b
java-manifest-mf + improve unescaping
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
180 |
reader->addDialect(std::make_shared<JavaPropertiesDialect>(), dialect::JavaManifestMF, false); |
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
181 |
reader->addHandler(&handler); |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
182 |
// TODO: smart pointers vs. references: are we going to call addUnescapingProcessor() dynamically/conditionally or share instances? Then pointers will be better. |
23
b497140b0b63
enable configuring the parser from CLI: --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
183 |
for (ParserOptionRecipe option : configuration.parserOptions) reader->setOption(convertor.to_bytes(option.uri), convertor.to_bytes(option.value)); |
0
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
184 |
reader->process(); |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
185 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
186 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
187 |
|
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
188 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
189 |
} |
16c7fa9b7c49
project and parser skeleton + output demo data
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
190 |
} |