author | František Kučera <franta-hg@frantovo.cz> |
Mon, 08 Apr 2019 13:37:11 +0200 | |
branch | v_0 |
changeset 5 | 12b69d6b2875 |
parent 4 | b5239b4b345b |
child 8 | 9c8c20c3bd64 |
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 |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
#pragma once |
|
19 |
||
20 |
#include <iostream> |
|
21 |
#include <string> |
|
22 |
#include <sstream> |
|
23 |
#include <vector> |
|
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
24 |
#include <set> |
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; |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
52 |
std::vector<string_t> currentRecord; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
53 |
std::vector<std::vector<string_t>> currentRecords; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
54 |
size_t prefetchCount = 1; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
55 |
bool headerWritten = false; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
56 |
|
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
57 |
void writeHeader() { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
58 |
if (headerWritten) return; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
59 |
|
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
60 |
if (currentRelationName.size() == 0) currentRelationName = L"recfile"; |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
61 |
|
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
62 |
std::set<string_t> uniqueAttributeNames; |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
63 |
|
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
64 |
// 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
|
65 |
for (int i = 0; i < currentRecords.size(); i++) { |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
66 |
std::vector<string_t> record = currentRecords[i]; |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
67 |
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
|
68 |
if (uniqueAttributeNames.insert(record[j]).second) { |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
69 |
currentAttributeMetadata.push_back({record[j], TypeId::STRING}); // TODO: type from type hints |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
70 |
} |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
71 |
} |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
72 |
} |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
73 |
|
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
74 |
writer->startRelation(currentRelationName, currentAttributeMetadata, true); |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
75 |
headerWritten = true; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
76 |
} |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
77 |
|
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
} |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
82 |
|
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
83 |
void writeRecords() { |
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
84 |
for (std::vector<string_t> record : currentRecords) { |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
85 |
for (AttributeMetadata a : currentAttributeMetadata) { |
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
86 |
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
|
87 |
} |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
88 |
} |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
89 |
currentRecords.clear(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
90 |
} |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
91 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
92 |
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
|
93 |
if (name == L"rec") { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
94 |
currentRelationName = value; |
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
95 |
currentAttributeMetadata.clear(); |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
96 |
currentRecord.clear(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
97 |
currentRecords.clear(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
98 |
headerWritten = false; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
99 |
} else if (name == L"type") { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
100 |
// TODO: save type hint |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
101 |
} else { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
102 |
// 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
|
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 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
106 |
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
|
107 |
currentRecord.push_back(name); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
108 |
currentRecord.push_back(value); |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
109 |
} |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
110 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
111 |
void comment(const string_t& value) { |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
112 |
// ignore comments |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
113 |
} |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
114 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
115 |
void separator() { |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
116 |
if (currentRecord.size()) { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
117 |
currentRecords.push_back(currentRecord); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
118 |
currentRecord.clear(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
119 |
} |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
120 |
|
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
121 |
if (prefetchCount > 0 && currentRecords.size() >= prefetchCount) { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
122 |
writeHeader(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
123 |
writeRecords(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
124 |
} |
2
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 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
127 |
void end() { |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
128 |
if (currentRecord.size()) currentRecords.push_back(currentRecord); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
129 |
writeHeader(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
130 |
writeRecords(); |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
131 |
} |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
132 |
|
0 | 133 |
public: |
134 |
||
135 |
RecfileHandler(RelationalWriter* writer) : writer(writer) { |
|
136 |
} |
|
137 |
||
138 |
virtual ~RecfileHandler() { |
|
139 |
} |
|
140 |
||
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
141 |
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
|
142 |
switch (type) { |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
143 |
case RecfileLineType::METADATA: return metadata(name, value); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
144 |
case RecfileLineType::DATA: return data(name, value); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
145 |
case RecfileLineType::COMMENT: return comment(value); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
146 |
case RecfileLineType::SEPARATOR: return separator(); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
147 |
case RecfileLineType::END: return end(); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
148 |
} |
0 | 149 |
} |
150 |
||
151 |
}; |
|
152 |
||
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
153 |
enum class ParserState { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
154 |
START, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
155 |
NAME, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
156 |
VALUE, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
157 |
VALUE_CONTINUATION, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
158 |
COMMENT, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
159 |
END, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
160 |
}; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
161 |
|
0 | 162 |
class RecfileParser { |
163 |
private: |
|
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
164 |
wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings or are recfiles always in UTF-8? |
0 | 165 |
RecfileHandler& handler; |
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
166 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
167 |
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
|
168 |
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
|
169 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
170 |
name.str(""); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
171 |
name.clear(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
172 |
value.str(""); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
173 |
value.clear(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
174 |
type = RecfileLineType::DATA; |
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 |
public: |
178 |
||
179 |
RecfileParser(RecfileHandler& handler) : handler(handler) { |
|
180 |
} |
|
181 |
||
182 |
virtual ~RecfileParser() { |
|
183 |
} |
|
184 |
||
185 |
void parse(std::istream& input) { |
|
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
186 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
187 |
ParserState state = ParserState::START; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
188 |
RecfileLineType type = RecfileLineType::DATA; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
189 |
std::stringstream name; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
190 |
std::stringstream value; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
191 |
char ch; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
192 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
193 |
while (state != ParserState::END && input.good()) { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
194 |
ch = input.get(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
195 |
if (input.eof()) continue; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
196 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
197 |
switch (state) { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
198 |
case ParserState::START: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
199 |
if (ch == '%') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
200 |
type = RecfileLineType::METADATA; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
201 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
202 |
} else if (ch == ' ') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
203 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
204 |
} else if (ch == '\n') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
205 |
handler.logicalLine(RecfileLineType::SEPARATOR); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
206 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
207 |
} else if (ch == '#') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
208 |
type = RecfileLineType::COMMENT; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
209 |
state = ParserState::COMMENT; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
210 |
if (input.get() != ' ') input.unget(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
211 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
212 |
} // else → name |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
213 |
case ParserState::NAME: |
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 |
state = ParserState::VALUE; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
216 |
if (input.get() != ' ') input.unget(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
217 |
} else { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
218 |
name << ch; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
219 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
220 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
221 |
case ParserState::VALUE: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
222 |
if (ch == '\n') state = ParserState::VALUE_CONTINUATION; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
223 |
else value << ch; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
224 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
225 |
case ParserState::VALUE_CONTINUATION: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
226 |
if (ch == '+') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
227 |
state = ParserState::VALUE; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
228 |
if (value.tellp()) value << '\n'; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
229 |
if (input.get() != ' ') input.unget(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
230 |
} else { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
231 |
input.unget(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
232 |
state = ParserState::START; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
233 |
emitLogicalLine(type, name, value); |
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::COMMENT: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
237 |
if (ch == '\n') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
238 |
state = ParserState::START; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
239 |
emitLogicalLine(type, name, value); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
240 |
} else { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
241 |
value << ch; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
242 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
243 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
244 |
default: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
245 |
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
|
246 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
247 |
} |
4
b5239b4b345b
first working version
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
248 |
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
|
249 |
handler.logicalLine(RecfileLineType::END); |
0 | 250 |
} |
251 |
}; |
|
252 |
||
253 |
public: |
|
254 |
||
255 |
void process(std::istream& input, std::ostream& output) { |
|
256 |
unique_ptr<RelationalWriter> writer(Factory::create(output)); |
|
257 |
RecfileHandler handler(writer.get()); |
|
258 |
RecfileParser parser(handler); |
|
259 |
parser.parse(input); |
|
260 |
} |
|
261 |
}; |
|
262 |
||
263 |
} |
|
264 |
} |
|
265 |
} |