author | František Kučera <franta-hg@frantovo.cz> |
Fri, 13 Dec 2019 22:19:39 +0100 | |
branch | v_0 |
changeset 10 | c59363fd805b |
parent 8 | 9c8c20c3bd64 |
child 17 | 06c8926debe0 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
8
9c8c20c3bd64
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
0 | 8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <iostream> |
|
20 |
#include <string> |
|
21 |
#include <sstream> |
|
22 |
#include <vector> |
|
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
23 |
#include <set> |
10
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
24 |
#include <regex> |
0 | 25 |
|
26 |
#include <relpipe/writer/typedefs.h> |
|
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
27 |
#include <relpipe/writer/RelationalWriter.h> |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
28 |
#include <relpipe/writer/AttributeMetadata.h> |
0 | 29 |
|
30 |
namespace relpipe { |
|
31 |
namespace in { |
|
32 |
namespace recfile { |
|
33 |
||
34 |
using namespace relpipe::writer; |
|
35 |
||
36 |
class RecfileCommand { |
|
37 |
private: |
|
38 |
||
39 |
enum class RecfileLineType { |
|
40 |
METADATA, |
|
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
41 |
DATA, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
42 |
SEPARATOR, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
43 |
COMMENT, |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
44 |
END, |
0 | 45 |
}; |
46 |
||
47 |
class RecfileHandler { |
|
48 |
private: |
|
49 |
RelationalWriter* writer; |
|
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
50 |
string_t currentRelationName; |
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
51 |
std::vector<AttributeMetadata> currentAttributeMetadata; |
10
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
52 |
std::vector<AttributeMetadata> currentTypeHints; |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
53 |
std::vector<string_t> currentRecord; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
54 |
std::vector<std::vector<string_t>> currentRecords; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
55 |
size_t prefetchCount = 1; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
56 |
bool headerWritten = false; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
57 |
|
10
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
58 |
TypeId findType(string_t attributeName, TypeId defaultType = TypeId::STRING) { |
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
59 |
for (AttributeMetadata m : currentTypeHints) if (m.attributeName == attributeName) return m.typeId; |
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
60 |
return defaultType; |
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
61 |
} |
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
62 |
|
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
63 |
TypeId recType2typeId(string_t recType) { |
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
64 |
// TODO: support more types |
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
65 |
// boolean is currently unsupported, because NULLs are not implemented yet and recfile booleans might be null |
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
66 |
if (recType == L"int") return TypeId::INTEGER; |
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
67 |
else return TypeId::STRING; |
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
68 |
} |
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
69 |
|
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
70 |
void writeHeader() { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
71 |
if (headerWritten) return; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
72 |
|
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
73 |
if (currentRelationName.size() == 0) currentRelationName = L"recfile"; |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
74 |
|
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
75 |
std::set<string_t> uniqueAttributeNames; |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
76 |
|
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
77 |
// TODO: add also attribute names from type hints from recfile metadata |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
78 |
for (int i = 0; i < currentRecords.size(); i++) { |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
79 |
std::vector<string_t> record = currentRecords[i]; |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
80 |
for (int j = 0; j < record.size(); j += 2) { |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
81 |
if (uniqueAttributeNames.insert(record[j]).second) { |
10
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
82 |
currentAttributeMetadata.push_back({record[j], findType(record[j])}); |
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
83 |
} |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
84 |
} |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
85 |
} |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
86 |
|
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
87 |
writer->startRelation(currentRelationName, currentAttributeMetadata, true); |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
88 |
headerWritten = true; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
89 |
} |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
90 |
|
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
91 |
string_t findValue(std::vector<string_t>& record, TypeId type, string_t& name) { |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
92 |
for (int j = 0; j < record.size(); j += 2) if (record[j] == name) return record[j + 1]; |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
93 |
return L""; // TODO: proper empty/null value for given type |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
94 |
} |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
95 |
|
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
96 |
void writeRecords() { |
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
97 |
for (std::vector<string_t> record : currentRecords) { |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
98 |
for (AttributeMetadata a : currentAttributeMetadata) { |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
99 |
writer->writeAttribute(findValue(record, a.typeId, a.attributeName)); |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
100 |
} |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
101 |
} |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
102 |
currentRecords.clear(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
103 |
} |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
104 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
105 |
void metadata(const string_t& name, const string_t& value) { |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
106 |
if (name == L"rec") { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
107 |
currentRelationName = value; |
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
108 |
currentAttributeMetadata.clear(); |
10
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
109 |
currentTypeHints.clear(); |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
110 |
currentRecord.clear(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
111 |
currentRecords.clear(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
112 |
headerWritten = false; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
113 |
} else if (name == L"type") { |
10
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
114 |
std::wsmatch match; |
c59363fd805b
support signed integers, negative numbers; binary format change: encode numbers as SLEB128
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
115 |
if (regex_search(value, match, std::wregex(L"\\s?(.*)\\s+(.*)\\s?"))) currentTypeHints.push_back({match[1], recType2typeId(match[2])}); |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
116 |
} else { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
117 |
// ignore – other recfile metadata like keys or auto-increments |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
118 |
} |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
119 |
} |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
120 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
121 |
void data(const string_t& name, const string_t& value) { |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
122 |
currentRecord.push_back(name); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
123 |
currentRecord.push_back(value); |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
124 |
} |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
125 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
126 |
void comment(const string_t& value) { |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
127 |
// ignore comments |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
128 |
} |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
129 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
130 |
void separator() { |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
131 |
if (currentRecord.size()) { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
132 |
currentRecords.push_back(currentRecord); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
133 |
currentRecord.clear(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
134 |
} |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
135 |
|
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
136 |
if (prefetchCount > 0 && currentRecords.size() >= prefetchCount) { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
137 |
writeHeader(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
138 |
writeRecords(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
139 |
} |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
140 |
} |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
141 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
142 |
void end() { |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
143 |
if (currentRecord.size()) currentRecords.push_back(currentRecord); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
144 |
writeHeader(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
145 |
writeRecords(); |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
146 |
} |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
147 |
|
0 | 148 |
public: |
149 |
||
150 |
RecfileHandler(RelationalWriter* writer) : writer(writer) { |
|
151 |
} |
|
152 |
||
153 |
virtual ~RecfileHandler() { |
|
154 |
} |
|
155 |
||
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
156 |
void logicalLine(RecfileLineType type, const string_t& name = L"", const string_t& value = L"") { |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
157 |
switch (type) { |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
158 |
case RecfileLineType::METADATA: return metadata(name, value); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
159 |
case RecfileLineType::DATA: return data(name, value); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
160 |
case RecfileLineType::COMMENT: return comment(value); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
161 |
case RecfileLineType::SEPARATOR: return separator(); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
162 |
case RecfileLineType::END: return end(); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
163 |
} |
0 | 164 |
} |
165 |
||
166 |
}; |
|
167 |
||
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
168 |
enum class ParserState { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
169 |
START, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
170 |
NAME, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
171 |
VALUE, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
172 |
VALUE_CONTINUATION, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
173 |
COMMENT, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
174 |
END, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
175 |
}; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
176 |
|
0 | 177 |
class RecfileParser { |
178 |
private: |
|
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
179 |
wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings or are recfiles always in UTF-8? |
0 | 180 |
RecfileHandler& handler; |
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
181 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
182 |
void emitLogicalLine(RecfileLineType& type, std::stringstream& name, std::stringstream& value) { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
183 |
handler.logicalLine(type, convertor.from_bytes(name.str()), convertor.from_bytes(value.str())); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
184 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
185 |
name.str(""); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
186 |
name.clear(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
187 |
value.str(""); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
188 |
value.clear(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
189 |
type = RecfileLineType::DATA; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
190 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
191 |
|
0 | 192 |
public: |
193 |
||
194 |
RecfileParser(RecfileHandler& handler) : handler(handler) { |
|
195 |
} |
|
196 |
||
197 |
virtual ~RecfileParser() { |
|
198 |
} |
|
199 |
||
200 |
void parse(std::istream& input) { |
|
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
201 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
202 |
ParserState state = ParserState::START; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
203 |
RecfileLineType type = RecfileLineType::DATA; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
204 |
std::stringstream name; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
205 |
std::stringstream value; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
206 |
char ch; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
207 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
208 |
while (state != ParserState::END && input.good()) { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
209 |
ch = input.get(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
210 |
if (input.eof()) continue; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
211 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
212 |
switch (state) { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
213 |
case ParserState::START: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
214 |
if (ch == '%') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
215 |
type = RecfileLineType::METADATA; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
216 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
217 |
} else if (ch == ' ') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
218 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
219 |
} else if (ch == '\n') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
220 |
handler.logicalLine(RecfileLineType::SEPARATOR); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
221 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
222 |
} else if (ch == '#') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
223 |
type = RecfileLineType::COMMENT; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
224 |
state = ParserState::COMMENT; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
225 |
if (input.get() != ' ') input.unget(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
226 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
227 |
} // else → name |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
228 |
case ParserState::NAME: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
229 |
if (ch == ':') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
230 |
state = ParserState::VALUE; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
231 |
if (input.get() != ' ') input.unget(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
232 |
} else { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
233 |
name << ch; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
234 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
235 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
236 |
case ParserState::VALUE: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
237 |
if (ch == '\n') state = ParserState::VALUE_CONTINUATION; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
238 |
else value << ch; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
239 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
240 |
case ParserState::VALUE_CONTINUATION: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
241 |
if (ch == '+') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
242 |
state = ParserState::VALUE; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
243 |
if (value.tellp()) value << '\n'; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
244 |
if (input.get() != ' ') input.unget(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
245 |
} else { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
246 |
input.unget(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
247 |
state = ParserState::START; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
248 |
emitLogicalLine(type, name, value); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
249 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
250 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
251 |
case ParserState::COMMENT: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
252 |
if (ch == '\n') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
253 |
state = ParserState::START; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
254 |
emitLogicalLine(type, name, value); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
255 |
} else { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
256 |
value << ch; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
257 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
258 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
259 |
default: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
260 |
throw RelpipeWriterException(L"Unknown ParserState: " + std::to_wstring((int) state) + L" in RecfileParser."); // TODO: better exception |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
261 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
262 |
} |
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
263 |
if (name.tellp()) emitLogicalLine(type, name, value); |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
264 |
handler.logicalLine(RecfileLineType::END); |
0 | 265 |
} |
266 |
}; |
|
267 |
||
268 |
public: |
|
269 |
||
270 |
void process(std::istream& input, std::ostream& output) { |
|
271 |
unique_ptr<RelationalWriter> writer(Factory::create(output)); |
|
272 |
RecfileHandler handler(writer.get()); |
|
273 |
RecfileParser parser(handler); |
|
274 |
parser.parse(input); |
|
275 |
} |
|
276 |
}; |
|
277 |
||
278 |
} |
|
279 |
} |
|
280 |
} |