|
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 "UnescapingProcessor.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 BackspaceUnescapingProcessor : public UnescapingProcessor { |
|
32 private: |
|
33 const bool lastEscaphingPhase = true; |
|
34 public: |
|
35 |
|
36 std::string unescape(const std::string& s, const TextType type) override { |
|
37 std::stringstream result; |
|
38 for (int i = 0, length = s.size(); i < length; i++) { |
|
39 char ch = s[i]; |
|
40 if (i + 1 < length && ch == ESC) { |
|
41 ch = s[i + 1]; |
|
42 if (ch == ESC) put(result, ESC, i); // unescape \\ to \. |
|
43 else if (lastEscaphingPhase) throw std::logic_error(std::string("Unsupported escape sequence: ") + ch); |
|
44 else result.put(ESC); // keep the escape sequence for later unescaping phase |
|
45 } else if (ch == ESC) { |
|
46 throw std::logic_error(std::string("Missing escape sequence")); // this should not happen |
|
47 } else { |
|
48 result.put(ch); |
|
49 } |
|
50 } |
|
51 return result.str(); |
|
52 } |
|
53 |
|
54 /** |
|
55 * @param lastEscaphingPhase whether this is final unescaping stage. |
|
56 * By default it is set to true, thus no unrecognized escape sequences may left after this stage. |
|
57 * Setting this to false is dangerous and may lead to errors and ambiguous behavior. |
|
58 * It should be used only as a last resort. |
|
59 * Because both "\\ \xxx" and "\ \xxx" will be converted to "\ \xxx" and the information will be lost. |
|
60 * So, it is usually better to keep the "\" escaped as "\\" and process both the escaped backspaces and unrecognized escape sequences later. |
|
61 */ |
|
62 BackspaceUnescapingProcessor(bool lastEscaphingPhase = true) : lastEscaphingPhase(lastEscaphingPhase) { |
|
63 } |
|
64 |
|
65 }; |
|
66 |
|
67 } |
|
68 } |
|
69 } |
|
70 } |