author | František Kučera <franta-hg@frantovo.cz> |
Wed, 29 Sep 2021 19:16:33 +0200 | |
branch | v_0 |
changeset 36 | b4fbf2772cfd |
parent 29 | f0bf2755249f |
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 BackspaceUnescapingProcessor : public UnescapingProcessor { |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
32 |
private: |
29
f0bf2755249f
remove BackspaceDisorder unescaping
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
33 |
/** |
f0bf2755249f
remove BackspaceDisorder unescaping
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
34 |
* By default it is set to true, thus no unrecognized escape sequences may left after this stage. |
f0bf2755249f
remove BackspaceDisorder unescaping
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
35 |
* Setting this to false is dangerous and may lead to errors and ambiguous behavior. |
f0bf2755249f
remove BackspaceDisorder unescaping
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
36 |
* It should be used only as a last resort. |
f0bf2755249f
remove BackspaceDisorder unescaping
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
37 |
* Because both "\\ \xxx" and "\ \xxx" will be converted to "\ \xxx" and the information will be lost. |
f0bf2755249f
remove BackspaceDisorder unescaping
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
38 |
* So, it is usually better to keep the "\" escaped as "\\" and process both the escaped backspaces and unrecognized escape sequences later. |
f0bf2755249f
remove BackspaceDisorder unescaping
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
39 |
*/ |
f0bf2755249f
remove BackspaceDisorder unescaping
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
40 |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
41 |
const bool lastEscaphingPhase = true; |
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
42 |
public: |
16 | 43 |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
44 |
std::string unescape(const std::string& s, const TextType type) override { |
16 | 45 |
std::stringstream result; |
46 |
for (int i = 0, length = s.size(); i < length; i++) { |
|
47 |
char ch = s[i]; |
|
48 |
if (i + 1 < length && ch == ESC) { |
|
49 |
ch = s[i + 1]; |
|
27
e9aad9dd823a
configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
50 |
if (ch == ESC) put(result, ESC, i); // unescape \\ to \. |
16 | 51 |
else if (lastEscaphingPhase) throw std::logic_error(std::string("Unsupported escape sequence: ") + ch); |
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 |
} |
|
61 |
||
62 |
}; |
|
63 |
||
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
} |