author | František Kučera <franta-hg@frantovo.cz> |
Sat, 28 Nov 2020 18:10:47 +0100 | |
branch | v_0 |
changeset 27 | e9aad9dd823a |
parent 16 | src/lib/BasicUnescapingINIHandler.h@db994a2ddffa |
child 33 | 3b81fbeb5f3b |
permissions | -rw-r--r-- |
16 | 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 |
||
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
21 |
#include "UnescapingProcessor.h" |
16 | 22 |
|
23 |
using namespace std; |
|
24 |
using namespace relpipe::writer; |
|
25 |
||
26 |
namespace relpipe { |
|
27 |
namespace in { |
|
28 |
namespace ini { |
|
29 |
namespace lib { |
|
30 |
||
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
31 |
class BasicUnescapingProcessor : public UnescapingProcessor { |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
32 |
public: |
16 | 33 |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
34 |
std::string unescape(const std::string& s, const TextType type) override { |
16 | 35 |
std::stringstream result; |
36 |
for (int i = 0, length = s.size(); i < length; i++) { |
|
37 |
char ch = s[i]; |
|
38 |
if (i + 1 < length && ch == ESC) { |
|
39 |
ch = s[i + 1]; |
|
40 |
if (ch == 'n') put(result, '\n', i); |
|
41 |
else if (ch == 'r') put(result, '\r', i); |
|
42 |
else if (ch == 't') put(result, '\t', i); |
|
43 |
else if (ch == 's') put(result, ' ', i); // TODO: Reconsider what is „basic“ escaping and should be supported. |
|
44 |
else if (ch == '"') put(result, ch, i); // The delimiters (\n,]",') are already unescaped during the first stage in the INIReader while parsing (the delimiter relevant to given environment is unescaped, e.g. \" in "quoted" value). |
|
45 |
else if (ch == '\'') put(result, ch, i); // So it does not necessary to do it here. But someone might write a="xxx\'zzz" however it is superfluous because a="xxx'zzz" will also work. |
|
46 |
else if (ch == ']') put(result, ch, i); |
|
47 |
else if (ch == ':') put(result, ch, i); |
|
48 |
else if (ch == ';') put(result, ch, i); |
|
49 |
else if (ch == '#') put(result, ch, i); |
|
50 |
else if (ch == '=') put(result, ch, i); |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
51 |
else if (ch == ESC) put(result, ESC, i).put(ESC); // copy and skip even the second \ to avoid its misinterpretation in the next cycle |
16 | 52 |
else result.put(ESC); // keep the escape sequence for later unescaping phase |
53 |
} else if (ch == ESC) { |
|
54 |
throw std::logic_error(std::string("Missing escape sequence")); // this should not happen |
|
55 |
} else { |
|
56 |
result.put(ch); |
|
57 |
} |
|
58 |
} |
|
59 |
return result.str(); |
|
60 |
} |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
61 |
|
16 | 62 |
}; |
63 |
||
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
} |