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 "UnescapingINIHandler.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 BasicUnescapingINIContentHandler : public UnescapingINIContentHandler {
|
|
32 |
protected:
|
|
33 |
|
|
34 |
virtual std::string unescape(const std::string& s) {
|
|
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);
|
|
51 |
else if (ch == ESC && !lastEscaphingPhase) put(result, ESC, i).put(ESC); // copy and skip even the second \ to avoid its misinterpretation in the next cycle
|
|
52 |
else if (ch == ESC && lastEscaphingPhase) put(result, ESC, i); // unescape \\ to \.
|
|
53 |
else if (lastEscaphingPhase) throw std::logic_error(std::string("Unsupported escape sequence: ") + ch);
|
|
54 |
else result.put(ESC); // keep the escape sequence for later unescaping phase
|
|
55 |
} else if (ch == ESC) {
|
|
56 |
throw std::logic_error(std::string("Missing escape sequence")); // this should not happen
|
|
57 |
} else {
|
|
58 |
result.put(ch);
|
|
59 |
}
|
|
60 |
}
|
|
61 |
return result.str();
|
|
62 |
}
|
|
63 |
|
|
64 |
public:
|
|
65 |
|
|
66 |
BasicUnescapingINIContentHandler(INIContentHandler& output, bool lastEscaphingPhase) : UnescapingINIContentHandler(output, lastEscaphingPhase) {
|
|
67 |
}
|
|
68 |
|
|
69 |
};
|
|
70 |
|
|
71 |
}
|
|
72 |
}
|
|
73 |
}
|
|
74 |
}
|