author | František Kučera <franta-hg@frantovo.cz> |
Tue, 22 Oct 2019 22:01:17 +0200 | |
branch | v_0 |
changeset 35 | eafffeea6a3e |
parent 33 | 5288af2e4921 |
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 |
|
35
eafffeea6a3e
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
33
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 |
||
33
5288af2e4921
namespace, formatting
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
19 |
#include <functional> |
0 | 20 |
#include <memory> |
21 |
#include <string> |
|
22 |
#include <vector> |
|
23 |
#include <iostream> |
|
24 |
#include <sstream> |
|
25 |
#include <locale> |
|
26 |
#include <codecvt> |
|
27 |
#include <regex> |
|
28 |
||
1 | 29 |
#include <unistd.h> |
30 |
#include <wait.h> |
|
31 |
#include <ext/stdio_filebuf.h> |
|
32 |
||
0 | 33 |
#include <relpipe/reader/typedefs.h> |
34 |
#include <relpipe/reader/TypeId.h> |
|
35 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
36 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
37 |
||
38 |
#include <relpipe/writer/Factory.h> |
|
39 |
||
40 |
#include "Configuration.h" |
|
28
4fdbe30d8c58
introduce AwkException
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
41 |
#include "AwkException.h" |
0 | 42 |
|
43 |
namespace relpipe { |
|
44 |
namespace tr { |
|
45 |
namespace awk { |
|
46 |
||
47 |
using namespace std; |
|
48 |
using namespace relpipe; |
|
49 |
using namespace relpipe::reader; |
|
50 |
using namespace relpipe::reader::handlers; |
|
51 |
||
5 | 52 |
/** |
53 |
* This transformation consists of three processes connected together using pipes. |
|
54 |
* |
|
55 |
* Processes: |
|
56 |
* - Parent: process RelationalReaderStringHandler events (read relational data, usually from STDIN) and pass them in the special text format to the pipe1 |
|
57 |
* - AWK: external program (/usr/bin/awk), read from the pipe1, writes to the pipe2 |
|
58 |
* - Writer: reads from the pipe2 and writes to relationalWriter (relational data, usually to STDOUT) |
|
59 |
*/ |
|
0 | 60 |
class AwkHandler : public RelationalReaderStringHandler { |
61 |
private: |
|
62 |
Configuration configuration; |
|
63 |
writer::RelationalWriter* relationalWriter; |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
64 |
std::function<void() > relationalWriterFlush; |
7
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
65 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings |
0 | 66 |
|
2 | 67 |
int awkInputWriterFD = -1; |
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
68 |
RelationConfiguration* currentRelationConfiguration = nullptr; |
7
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
69 |
std::vector<AttributeMetadata> currentReaderMetadata; |
25
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
70 |
std::vector<writer::AttributeMetadata> currentWriterMetadata; |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
71 |
std::map<string_t, string_t> currenVariablesMapping; |
7
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
72 |
integer_t currentAttributeIndex = 0; |
2 | 73 |
|
1 | 74 |
void createPipe(int& readerFD, int& writerFD) { |
75 |
int fds[2]; |
|
76 |
int result = pipe(fds); |
|
77 |
readerFD = fds[0]; |
|
78 |
writerFD = fds[1]; |
|
28
4fdbe30d8c58
introduce AwkException
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
79 |
if (result < 0) throw AwkException(L"Unable to create a pipe."); |
1 | 80 |
} |
81 |
||
82 |
void redirectFD(int oldfd, int newfd) { |
|
83 |
int result = dup2(oldfd, newfd); |
|
28
4fdbe30d8c58
introduce AwkException
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
84 |
if (result < 0) throw AwkException(L"Unable redirect FD."); |
1 | 85 |
} |
86 |
||
87 |
void closeOrThrow(int fd) { |
|
88 |
int error = close(fd); |
|
28
4fdbe30d8c58
introduce AwkException
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
89 |
if (error) throw AwkException(L"Unable to close FD: " + to_wstring(fd) + L" from PID: " + to_wstring(getpid())); |
2 | 90 |
} |
91 |
||
11
f515d14794e0
variable execvp() arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
92 |
void execp(const std::vector<std::string>& args) { |
f515d14794e0
variable execvp() arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
93 |
const char** a = new const char*[args.size() + 1]; |
f515d14794e0
variable execvp() arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
94 |
for (size_t i = 0; i < args.size(); i++) a[i] = args[i].c_str(); |
f515d14794e0
variable execvp() arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
95 |
a[args.size()] = nullptr; |
f515d14794e0
variable execvp() arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
96 |
|
f515d14794e0
variable execvp() arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
97 |
execvp(a[0], (char*const*) a); |
f515d14794e0
variable execvp() arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
98 |
|
f515d14794e0
variable execvp() arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
99 |
delete[] a; |
28
4fdbe30d8c58
introduce AwkException
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
100 |
throw AwkException(L"Unable to do execvp()."); |
11
f515d14794e0
variable execvp() arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
101 |
} |
f515d14794e0
variable execvp() arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
102 |
|
18
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
103 |
/* TODO: move to lib-cli when stable and used in other modules */ |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
104 |
void setEnv(const char * name, const string_t& value) { |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
105 |
setenv(name, convertor.to_bytes(value).c_str(), true); |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
106 |
} |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
107 |
|
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
108 |
/* TODO: move to lib-cli when stable and used in other modules */ |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
109 |
void setEnv(const char * name, std::string& value) { |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
110 |
setenv(name, value.c_str(), true); |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
111 |
} |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
112 |
|
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
113 |
/* TODO: move to lib-cli when stable and used in other modules */ |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
114 |
void setEnv(const char * name, const integer_t& value) { |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
115 |
setenv(name, to_string(value).c_str(), true); |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
116 |
} |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
117 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
118 |
void addDefinition(std::vector<std::string>& awkCliArgs, DefinitionRecipe& d) { |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
119 |
awkCliArgs.push_back("-v"); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
120 |
awkCliArgs.push_back(convertor.to_bytes(a2v(d.name) + L"=" + d.value)); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
121 |
} |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
122 |
|
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
123 |
void add(vector<AttributeMetadata>& readerAttributes, vector<writer::AttributeMetadata>& writerAttributes) { |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
124 |
for (AttributeMetadata readerAttributes : readerAttributes) |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
125 |
writerAttributes.push_back({ |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
126 |
readerAttributes.getAttributeName(), |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
127 |
relationalWriter->toTypeId(readerAttributes.getTypeName()) |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
128 |
}); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
129 |
} |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
130 |
|
2 | 131 |
void cleanUp() { |
132 |
if (awkInputWriterFD >= 0) { |
|
133 |
closeOrThrow(awkInputWriterFD); |
|
27
86d8bbc99e7b
check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
134 |
int error1; |
86d8bbc99e7b
check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
135 |
int error2; |
86d8bbc99e7b
check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
136 |
__pid_t waitPID1 = wait(&error1); |
86d8bbc99e7b
check exit codes of child processes
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
137 |
__pid_t waitPID2 = wait(&error2); |
30
5261dfd3b952
more reserved names, exception
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
138 |
if (error1 || error2) throw AwkException(L"The child process returned an error exit code."); |
2 | 139 |
awkInputWriterFD = -1; |
140 |
} |
|
7
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
141 |
|
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
142 |
currentAttributeIndex = 0; |
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
143 |
currentReaderMetadata.clear(); |
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
144 |
currentWriterMetadata.clear(); |
25
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
145 |
currenVariablesMapping.clear(); |
19
e4558df9ba2d
drop – run AWK code but generate no output for given relation
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
146 |
currentRelationConfiguration = nullptr; |
7
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
147 |
} |
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
148 |
|
25
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
149 |
void generateVariableMappings() { |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
150 |
for (AttributeMetadata m : currentReaderMetadata) currenVariablesMapping[m.getAttributeName()] = L""; |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
151 |
for (writer::AttributeMetadata m : currentWriterMetadata) currenVariablesMapping[m.attributeName] = L""; |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
152 |
for (DefinitionRecipe d : configuration.definitions) currenVariablesMapping[d.name] = L""; |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
153 |
for (DefinitionRecipe d : currentRelationConfiguration->definitions) currenVariablesMapping[d.name] = L""; |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
154 |
|
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
155 |
for (std::pair<string_t, string_t> m : currenVariablesMapping) { |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
156 |
currenVariablesMapping[m.first] = escapeAwkVariableName(m.first); |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
157 |
} |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
158 |
} |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
159 |
|
7
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
160 |
string_t a2v(const string_t& attributeName) { |
25
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
161 |
if (currenVariablesMapping.find(attributeName) != currenVariablesMapping.end()) return currenVariablesMapping[attributeName]; |
28
4fdbe30d8c58
introduce AwkException
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
162 |
else throw AwkException(L"Unable to find value in currenVariablesMapping"); |
25
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
163 |
} |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
164 |
|
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
165 |
template <typename K, typename V> bool containsValue(std::map<K, V> map, V value) { |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
166 |
for (std::pair<K, V> p : map) if (p.second == value) return true; |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
167 |
return false; |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
168 |
} |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
169 |
|
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
170 |
string_t escapeAwkVariableName(const string_t& attributeName) { |
7
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
171 |
// cat awkgram.y | awk -v FS='\\{"|",' -v ORS='|' '/static const struct token tokentab/, /\};/ { if (/^\{/) { print $2} }' |
25
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
172 |
// cat AwkHandler.h | awk -v FS=' |\\(' -v ORS='|' '/awkScript.*"function/ { print $4; }' |
30
5261dfd3b952
more reserved names, exception
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
173 |
std::wregex awkReservedNames(L"FS|OFS|NR|NF|" L"BEGIN|BEGINFILE|END|ENDFILE|adump|and|asort|asorti|atan2|bindtextdomain|break|case|close|compl|continue|cos|dcgettext|dcngettext|default|delete|do|else|eval|exit|exp|fflush|for|func|function|gensub|getline|gsub|if|in|include|index|int|intdiv0|isarray|length|load|log|lshift|match|mktime|namespace|next|nextfile|or|patsplit|print|printf|rand|return|rshift|sin|split|sprintf|sqrt|srand|stopme|strftime|strtonum|sub|substr|switch|system|systime|tolower|toupper|typeof|while|xor"); |
24
c805c968b7ed
escape reserved AWK and our names: prefix conflicting variables with _
František Kučera <franta-hg@frantovo.cz>
parents:
23
diff
changeset
|
174 |
std::wregex trReservedNames(L"_escape|_unescape|_readVariables|_writeVariables|record"); |
25
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
175 |
std::wregex badCharacters(L"[^a-zA-Z0-9_]|^([0-9])"); |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
176 |
|
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
177 |
const string_t& name = std::regex_replace(attributeName, badCharacters, L"_$1"); |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
178 |
|
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
179 |
bool badName = false; |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
180 |
badName |= regex_match(name, awkReservedNames); |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
181 |
badName |= regex_match(name, trReservedNames); |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
182 |
badName |= containsValue(currenVariablesMapping, name); |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
183 |
|
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
184 |
if (badName) return escapeAwkVariableName(L"_" + name); |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
185 |
else return name; |
7
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
186 |
} |
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
187 |
|
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
188 |
string_t escapeAwkValue(const string_t& value) { |
23
bc20d499a458
value escaping in c++
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
189 |
std::wstringstream escaped; |
bc20d499a458
value escaping in c++
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
190 |
for (wchar_t ch : value) { |
bc20d499a458
value escaping in c++
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
191 |
if (ch == '\t') escaped << L"\\t"; |
bc20d499a458
value escaping in c++
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
192 |
else if (ch == '\n') escaped << L"\\n"; |
bc20d499a458
value escaping in c++
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
193 |
else if (ch == '\\') escaped << L"\\\\"; |
bc20d499a458
value escaping in c++
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
194 |
else escaped << ch; |
bc20d499a458
value escaping in c++
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
195 |
} |
bc20d499a458
value escaping in c++
František Kučera <franta-hg@frantovo.cz>
parents:
22
diff
changeset
|
196 |
return escaped.str(); |
1 | 197 |
} |
198 |
||
20
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
199 |
void processAwkOutput(int awkOutputReaderFD) { |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
200 |
locale::global(locale("")); // needed for processing unicode texts, otherwise getline() stopped working on first line with non-ascii characters; TODO: move somewhere else? |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
201 |
|
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
202 |
__gnu_cxx::stdio_filebuf<wchar_t> awkOutputReaderBuffer(awkOutputReaderFD, std::ios::in); |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
203 |
std::wistream awkOutputReader(&awkOutputReaderBuffer); |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
204 |
|
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
205 |
if (currentRelationConfiguration->drop) { |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
206 |
for (wchar_t ch; awkOutputReader.read(&ch, 1).good();); // just eat the lines from the AWK |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
207 |
} else { |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
208 |
std::wstringstream currentValue; |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
209 |
for (wchar_t ch; awkOutputReader.read(&ch, 1).good();) { |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
210 |
if (ch == '\t' || ch == '\n') { |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
211 |
relationalWriter->writeAttribute(currentValue.str()); |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
212 |
currentValue.str(L""); |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
213 |
currentValue.clear(); |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
214 |
} else if (ch == '\\') { |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
215 |
ch = awkOutputReader.get(); |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
216 |
if (ch == 't') currentValue << L'\t'; |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
217 |
else if (ch == 'n') currentValue << L'\n'; |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
218 |
else if (ch == '\\') currentValue << L'\\'; |
28
4fdbe30d8c58
introduce AwkException
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
219 |
else throw AwkException(L"Unknown escape sequence. Only \\t, \\n and \\\\ are supported."); |
20
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
220 |
} else { |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
221 |
currentValue << ch; |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
222 |
} |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
223 |
} |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
224 |
} |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
225 |
|
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
226 |
closeOrThrow(awkOutputReaderFD); |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
227 |
} |
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
228 |
|
26
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
229 |
void debugVariableMapping(const string_t& relationName) { |
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
230 |
relationalWriter->startRelation(relationName + L".variableMapping",{ |
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
231 |
{L"attribute", writer::TypeId::STRING}, |
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
232 |
{L"variable", writer::TypeId::STRING}, |
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
233 |
}, true); |
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
234 |
|
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
235 |
for (std::pair<string_t, string_t> m : currenVariablesMapping) { |
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
236 |
relationalWriter->writeAttribute(m.first); |
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
237 |
relationalWriter->writeAttribute(m.second); |
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
238 |
} |
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
239 |
} |
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
240 |
|
0 | 241 |
public: |
242 |
||
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
243 |
/** |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
244 |
* @param relationalWriter |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
245 |
* @param relationalWriterFlush the writer must be flushed before fork() in order to |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
246 |
* avoid duplicate output (otherwise single relation might be written from two processes); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
247 |
* This is a little hack – if it stops working, we should reconnect the pipes |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
248 |
* and use the writer only from a single process and avoid its effective duplication, |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
249 |
* or use different writers for each relation (or process). |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
250 |
* @param configuration |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
251 |
*/ |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
252 |
AwkHandler(writer::RelationalWriter* relationalWriter, std::function<void() > relationalWriterFlush, Configuration& configuration) : relationalWriter(relationalWriter), relationalWriterFlush(relationalWriterFlush), configuration(configuration) { |
0 | 253 |
} |
254 |
||
255 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override { |
|
2 | 256 |
cleanUp(); |
0 | 257 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
258 |
for (int i = 0; i < configuration.relationConfigurations.size(); i++) { |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
259 |
if (regex_match(name, wregex(configuration.relationConfigurations[i].relation))) { |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
260 |
currentRelationConfiguration = &configuration.relationConfigurations[i]; |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
261 |
break; // it there are multiple matches, only the first configuration is used |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
262 |
} |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
263 |
} |
1 | 264 |
|
21
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
265 |
currentReaderMetadata = attributes; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
266 |
// TODO: move to a reusable method (or use same metadata on both reader and writer side?) |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
267 |
if (currentRelationConfiguration && currentRelationConfiguration->writerMetadata.size()) { |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
268 |
if (currentRelationConfiguration->inputAttributesPrepend) add(currentReaderMetadata, currentWriterMetadata); |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
269 |
currentWriterMetadata.insert(currentWriterMetadata.end(), currentRelationConfiguration->writerMetadata.begin(), currentRelationConfiguration->writerMetadata.end()); |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
270 |
if (currentRelationConfiguration->inputAttributesAppend) add(currentReaderMetadata, currentWriterMetadata); |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
271 |
} else { |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
272 |
add(currentReaderMetadata, currentWriterMetadata); |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
273 |
} |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
274 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
275 |
if (currentRelationConfiguration) { |
25
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
276 |
generateVariableMappings(); |
13a1e1134797
avoid collisions in AWK variable names
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
277 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
278 |
int awkInputReaderFD; |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
279 |
int awkOutputReaderFD; |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
280 |
int awkOutputWriterFD; |
1 | 281 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
282 |
createPipe(awkInputReaderFD, awkInputWriterFD); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
283 |
createPipe(awkOutputReaderFD, awkOutputWriterFD); |
1 | 284 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
285 |
relationalWriterFlush(); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
286 |
__pid_t awkPid = fork(); |
11
f515d14794e0
variable execvp() arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
287 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
288 |
if (awkPid < 0) { |
28
4fdbe30d8c58
introduce AwkException
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
289 |
throw AwkException(L"Unable to fork AWK process."); |
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
290 |
} else if (awkPid == 0) { |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
291 |
// AWK child process |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
292 |
closeOrThrow(awkInputWriterFD); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
293 |
closeOrThrow(awkOutputReaderFD); |
12
8844ebce8fb4
pass defined global variables to AWK as CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
294 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
295 |
redirectFD(awkInputReaderFD, STDIN_FILENO); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
296 |
redirectFD(awkOutputWriterFD, STDOUT_FILENO); |
11
f515d14794e0
variable execvp() arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
297 |
|
18
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
298 |
// Environment variables: |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
299 |
setEnv("relationName", name); |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
300 |
setEnv("inputAttributeCount", currentReaderMetadata.size()); |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
301 |
setEnv("outputAttributeCount", currentWriterMetadata.size()); |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
302 |
// TODO: better names? ENV vs. AWK variables? |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
303 |
for (int i = 0; i < currentReaderMetadata.size(); i++) { |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
304 |
setEnv((std::string("inputAttributeName") + std::to_string(i)).c_str(), currentReaderMetadata[i].getAttributeName()); |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
305 |
setEnv("inputAttributeType" + i, currentReaderMetadata[i].getTypeName()); |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
306 |
} |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
307 |
for (int i = 0; i < currentWriterMetadata.size(); i++) { |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
308 |
setEnv("outputAttributeName" + i, currentWriterMetadata[i].attributeName); |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
309 |
// setEnv("outputAttributeType" + i, currentWriterMetadata[i].typeId); // TODO: type? |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
310 |
} |
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
311 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
312 |
// AWK script: |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
313 |
std::wstringstream awkScript; |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
314 |
awkScript << L"BEGIN {" << std::endl; |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
315 |
awkScript << L"FS=\"\\t\";" << std::endl; |
18
2ac59242d0cb
environment variables
František Kučera <franta-hg@frantovo.cz>
parents:
17
diff
changeset
|
316 |
awkScript << L"OFS=\"\\t\";" << std::endl; |
15
ba91a464d2b3
implement --for-each, --before-records, --after-records
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
317 |
awkScript << currentRelationConfiguration->awkBeforeRecords << std::endl; |
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
318 |
awkScript << L"};" << std::endl; |
15
ba91a464d2b3
implement --for-each, --before-records, --after-records
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
319 |
awkScript << std::endl; |
1 | 320 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
321 |
awkScript << L"END {" << std::endl; |
15
ba91a464d2b3
implement --for-each, --before-records, --after-records
František Kučera <franta-hg@frantovo.cz>
parents:
13
diff
changeset
|
322 |
awkScript << currentRelationConfiguration->awkAfterRecords << std::endl; |
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
323 |
awkScript << L"};" << std::endl; |
1 | 324 |
|
29
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
325 |
awkScript << LR"AWK( |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
326 |
function _escape(value, i) { |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
327 |
result = ""; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
328 |
split(value, chars, ""); |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
329 |
for (i = 1; i <= length(chars); i++) { |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
330 |
ch = chars[i]; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
331 |
if (ch == "\\") { ch = "\\\\"; } |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
332 |
else if (ch == "\t") { ch = "\\t"; } |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
333 |
else if (ch == "\n") { ch = "\\n"; } |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
334 |
result = result ch; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
335 |
} |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
336 |
return result; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
337 |
}; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
338 |
|
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
339 |
function _unescape(value, i) { |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
340 |
result = ""; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
341 |
split(value, chars, ""); |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
342 |
for (i = 1; i <= length(chars); i++) { |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
343 |
ch = chars[i]; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
344 |
if (ch == "\\") { |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
345 |
ch = chars[++i]; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
346 |
if (ch == "\\") { ch = "\\"; } |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
347 |
else if (ch == "t") { ch = "\t"; } |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
348 |
else if (ch == "n") { ch = "\n"; } |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
349 |
else { |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
350 |
printf("Unsupported escape sequence: %s\n", ch) > "/dev/stderr"; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
351 |
exit 70; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
352 |
} |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
353 |
} |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
354 |
result = result ch; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
355 |
} |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
356 |
return result; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
357 |
}; |
b3d1a671315b
AWK escaping and unescaping functions
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
358 |
)AWK"; |
21
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
359 |
|
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
360 |
awkScript << std::endl; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
361 |
|
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
362 |
awkScript << L"function _readVariables() {" << std::endl; |
31
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
363 |
for (int i = 0; i < currentReaderMetadata.size(); i++) { |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
364 |
AttributeMetadata& a = currentReaderMetadata[i]; |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
365 |
awkScript << a2v(a.getAttributeName()); |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
366 |
if (a.getTypeId() == TypeId::INTEGER) awkScript << L"=$" << (i + 1) << L";"; |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
367 |
else if (a.getTypeId() == TypeId::BOOLEAN) awkScript << L"= $" << (i + 1) << L" == \"true\";"; |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
368 |
else awkScript << L"=_unescape($" << (i + 1) << L");"; |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
369 |
awkScript << std::endl; |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
370 |
} |
21
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
371 |
awkScript << L"};" << std::endl; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
372 |
awkScript << std::endl; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
373 |
|
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
374 |
awkScript << L"function _writeVariables() {" << std::endl; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
375 |
awkScript << L"NF=" << currentWriterMetadata.size() << ";" << std::endl; |
31
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
376 |
for (int i = 0; i < currentWriterMetadata.size(); i++) { |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
377 |
writer::AttributeMetadata& a = currentWriterMetadata[i]; |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
378 |
awkScript << L"$" << (i + 1); |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
379 |
if (a.typeId == writer::TypeId::INTEGER) awkScript << L"=" << a2v(a.attributeName) << L";"; |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
380 |
else if (a.typeId == writer::TypeId::BOOLEAN) awkScript << L"= " << a2v(a.attributeName) << L" ? \"true\" : \"false\" ;"; |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
381 |
else awkScript << L"=_escape(" << a2v(a.attributeName) << L");"; |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
382 |
awkScript << std::endl; |
64d9244ee252
integer and boolean types in AWK
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
383 |
} |
21
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
384 |
awkScript << L"};" << std::endl; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
385 |
awkScript << std::endl; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
386 |
|
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
387 |
awkScript << L"function record() {" << std::endl; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
388 |
awkScript << L"_writeVariables();" << std::endl; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
389 |
awkScript << L"print;" << std::endl; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
390 |
awkScript << L"};" << std::endl; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
391 |
awkScript << std::endl; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
392 |
|
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
393 |
awkScript << L"{ _readVariables(); }" << std::endl; // read line (input attributes) into AWK variables |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
394 |
awkScript << L"{ _writeVariables(); }" << std::endl; // write AWK variables to the line (so it matches the output attributes and can be implicitly printed without explicit record() call) |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
395 |
awkScript << std::endl; |
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
396 |
|
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
397 |
awkScript << currentRelationConfiguration->awkForEach << std::endl; // user's code – can modify variables, filter results or explicitly call record() (can generate additional records or duplicate them) |
1 | 398 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
399 |
// CLI arguments: |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
400 |
std::vector<std::string> args; |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
401 |
args.push_back("awk"); |
1 | 402 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
403 |
for (auto d : configuration.definitions) addDefinition(args, d); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
404 |
for (auto d : currentRelationConfiguration->definitions) addDefinition(args, d); |
3
e086ae6a19c3
generate (some) relational output
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
405 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
406 |
args.push_back(convertor.to_bytes(awkScript.str())); |
1 | 407 |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
408 |
// Runs AWK program found on $PATH → user can plug-in a custom implementation or a wrapper, but this can be also bit dangerous (however AWK itself is dangerous). |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
409 |
execp(args); |
1 | 410 |
} else { |
411 |
// Parent process |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
412 |
closeOrThrow(awkInputReaderFD); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
413 |
closeOrThrow(awkOutputWriterFD); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
414 |
|
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
415 |
__pid_t writerPid = fork(); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
416 |
|
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
417 |
if (writerPid < 0) { |
28
4fdbe30d8c58
introduce AwkException
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
418 |
throw AwkException(L"Unable to fork Writer process."); |
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
419 |
} else if (writerPid == 0) { |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
420 |
// Writer child process |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
421 |
closeOrThrow(awkInputWriterFD); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
422 |
|
26
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
423 |
if (currentRelationConfiguration->debugVariableMapping) debugVariableMapping(name); |
cf57e8c78492
add option: --debug-variable-mapping
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
424 |
|
19
e4558df9ba2d
drop – run AWK code but generate no output for given relation
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
425 |
if (currentRelationConfiguration->drop) { |
e4558df9ba2d
drop – run AWK code but generate no output for given relation
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
426 |
// TODO: omit whole this process and pipe AWK output to /dev/null? |
e4558df9ba2d
drop – run AWK code but generate no output for given relation
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
427 |
} else { |
21
d46a727b7965
read and write variables, generate real relation
František Kučera <franta-hg@frantovo.cz>
parents:
20
diff
changeset
|
428 |
relationalWriter->startRelation(name, currentWriterMetadata, true); |
19
e4558df9ba2d
drop – run AWK code but generate no output for given relation
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
429 |
} |
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
430 |
|
20
f937ad57351f
read and un-escape the AWK output, convert to relational attributes
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
431 |
processAwkOutput(awkOutputReaderFD); |
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
432 |
exit(0); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
433 |
} else { |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
434 |
// Parent process |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
435 |
closeOrThrow(awkOutputReaderFD); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
436 |
} |
1 | 437 |
} |
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
438 |
} else { |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
439 |
relationalWriter->startRelation(name, currentWriterMetadata, true); |
1 | 440 |
} |
441 |
||
0 | 442 |
} |
443 |
||
444 |
void attribute(const string_t& value) override { |
|
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
445 |
if (currentRelationConfiguration) { |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
446 |
currentAttributeIndex++; |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
447 |
currentAttributeIndex = currentAttributeIndex % currentReaderMetadata.size(); |
7
46db0e6e548b
pass name-value pair to AWK
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
448 |
|
17
54716ea44d52
pass only values to the awk process
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
449 |
std::string awkValue = convertor.to_bytes(escapeAwkValue(value)); |
54716ea44d52
pass only values to the awk process
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
450 |
if (currentAttributeIndex == 0) awkValue += "\n"; |
54716ea44d52
pass only values to the awk process
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
451 |
else awkValue += "\t"; |
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
452 |
|
17
54716ea44d52
pass only values to the awk process
František Kučera <franta-hg@frantovo.cz>
parents:
15
diff
changeset
|
453 |
write(awkInputWriterFD, awkValue.c_str(), awkValue.length()); |
13
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
454 |
} else { |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
455 |
relationalWriter->writeAttribute(value); |
b74001992ec3
implement --relation option (thus some relations might pass unmodified by AWK), support per-relation variables
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
456 |
} |
0 | 457 |
} |
458 |
||
459 |
void endOfPipe() { |
|
2 | 460 |
cleanUp(); |
0 | 461 |
} |
462 |
||
463 |
}; |
|
464 |
||
465 |
} |
|
466 |
} |
|
467 |
} |