src/RecfileHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 07 Apr 2019 12:49:38 +0200
branchv_0
changeset 3 b0fe316e1201
parent 2 db029ce62ec5
child 7 d59f31fa7009
permissions -rw-r--r--
string is the default type in recfile, so there is no need for %type metadata for string attributes and recfix will also stop complaining about: stdin:2: error: invalid type specification
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
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * 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
    11
 * 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
    12
 * 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
    13
 * 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
    14
 *
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * 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
    16
 * 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
    17
 */
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <regex>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <cassert>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/typedefs.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/TypeId.h>
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    31
#include <relpipe/reader/RelpipeReaderException.h>
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    32
#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
    33
#include <relpipe/reader/handlers/AttributeMetadata.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace relpipe {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace out {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace recfile {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace relpipe::reader;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
2
db029ce62ec5 fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    41
class RecfileHandler : public handlers::RelationalReaderStringHandler {
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
private:
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    43
	std::ostream& output;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
	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
    45
	std::vector<TypeId> attributeTypes;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
	std::vector<string_t> attributeTypeCodes;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    47
	std::vector<string_t> attributeNames;
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	integer_t valueCount = 0;
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
	integer_t attributeCount = 0;
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	integer_t relationCount = 0;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
	void writeRelationName(const string_t& name) {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
		// FIXME: escaping/filtering
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
		output << "%rec: " << convertor.to_bytes(name) << std::endl;
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
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
	const std::string toRecfileType(const TypeId& type) {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
		switch (type) {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
			case TypeId::BOOLEAN: return "bool";
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    60
			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
    61
			case TypeId::STRING: return "";
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
			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
    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
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
	void writeAttributeMetadata(const handlers::AttributeMetadata& attribute) {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
		// 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
    68
		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
    69
		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
    70
	}
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
	void writeSeparator() {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    73
		output << std::endl;
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
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
	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
    77
		// FIXME: escaping/filtering
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    78
		output << convertor.to_bytes(name) << ": ";
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    79
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    80
		for (char ch : convertor.to_bytes(value)) {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    81
			output << ch;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    82
			if (ch == '\n') output << "+ ";
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
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    85
		output << std::endl;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    86
	}
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    87
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
public:
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    90
	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
    91
	}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
	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
    94
		valueCount = 0;
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    95
		attributeCount = 0;
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    97
		if (relationCount) writeSeparator();
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    98
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
		relationCount++;
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   100
		writeRelationName(name);
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   102
		attributeCount = attributes.size();
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   103
		attributeTypes.resize(attributeCount);
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   104
		attributeTypeCodes.resize(attributeCount);
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   105
		attributeNames.resize(attributeCount);
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
		for (int i = 0; i < attributes.size(); i++) {
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   107
			attributeNames[i] = attributes[i].getAttributeName();
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   108
			attributeTypes[i] = attributes[i].getTypeId();
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   109
			attributeTypeCodes[i] = attributes[i].getTypeName();
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   110
			writeAttributeMetadata(attributes[i]);
0
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
	}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   114
	void attribute(const string_t& value) override {
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   115
		integer_t i = valueCount % attributeCount;
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   116
		if (i == 0) writeSeparator();
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
		valueCount++;
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   118
		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
   119
	}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
	void endOfPipe() {
1
1b4ca23e5d04 draft version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   122
		if (valueCount) writeSeparator();
0
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
}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
}