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 bool unescapeComments; |
|
35 |
|
36 protected: |
|
37 const char ESC = '\\'; |
|
38 bool lastEscaphingPhase; |
|
39 |
|
40 std::stringstream& put(std::stringstream& result, const char& ch, int& i) { |
|
41 result.put(ch); |
|
42 i++; |
|
43 return result; |
|
44 } |
|
45 |
|
46 virtual std::string unescape(const std::string& s) = 0; |
|
47 |
|
48 public: |
|
49 |
|
50 /** |
|
51 * @param output here will be sent events with unescaped values |
|
52 * @param lastEscaphingPhase instances of UnescapingINIContentHandler might be chained: |
|
53 * unsupported escaping sequences are kept untouched to be processed in further phases; |
|
54 * in the last phase, all remaining sequences (including \\) must be recognized and unescaped |
|
55 * (otherwise the input is considered invalid and an exception is thrown) |
|
56 */ |
|
57 UnescapingINIContentHandler(INIContentHandler& output, bool lastEscaphingPhase, bool unescapeComments = false) : output(output), lastEscaphingPhase(lastEscaphingPhase), unescapeComments(unescapeComments) { |
|
58 } |
|
59 |
|
60 void startDocument() override { |
|
61 output.startDocument(); |
|
62 } |
|
63 |
|
64 void endDocument() override { |
|
65 output.endDocument(); |
|
66 } |
|
67 |
|
68 void startSection(const SectionStartEvent& event) override { |
|
69 SectionStartEvent e = event; |
|
70 e.name = unescape(e.name); |
|
71 if (unescapeComments) e.comment = unescape(e.comment); |
|
72 output.startSection(e); |
|
73 } |
|
74 |
|
75 void endSection() override { |
|
76 output.endSection(); |
|
77 } |
|
78 |
|
79 void entry(const EntryEvent& event) override { |
|
80 EntryEvent e = event; |
|
81 e.key = unescape(e.key); |
|
82 e.fullKey = unescape(e.fullKey); |
|
83 e.subKey = unescape(e.subKey); |
|
84 e.value = unescape(e.value); |
|
85 if (unescapeComments) e.comment = unescape(e.comment); |
|
86 output.entry(e); |
|
87 } |
|
88 |
|
89 void comment(const CommentEvent& event) override { |
|
90 if (unescapeComments) { |
|
91 CommentEvent e = event; |
|
92 e.comment = unescape(e.comment); |
|
93 output.comment(e); |
|
94 } else { |
|
95 output.comment(event); |
|
96 } |
|
97 } |
|
98 |
|
99 void whitespace(const WhitespaceEvent& event) override { |
|
100 output.whitespace(event); |
|
101 } |
|
102 |
|
103 }; |
|
104 |
|
105 } |
|
106 } |
|
107 } |
|
108 } |
|