src/lib/BasicUnescapingProcessor.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 13 Dec 2020 17:34:26 +0100
branchv_0
changeset 33 3b81fbeb5f3b
parent 27 e9aad9dd823a
child 36 b4fbf2772cfd
permissions -rw-r--r--
java-manifest-mf + improve unescaping
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <sstream>
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    21
#include "UnescapingProcessor.h"
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
using namespace std;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
using namespace relpipe::writer;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
namespace relpipe {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace in {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace ini {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace lib {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		std::stringstream result;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		for (int i = 0, length = s.size(); i < length; i++) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
			char ch = s[i];
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
			if (i + 1 < length && ch == ESC) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
				ch = s[i + 1];
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
				if (ch == 'n') put(result, '\n', i);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
				else if (ch == 'r') put(result, '\r', i);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
				else if (ch == 't') put(result, '\t', i);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
				else if (ch == 's') put(result, ' ', i); // TODO: Reconsider what is „basic“ escaping and should be supported.
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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).
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
3b81fbeb5f3b java-manifest-mf + improve unescaping
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    46
				else if (ch == '[') put(result, ch, i);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
				else if (ch == ']') put(result, ch, i);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
				else if (ch == ':') put(result, ch, i);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
				else if (ch == ';') put(result, ch, i);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
				else if (ch == '#') put(result, ch, i);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
				else if (ch == '=') put(result, ch, i);
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
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
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
				else result.put(ESC); // keep the escape sequence for later unescaping phase
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
			} else if (ch == ESC) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
				throw std::logic_error(std::string("Missing escape sequence")); // this should not happen
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
			} else {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
				result.put(ch);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
			}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		return result.str();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	}
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    62
	
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
};
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
}