author | František Kučera <franta-hg@frantovo.cz> |
Sat, 27 Nov 2021 23:15:17 +0100 | |
branch | v_0 |
changeset 13 | 6bcc0cce7b75 |
parent 7 | d59f31fa7009 |
child 15 | e5421eea0583 |
permissions | -rw-r--r-- |
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 | 30 |
#include <relpipe/reader/RelpipeReaderException.h> |
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 | 42 |
std::ostream& output; |
43 |
wstring_convert<codecvt_utf8<wchar_t>> convertor; // XML output will be always in UTF-8 |
|
44 |
std::vector<TypeId> attributeTypes; |
|
45 |
std::vector<string_t> attributeTypeCodes; |
|
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 | 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 | 51 |
void writeRelationName(const string_t& name) { |
52 |
// FIXME: escaping/filtering |
|
53 |
output << "%rec: " << convertor.to_bytes(name) << std::endl; |
|
54 |
} |
|
55 |
||
56 |
const std::string toRecfileType(const TypeId& type) { |
|
57 |
switch (type) { |
|
58 |
case TypeId::BOOLEAN: return "bool"; |
|
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 | 61 |
default: throw RelpipeReaderException(L"Unsupported type – unable to convert to a Recfile type"); |
62 |
} |
|
63 |
} |
|
64 |
||
65 |
void writeAttributeMetadata(const handlers::AttributeMetadata& attribute) { |
|
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 | 69 |
} |
70 |
||
71 |
void writeSeparator() { |
|
72 |
output << std::endl; |
|
73 |
} |
|
74 |
||
75 |
void writeAttribute(const string_t& name, const TypeId& type, const string_t& value) { |
|
76 |
// FIXME: escaping/filtering |
|
77 |
output << convertor.to_bytes(name) << ": "; |
|
78 |
||
79 |
for (char ch : convertor.to_bytes(value)) { |
|
80 |
output << ch; |
|
81 |
if (ch == '\n') output << "+ "; |
|
82 |
} |
|
83 |
||
84 |
output << std::endl; |
|
85 |
} |
|
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 | 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 | 94 |
attributeCount = 0; |
0
9005fdd81bca
project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
|
1 | 96 |
if (relationCount) writeSeparator(); |
97 |
||
0
9005fdd81bca
project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
98 |
relationCount++; |
1 | 99 |
writeRelationName(name); |
0
9005fdd81bca
project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
|
1 | 101 |
attributeCount = attributes.size(); |
102 |
attributeTypes.resize(attributeCount); |
|
103 |
attributeTypeCodes.resize(attributeCount); |
|
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 | 106 |
attributeNames[i] = attributes[i].getAttributeName(); |
107 |
attributeTypes[i] = attributes[i].getTypeId(); |
|
108 |
attributeTypeCodes[i] = attributes[i].getTypeName(); |
|
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 | 113 |
void attribute(const string_t& value) override { |
114 |
integer_t i = valueCount % attributeCount; |
|
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 | 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 | 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 |
} |