src/RecfileHandler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 21:59:01 +0200
branchv_0
changeset 7 d59f31fa7009
parent 3 b0fe316e1201
child 15 e5421eea0583
permissions -rw-r--r--
fix license version: GNU GPLv3

/**
 * Relational pipes
 * Copyright © 2019 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 <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <regex>
#include <cassert>

#include <relpipe/reader/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/RelpipeReaderException.h>
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>

namespace relpipe {
namespace out {
namespace recfile {

using namespace relpipe::reader;

class RecfileHandler : public handlers::RelationalReaderStringHandler {
private:
	std::ostream& output;
	wstring_convert<codecvt_utf8<wchar_t>> convertor; // XML output will be always in UTF-8
	std::vector<TypeId> attributeTypes;
	std::vector<string_t> attributeTypeCodes;
	std::vector<string_t> attributeNames;
	integer_t valueCount = 0;
	integer_t attributeCount = 0;
	integer_t relationCount = 0;

	void writeRelationName(const string_t& name) {
		// FIXME: escaping/filtering
		output << "%rec: " << convertor.to_bytes(name) << std::endl;
	}

	const std::string toRecfileType(const TypeId& type) {
		switch (type) {
			case TypeId::BOOLEAN: return "bool";
			case TypeId::INTEGER: return "int";
			case TypeId::STRING: return "";
			default: throw RelpipeReaderException(L"Unsupported type – unable to convert to a Recfile type");
		}
	}

	void writeAttributeMetadata(const handlers::AttributeMetadata& attribute) {
		// FIXME: escaping/filtering
		std::string recfileType = toRecfileType(attribute.getTypeId());
		if (recfileType.size()) output << "%type: " << convertor.to_bytes(attribute.getAttributeName()) << " " << recfileType << std::endl;
	}

	void writeSeparator() {
		output << std::endl;
	}

	void writeAttribute(const string_t& name, const TypeId& type, const string_t& value) {
		// FIXME: escaping/filtering
		output << convertor.to_bytes(name) << ": ";

		for (char ch : convertor.to_bytes(value)) {
			output << ch;
			if (ch == '\n') output << "+ ";
		}

		output << std::endl;
	}

public:

	RecfileHandler(std::ostream& output) : output(output) {
	}

	void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
		valueCount = 0;
		attributeCount = 0;

		if (relationCount) writeSeparator();

		relationCount++;
		writeRelationName(name);

		attributeCount = attributes.size();
		attributeTypes.resize(attributeCount);
		attributeTypeCodes.resize(attributeCount);
		attributeNames.resize(attributeCount);
		for (int i = 0; i < attributes.size(); i++) {
			attributeNames[i] = attributes[i].getAttributeName();
			attributeTypes[i] = attributes[i].getTypeId();
			attributeTypeCodes[i] = attributes[i].getTypeName();
			writeAttributeMetadata(attributes[i]);
		}
	}

	void attribute(const string_t& value) override {
		integer_t i = valueCount % attributeCount;
		if (i == 0) writeSeparator();
		valueCount++;
		writeAttribute(attributeNames[i], attributeTypes[i], value);
	}

	void endOfPipe() {
		if (valueCount) writeSeparator();
	}

};

}
}
}