author | František Kučera <franta-hg@frantovo.cz> |
Sun, 07 Apr 2019 01:10:31 +0200 | |
branch | v_0 |
changeset 3 | 891fe13d7397 |
parent 2 | 2390e2949a36 |
child 4 | b5239b4b345b |
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; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
51 |
std::vector<string_t> currentRecord; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
52 |
std::vector<std::vector<string_t>> currentRecords; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
53 |
size_t prefetchCount = 1; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
54 |
bool headerWritten = false; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
55 |
|
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
56 |
void writeHeader() { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
57 |
if (headerWritten) return; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
58 |
|
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
59 |
std::vector<AttributeMetadata> attributeMetadata; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
60 |
|
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
61 |
// TODO: writer->startRelation(currentRelationName, attributeMetadata, true); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
62 |
|
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
63 |
headerWritten = true; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
64 |
} |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
65 |
|
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
66 |
void writeRecords() { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
67 |
for (int i = 0; i < currentRecords.size(); i++) { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
68 |
std::vector<string_t> record = currentRecords[i]; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
69 |
std::wcerr << L"record: " << std::endl; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
70 |
for (int j = 0; j < record.size(); j += 2) { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
71 |
std::wcerr << L" '" << record[j] << L"': '" << record[j + 1] << L"'" << std::endl; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
72 |
// TODO: writer->writeAttribute() |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
73 |
} |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
74 |
} |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
75 |
currentRecords.clear(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
76 |
} |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
77 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
78 |
void metadata(const string_t& name, const string_t& value) { |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
79 |
std::wcerr << L"metadata(" << name << L", " << value << L");" << std::endl; // TODO: remove debug |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
80 |
|
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
81 |
if (name == L"rec") { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
82 |
currentRelationName = value; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
83 |
currentRecord.clear(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
84 |
currentRecords.clear(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
85 |
headerWritten = false; |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
86 |
} else if (name == L"type") { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
87 |
// TODO: save type hint |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
88 |
} else { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
89 |
// 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
|
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 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
93 |
void data(const string_t& name, const string_t& value) { |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
94 |
std::wcerr << L"data(" << name << L", " << value << L");" << std::endl; // TODO: remove debug |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
95 |
currentRecord.push_back(name); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
96 |
currentRecord.push_back(value); |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
97 |
} |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
98 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
99 |
void comment(const string_t& value) { |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
100 |
// ignore comments |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
101 |
} |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
102 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
103 |
void separator() { |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
104 |
std::wcerr << L"separator()" << std::endl; // TODO: remove debug |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
105 |
if (currentRecord.size()) { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
106 |
currentRecords.push_back(currentRecord); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
107 |
currentRecord.clear(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
108 |
} |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
109 |
|
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
110 |
if (prefetchCount > 0 && currentRecords.size() >= prefetchCount) { |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
111 |
writeHeader(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
112 |
writeRecords(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
113 |
} |
2
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 |
|
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
116 |
void end() { |
3
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
117 |
std::wcerr << L"end();" << std::endl; // TODO: remove debug |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
118 |
if (currentRecord.size()) currentRecords.push_back(currentRecord); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
119 |
writeHeader(); |
891fe13d7397
handler logic structure
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
120 |
writeRecords(); |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
121 |
} |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
122 |
|
0 | 123 |
public: |
124 |
||
125 |
RecfileHandler(RelationalWriter* writer) : writer(writer) { |
|
126 |
} |
|
127 |
||
128 |
virtual ~RecfileHandler() { |
|
129 |
} |
|
130 |
||
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
131 |
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
|
132 |
switch (type) { |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
133 |
case RecfileLineType::METADATA: return metadata(name, value); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
134 |
case RecfileLineType::DATA: return data(name, value); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
135 |
case RecfileLineType::COMMENT: return comment(value); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
136 |
case RecfileLineType::SEPARATOR: return separator(); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
137 |
case RecfileLineType::END: return end(); |
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
138 |
} |
0 | 139 |
} |
140 |
||
141 |
}; |
|
142 |
||
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
143 |
enum class ParserState { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
144 |
START, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
145 |
NAME, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
146 |
VALUE, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
147 |
VALUE_CONTINUATION, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
148 |
COMMENT, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
149 |
END, |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
150 |
}; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
151 |
|
0 | 152 |
class RecfileParser { |
153 |
private: |
|
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
154 |
wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings or are recfiles always in UTF-8? |
0 | 155 |
RecfileHandler& handler; |
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
156 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
157 |
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
|
158 |
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
|
159 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
160 |
name.str(""); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
161 |
name.clear(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
162 |
value.str(""); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
163 |
value.clear(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
164 |
type = RecfileLineType::DATA; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
165 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
166 |
|
0 | 167 |
public: |
168 |
||
169 |
RecfileParser(RecfileHandler& handler) : handler(handler) { |
|
170 |
} |
|
171 |
||
172 |
virtual ~RecfileParser() { |
|
173 |
} |
|
174 |
||
175 |
void parse(std::istream& input) { |
|
1
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
176 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
177 |
ParserState state = ParserState::START; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
178 |
RecfileLineType type = RecfileLineType::DATA; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
179 |
std::stringstream name; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
180 |
std::stringstream value; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
181 |
char ch; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
182 |
|
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
183 |
while (state != ParserState::END && input.good()) { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
184 |
ch = input.get(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
185 |
if (input.eof()) continue; |
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 |
switch (state) { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
188 |
case ParserState::START: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
189 |
if (ch == '%') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
190 |
type = RecfileLineType::METADATA; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
191 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
192 |
} else if (ch == ' ') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
193 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
194 |
} else if (ch == '\n') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
195 |
handler.logicalLine(RecfileLineType::SEPARATOR); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
196 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
197 |
} else if (ch == '#') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
198 |
type = RecfileLineType::COMMENT; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
199 |
state = ParserState::COMMENT; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
200 |
if (input.get() != ' ') input.unget(); |
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 → name |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
203 |
case ParserState::NAME: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
204 |
if (ch == ':') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
205 |
state = ParserState::VALUE; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
206 |
if (input.get() != ' ') input.unget(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
207 |
} else { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
208 |
name << ch; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
209 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
210 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
211 |
case ParserState::VALUE: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
212 |
if (ch == '\n') state = ParserState::VALUE_CONTINUATION; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
213 |
else value << ch; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
214 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
215 |
case ParserState::VALUE_CONTINUATION: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
216 |
if (ch == '+') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
217 |
state = ParserState::VALUE; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
218 |
if (value.tellp()) value << '\n'; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
219 |
if (input.get() != ' ') input.unget(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
220 |
} else { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
221 |
input.unget(); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
222 |
state = ParserState::START; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
223 |
emitLogicalLine(type, name, value); |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
224 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
225 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
226 |
case ParserState::COMMENT: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
227 |
if (ch == '\n') { |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
228 |
state = ParserState::START; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
229 |
emitLogicalLine(type, name, value); |
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 |
value << ch; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
232 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
233 |
break; |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
234 |
default: |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
235 |
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
|
236 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
237 |
} |
8dfb42e5c088
parse recfile (logical lines)
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
238 |
emitLogicalLine(type, name, value); |
2
2390e2949a36
parse recfile: separate methods
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
239 |
handler.logicalLine(RecfileLineType::END); |
0 | 240 |
} |
241 |
}; |
|
242 |
||
243 |
public: |
|
244 |
||
245 |
void process(std::istream& input, std::ostream& output) { |
|
246 |
unique_ptr<RelationalWriter> writer(Factory::create(output)); |
|
247 |
RecfileHandler handler(writer.get()); |
|
248 |
RecfileParser parser(handler); |
|
249 |
parser.parse(input); |
|
250 |
} |
|
251 |
}; |
|
252 |
||
253 |
} |
|
254 |
} |
|
255 |
} |