src/lib/UnescapingINIHandler.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 25 Nov 2020 21:50:26 +0100
branchv_0
changeset 26 80e129ec3408
child 27 fd669e73d39a
permissions -rw-r--r--
new INI parser

/**
 * Relational pipes
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <sstream>

#include "INIReader.h"

using namespace std;
using namespace relpipe::writer;

namespace relpipe {
namespace in {
namespace ini {
namespace lib {

class UnescapingINIContentHandler : public INIContentHandler {
private:
	INIContentHandler& output;

protected:
	const char ESC = '\\';
	bool lastEscaphingPhase;

	std::stringstream& put(std::stringstream& result, const char& ch, int& i) {
		result.put(ch);
		i++;
		return result;
	}

	virtual std::string unescape(const std::string& s) = 0;

public:

	/**
	 * @param output here will be sent events with unescaped values
	 * @param lastEscaphingPhase instances of UnescapingINIContentHandler might be chained:
	 * unsupported escaping sequences are kept untouched to be processed in further phases;
	 * in the last phase, all remaining sequences (including \\) must be recognized and unescaped
	 * (otherwise the input is considered invalid and an exception is thrown)
	 */
	UnescapingINIContentHandler(INIContentHandler& output, bool lastEscaphingPhase) : output(output), lastEscaphingPhase(lastEscaphingPhase) {
	}

	void startDocument() override {
		output.startDocument();
	}

	void endDocument() override {
		output.endDocument();
	}

	void startSection(const SectionStartEvent& event) override {
		SectionStartEvent e = event;
		e.name = unescape(e.name);
		output.startSection(e);
	}

	void endSection() override {
		output.endSection();
	}

	void entry(const EntryEvent& event) override {
		EntryEvent e = event;
		e.key = unescape(e.key);
		e.fullKey = unescape(e.fullKey);
		e.subKey = unescape(e.subKey);
		e.value = unescape(e.value);
		output.entry(e);
	}

	void comment(const CommentEvent& event) override {
		output.comment(event);
	}

	void whitespace(const WhitespaceEvent& event) override {
		output.whitespace(event);
	}

};

}
}
}
}