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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
9005fdd81bca project and code skeleton: text output
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
7
d59f31fa7009 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <string>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <vector>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <iostream>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <sstream>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <locale>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <codecvt>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <regex>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <cassert>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/typedefs.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/TypeId.h>
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    30
#include <relpipe/reader/RelpipeReaderException.h>
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    31
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/reader/handlers/AttributeMetadata.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
namespace relpipe {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace out {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace recfile {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
using namespace relpipe::reader;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
2
db029ce62ec5 fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    40
class RecfileHandler : public handlers::RelationalReaderStringHandler {
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
private:
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    42
	std::ostream& output;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    43
	wstring_convert<codecvt_utf8<wchar_t>> convertor; // XML output will be always in UTF-8
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
	std::vector<TypeId> attributeTypes;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    45
	std::vector<string_t> attributeTypeCodes;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
	std::vector<string_t> attributeNames;
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	integer_t valueCount = 0;
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
	integer_t attributeCount = 0;
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	integer_t relationCount = 0;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
	void writeRelationName(const string_t& name) {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
		// FIXME: escaping/filtering
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
		output << "%rec: " << convertor.to_bytes(name) << std::endl;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
	}
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    56
	const std::string toRecfileType(const TypeId& type) {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
		switch (type) {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
			case TypeId::BOOLEAN: return "bool";
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
			case TypeId::INTEGER: return "int";
3
b0fe316e1201 string is the default type in recfile, so there is no need for %type metadata for string attributes
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    60
			case TypeId::STRING: return "";
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
			default: throw RelpipeReaderException(L"Unsupported type – unable to convert to a Recfile type");
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
		}
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
	}
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
	void writeAttributeMetadata(const handlers::AttributeMetadata& attribute) {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
		// FIXME: escaping/filtering
3
b0fe316e1201 string is the default type in recfile, so there is no need for %type metadata for string attributes
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    67
		std::string recfileType = toRecfileType(attribute.getTypeId());
b0fe316e1201 string is the default type in recfile, so there is no need for %type metadata for string attributes
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    68
		if (recfileType.size()) output << "%type: " << convertor.to_bytes(attribute.getAttributeName()) << " " << recfileType << std::endl;
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
	}
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
	void writeSeparator() {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
		output << std::endl;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    73
	}
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    74
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    75
	void writeAttribute(const string_t& name, const TypeId& type, const string_t& value) {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
		// FIXME: escaping/filtering
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    77
		output << convertor.to_bytes(name) << ": ";
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    78
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    79
		for (char ch : convertor.to_bytes(value)) {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    80
			output << ch;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    81
			if (ch == '\n') output << "+ ";
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    82
		}
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    83
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    84
		output << std::endl;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    85
	}
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    86
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
public:
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    89
	RecfileHandler(std::ostream& output) : output(output) {
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
	void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		valueCount = 0;
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    94
		attributeCount = 0;
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    96
		if (relationCount) writeSeparator();
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    97
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
		relationCount++;
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    99
		writeRelationName(name);
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   101
		attributeCount = attributes.size();
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   102
		attributeTypes.resize(attributeCount);
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   103
		attributeTypeCodes.resize(attributeCount);
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   104
		attributeNames.resize(attributeCount);
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
		for (int i = 0; i < attributes.size(); i++) {
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   106
			attributeNames[i] = attributes[i].getAttributeName();
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   107
			attributeTypes[i] = attributes[i].getTypeId();
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   108
			attributeTypeCodes[i] = attributes[i].getTypeName();
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   109
			writeAttributeMetadata(attributes[i]);
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
		}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
	}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   113
	void attribute(const string_t& value) override {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   114
		integer_t i = valueCount % attributeCount;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   115
		if (i == 0) writeSeparator();
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
		valueCount++;
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   117
		writeAttribute(attributeNames[i], attributeTypes[i], value);
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
	}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
	void endOfPipe() {
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   121
		if (valueCount) writeSeparator();
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
	}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
};
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
}