author | František Kučera <franta-hg@frantovo.cz> |
Fri, 26 Aug 2022 22:41:55 +0200 | |
branch | v_0 |
changeset 37 | d89f621951ae |
parent 33 | c9a158da6c32 |
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 |
||
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
21 |
#include "UnescapingProcessor.h" |
26 | 22 |
|
23 |
using namespace std; |
|
24 |
using namespace relpipe::writer; |
|
25 |
||
26 |
namespace relpipe { |
|
27 |
namespace in { |
|
28 |
namespace ini { |
|
29 |
namespace lib { |
|
30 |
||
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
31 |
class BasicUnescapingProcessor : public UnescapingProcessor { |
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
32 |
public: |
26 | 33 |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
34 |
std::string unescape(const std::string& s, const TextType type) override { |
26 | 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); |
|
33
c9a158da6c32
java-manifest-mf dialect for parsing MANIFEST.MF files (import shared code from relpipe-in-ini)
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
41 |
else if (ch == 'r') put(result, '\r', i); // TODO: should be 0x0d, not 0x0a like \n |
26 | 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. |
|
33
c9a158da6c32
java-manifest-mf dialect for parsing MANIFEST.MF files (import shared code from relpipe-in-ini)
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
46 |
else if (ch == '[') put(result, ch, i); |
26 | 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); |
|
51 |
else if (ch == '=') put(result, ch, i); |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
52 |
else if (ch == ESC) put(result, ESC, i).put(ESC); // copy and skip even the second \ to avoid its misinterpretation in the next cycle |
26 | 53 |
else result.put(ESC); // keep the escape sequence for later unescaping phase |
54 |
} else if (ch == ESC) { |
|
55 |
throw std::logic_error(std::string("Missing escape sequence")); // this should not happen |
|
56 |
} else { |
|
57 |
result.put(ch); |
|
58 |
} |
|
59 |
} |
|
60 |
return result.str(); |
|
61 |
} |
|
28
0e7c57d48d1e
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
62 |
|
26 | 63 |
}; |
64 |
||
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
} |