src/INICommand.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 23 Nov 2020 16:25:57 +0100
branchv_0
changeset 6 fb717cfbfea1
parent 5 d70ea23682aa
child 7 95b21edc9519
permissions -rw-r--r--
improved support for comments and whitespace

/**
 * 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/>.
 */
#include <cstdlib>
#include <vector>
#include <sstream>
#include <memory>

#include <relpipe/writer/RelationalWriter.h>
#include <relpipe/writer/RelpipeWriterException.h>
#include <relpipe/writer/AttributeMetadata.h>
#include <relpipe/writer/TypeId.h>

#include <relpipe/cli/CLI.h>

#include "INICommand.h"
#include "lib/INIReader.h"

using namespace std;
using namespace relpipe::writer;

namespace relpipe {
namespace in {
namespace ini {

class FlatINIContentHandler : public INIContentHandler {
private:
	wstring_convert < codecvt_utf8<wchar_t>> convertor; // INI parser works with UTF-8
	std::shared_ptr<writer::RelationalWriter> writer;
	Configuration& configuration;
	std::vector<std::string> currentSection;

	std::string getCurrentSectionFullName() {
		std::stringstream result;

		// TODO: configurable hierarchy delimiter
		// TODO: escape delimiter characters that are part of the section names
		for (int i = 0; i < currentSection.size(); i++) {
			if (i > 0)result << "/";
			result << currentSection[i];
		}

		return result.str();
	}

public:

	FlatINIContentHandler(std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) : writer(writer), configuration(configuration) {
	}

	virtual ~FlatINIContentHandler() {
	}

	void startDocument() override {
		vector<AttributeMetadata> metadata;
		if (configuration.enableLineNumbers) metadata.push_back({L"line", TypeId::INTEGER});
		if (configuration.enableEventNumbers) metadata.push_back({L"event", TypeId::INTEGER});
		if (configuration.enableSections) metadata.push_back({L"section", TypeId::STRING});
		metadata.push_back({L"key", TypeId::STRING});
		if (configuration.enableSubKeys) metadata.push_back({L"sub_key", TypeId::STRING});
		metadata.push_back({L"value", TypeId::STRING});
		if (configuration.enableComments) metadata.push_back({L"comment", TypeId::STRING});
		if (configuration.enableWhitespace) metadata.push_back({L"whitespace", TypeId::STRING});
		writer->startRelation(configuration.relation, metadata, true);
	};

	void endDocument() override {
		currentSection.clear();
	};

	void startSection(const SectionStartEvent& event) override {
		currentSection.push_back(event.name);
	};

	void endSection() override {
		currentSection.pop_back();
	};

	void entry(const EntryEvent& event) override {
		if (configuration.enableLineNumbers) writer->writeAttribute(&event.lineNumber, typeid (event.lineNumber));
		if (configuration.enableEventNumbers) writer->writeAttribute(&event.eventNumber, typeid (event.eventNumber));

		std::string section = getCurrentSectionFullName();
		std::string key = configuration.enableSubKeys ? event.key : event.fullKey;

		if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section));
		else if (section.size()) key = section + "/" + key;
		writer->writeAttribute(convertor.from_bytes(key));
		if (configuration.enableSubKeys) writer->writeAttribute(convertor.from_bytes(event.subKey));

		writer->writeAttribute(convertor.from_bytes(event.value));
		if (configuration.enableComments) writer->writeAttribute(convertor.from_bytes(event.comment));
		if (configuration.enableWhitespace) writer->writeAttribute(L"");
	};

	void comment(const CommentEvent& event) override {
		if (configuration.enableComments) {
			if (configuration.enableLineNumbers) writer->writeAttribute(&event.lineNumber, typeid (event.lineNumber));
			if (configuration.enableEventNumbers) writer->writeAttribute(&event.eventNumber, typeid (event.eventNumber));

			std::string section = getCurrentSectionFullName();
			std::string key;

			if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section));
			else if (section.size()) key = section + "/";
			writer->writeAttribute(convertor.from_bytes(key));

			if (configuration.enableSubKeys) writer->writeAttribute(L"");
			writer->writeAttribute(L""); // value
			writer->writeAttribute(convertor.from_bytes(event.comment));
			if (configuration.enableWhitespace) writer->writeAttribute(L"");
		}
	}

	void whitespace(const WhitespaceEvent& event) override {
		if (configuration.enableWhitespace) {
			if (configuration.enableLineNumbers) writer->writeAttribute(&event.lineNumber, typeid (event.lineNumber));
			if (configuration.enableEventNumbers) writer->writeAttribute(&event.eventNumber, typeid (event.eventNumber));

			std::string section = getCurrentSectionFullName();
			std::string key;

			if (configuration.enableSections) writer->writeAttribute(convertor.from_bytes(section));
			else if (section.size()) key = section + "/";
			writer->writeAttribute(convertor.from_bytes(key));

			if (configuration.enableSubKeys) writer->writeAttribute(L"");
			writer->writeAttribute(L""); // value
			if (configuration.enableComments) writer->writeAttribute(L"");
			writer->writeAttribute(convertor.from_bytes(event.whitespace));
		}
	}
	// TODO: unify methods, DRY

};

void INICommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
	FlatINIContentHandler handler(writer, configuration);
	std::shared_ptr<INIReader> reader(INIReader::create(input));
	reader->addHandler(&handler);
	reader->process();

}

}
}
}