src/lib/UnescapingINIHandler.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 26 Nov 2020 11:41:55 +0100
branchv_0
changeset 18 a8c1381ef103
parent 17 4b1612d20cb2
permissions -rw-r--r--
unescape also Java .properties encoding (\uXXXX): unescape also comments
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
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include "INIReader.h"
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
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
class UnescapingINIContentHandler : public INIContentHandler {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
private:
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	INIContentHandler& output;
18
a8c1381ef103 unescape also Java .properties encoding (\uXXXX): unescape also comments
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    34
	bool unescapeComments;
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
protected:
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	const char ESC = '\\';
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	bool lastEscaphingPhase;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	std::stringstream& put(std::stringstream& result, const char& ch, int& i) {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		result.put(ch);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		i++;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		return result;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	virtual std::string unescape(const std::string& s) = 0;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
public:
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
	 * @param output here will be sent events with unescaped values
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	 * @param lastEscaphingPhase instances of UnescapingINIContentHandler might be chained:
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	 * unsupported escaping sequences are kept untouched to be processed in further phases;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	 * in the last phase, all remaining sequences (including \\) must be recognized and unescaped
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	 * (otherwise the input is considered invalid and an exception is thrown)
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	 */
18
a8c1381ef103 unescape also Java .properties encoding (\uXXXX): unescape also comments
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    57
	UnescapingINIContentHandler(INIContentHandler& output, bool lastEscaphingPhase, bool unescapeComments = false) : output(output), lastEscaphingPhase(lastEscaphingPhase), unescapeComments(unescapeComments) {
16
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
	void startDocument() override {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		output.startDocument();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	}
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
	void endDocument() override {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		output.endDocument();
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
	void startSection(const SectionStartEvent& event) override {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		SectionStartEvent e = event;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		e.name = unescape(e.name);
18
a8c1381ef103 unescape also Java .properties encoding (\uXXXX): unescape also comments
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    71
		if (unescapeComments) e.comment = unescape(e.comment);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		output.startSection(e);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	void endSection() override {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		output.endSection();
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	void entry(const EntryEvent& event) override {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		EntryEvent e = event;
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		e.key = unescape(e.key);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
		e.fullKey = unescape(e.fullKey);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
		e.subKey = unescape(e.subKey);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
		e.value = unescape(e.value);
18
a8c1381ef103 unescape also Java .properties encoding (\uXXXX): unescape also comments
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    85
		if (unescapeComments) e.comment = unescape(e.comment);
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
		output.entry(e);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	void comment(const CommentEvent& event) override {
18
a8c1381ef103 unescape also Java .properties encoding (\uXXXX): unescape also comments
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    90
		if (unescapeComments) {
a8c1381ef103 unescape also Java .properties encoding (\uXXXX): unescape also comments
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    91
			CommentEvent e = event;
a8c1381ef103 unescape also Java .properties encoding (\uXXXX): unescape also comments
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    92
			e.comment = unescape(e.comment);
a8c1381ef103 unescape also Java .properties encoding (\uXXXX): unescape also comments
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    93
			output.comment(e);
a8c1381ef103 unescape also Java .properties encoding (\uXXXX): unescape also comments
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    94
		} else {
a8c1381ef103 unescape also Java .properties encoding (\uXXXX): unescape also comments
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    95
			output.comment(event);
a8c1381ef103 unescape also Java .properties encoding (\uXXXX): unescape also comments
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    96
		}
16
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
	void whitespace(const WhitespaceEvent& event) override {
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
		output.whitespace(event);
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
	}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
};
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
}
db994a2ddffa new INI parser
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
}