|
1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, version 3 of the License. |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 */ |
|
17 #pragma once |
|
18 |
|
19 #include <sstream> |
|
20 |
|
21 #include "INIReader.h" |
|
22 |
|
23 using namespace std; |
|
24 using namespace relpipe::writer; |
|
25 |
|
26 namespace relpipe { |
|
27 namespace in { |
|
28 namespace ini { |
|
29 namespace lib { |
|
30 |
|
31 class UnescapingINIContentHandler : public INIContentHandler { |
|
32 private: |
|
33 INIContentHandler& output; |
|
34 |
|
35 protected: |
|
36 const char ESC = '\\'; |
|
37 bool lastEscaphingPhase; |
|
38 |
|
39 std::stringstream& put(std::stringstream& result, const char& ch, int& i) { |
|
40 result.put(ch); |
|
41 i++; |
|
42 return result; |
|
43 } |
|
44 |
|
45 virtual std::string unescape(const std::string& s) = 0; |
|
46 |
|
47 public: |
|
48 |
|
49 /** |
|
50 * @param output here will be sent events with unescaped values |
|
51 * @param lastEscaphingPhase instances of UnescapingINIContentHandler might be chained: |
|
52 * unsupported escaping sequences are kept untouched to be processed in further phases; |
|
53 * in the last phase, all remaining sequences (including \\) must be recognized and unescaped |
|
54 * (otherwise the input is considered invalid and an exception is thrown) |
|
55 */ |
|
56 UnescapingINIContentHandler(INIContentHandler& output, bool lastEscaphingPhase) : output(output), lastEscaphingPhase(lastEscaphingPhase) { |
|
57 } |
|
58 |
|
59 void startDocument() override { |
|
60 output.startDocument(); |
|
61 } |
|
62 |
|
63 void endDocument() override { |
|
64 output.endDocument(); |
|
65 } |
|
66 |
|
67 void startSection(const SectionStartEvent& event) override { |
|
68 SectionStartEvent e = event; |
|
69 e.name = unescape(e.name); |
|
70 output.startSection(e); |
|
71 } |
|
72 |
|
73 void endSection() override { |
|
74 output.endSection(); |
|
75 } |
|
76 |
|
77 void entry(const EntryEvent& event) override { |
|
78 EntryEvent e = event; |
|
79 e.key = unescape(e.key); |
|
80 e.fullKey = unescape(e.fullKey); |
|
81 e.subKey = unescape(e.subKey); |
|
82 e.value = unescape(e.value); |
|
83 output.entry(e); |
|
84 } |
|
85 |
|
86 void comment(const CommentEvent& event) override { |
|
87 output.comment(event); |
|
88 } |
|
89 |
|
90 void whitespace(const WhitespaceEvent& event) override { |
|
91 output.whitespace(event); |
|
92 } |
|
93 |
|
94 }; |
|
95 |
|
96 } |
|
97 } |
|
98 } |
|
99 } |