src/lib/BackspaceUnescapingProcessor.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Nov 2020 18:10:47 +0100
branchv_0
changeset 27 e9aad9dd823a
parent 16 src/lib/BasicUnescapingINIHandler.h@db994a2ddffa
child 29 f0bf2755249f
permissions -rw-r--r--
configurable unescaping processors
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 BackspaceUnescapingProcessor : public UnescapingProcessor {
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    32
private:
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    33
	const bool lastEscaphingPhase = true;
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    34
public:
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    36
	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
    37
		std::stringstream result;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		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
    39
			char ch = s[i];
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
			if (i + 1 < length && ch == ESC) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
				ch = s[i + 1];
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    42
				if (ch == ESC) put(result, ESC, i); // unescape \\ to \.
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
				else if (lastEscaphingPhase) throw std::logic_error(std::string("Unsupported escape sequence: ") + ch);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
				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
    45
			} else if (ch == ESC) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
				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
    47
			} else {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
				result.put(ch);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
			}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		return result.str();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
27
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    54
	/**
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    55
	 * @param lastEscaphingPhase whether this is final unescaping stage.
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    56
	 * By default it is set to true, thus no unrecognized escape sequences may left after this stage.
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    57
	 * Setting this to false is dangerous and may lead to errors and ambiguous behavior.
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    58
	 * It should be used only as a last resort.
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    59
	 * Because both "\\ \xxx" and "\ \xxx" will be converted to "\ \xxx" and the information will be lost.
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    60
	 * So, it is usually better to keep the "\" escaped as "\\" and process both the escaped backspaces and unrecognized escape sequences later.
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    61
	 */
e9aad9dd823a configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    62
	BackspaceUnescapingProcessor(bool lastEscaphingPhase = true) : lastEscaphingPhase(lastEscaphingPhase) {
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
}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
}