author | František Kučera <franta-hg@frantovo.cz> |
Thu, 26 Nov 2020 11:42:26 +0100 | |
branch | v_0 |
changeset 27 | fd669e73d39a |
parent 26 | 80e129ec3408 |
permissions | -rw-r--r-- |
26 | 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; |
|
27
fd669e73d39a
unescape also Java .properties encoding (\uXXXX)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
34 |
bool unescapeComments; |
26 | 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 |
*/ |
|
27
fd669e73d39a
unescape also Java .properties encoding (\uXXXX)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
57 |
UnescapingINIContentHandler(INIContentHandler& output, bool lastEscaphingPhase, bool unescapeComments = false) : output(output), lastEscaphingPhase(lastEscaphingPhase), unescapeComments(unescapeComments) { |
26 | 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); |
|
27
fd669e73d39a
unescape also Java .properties encoding (\uXXXX)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
71 |
if (unescapeComments) e.comment = unescape(e.comment); |
26 | 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); |
|
27
fd669e73d39a
unescape also Java .properties encoding (\uXXXX)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
85 |
if (unescapeComments) e.comment = unescape(e.comment); |
26 | 86 |
output.entry(e); |
87 |
} |
|
88 |
||
89 |
void comment(const CommentEvent& event) override { |
|
27
fd669e73d39a
unescape also Java .properties encoding (\uXXXX)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
90 |
if (unescapeComments) { |
fd669e73d39a
unescape also Java .properties encoding (\uXXXX)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
91 |
CommentEvent e = event; |
fd669e73d39a
unescape also Java .properties encoding (\uXXXX)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
92 |
e.comment = unescape(e.comment); |
fd669e73d39a
unescape also Java .properties encoding (\uXXXX)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
93 |
output.comment(e); |
fd669e73d39a
unescape also Java .properties encoding (\uXXXX)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
94 |
} else { |
fd669e73d39a
unescape also Java .properties encoding (\uXXXX)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
95 |
output.comment(event); |
fd669e73d39a
unescape also Java .properties encoding (\uXXXX)
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
96 |
} |
26 | 97 |
} |
98 |
||
99 |
void whitespace(const WhitespaceEvent& event) override { |
|
100 |
output.whitespace(event); |
|
101 |
} |
|
102 |
||
103 |
}; |
|
104 |
||
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
} |