author | František Kučera <franta-hg@frantovo.cz> |
Fri, 13 May 2022 21:35:30 +0200 | |
branch | v_0 |
changeset 96 | c34106244a54 |
parent 95 | f2f2b81079a5 |
permissions | -rw-r--r-- |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#pragma once |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <iostream> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <exception> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
#include <vector> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <string> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <sstream> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <codecvt> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
#include <locale> |
67
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
26 |
#include <regex> |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
|
69
52f837fbb216
streamlet examples: symlink to StreamletMsg.h
František Kučera <franta-hg@frantovo.cz>
parents:
67
diff
changeset
|
28 |
#include "StreamletMsg.h" |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
/** |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
* Unlike the protocol and the message format, |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
* these helper classes and functions are not part of the public API. |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
* Thus when writing custom streamlets, it is better to copy this file |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
* and review its changes while upgrading to new upstream version. |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
*/ |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
using S = relpipe::in::filesystem::StreamletMsg; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
class Streamlet { |
67
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
40 |
protected: |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
class Message { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
public: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
int code; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
std::vector<std::wstring> parameters; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
Message() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
Message(int code) : code(code) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
Message(int code, std::vector<std::wstring> parameters) : code(code), parameters(parameters) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
Message(int code, std::wstring p1) : code(code), parameters({p1}) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
Message(int code, std::wstring p1, std::wstring p2) : code(code), parameters({p1, p2}) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
}; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
|
67
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
63 |
private: |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
64 |
|
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
static const char SEPARATOR = '\0'; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
int readInt() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
return std::stoi(readString()); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
std::wstring readString() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
std::stringstream s; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
for (char ch; std::cin.read(&ch, 1).good() && ch != SEPARATOR;) s.put(ch); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
return convertor.from_bytes(s.str()); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
void writeString(std::wstring s) { |
95
f2f2b81079a5
comment on null bytes 0x00 in the Streamlet protocol
František Kučera <franta-hg@frantovo.cz>
parents:
75
diff
changeset
|
78 |
// if the value contains a null byte 0x00, it will be trimmed (passing 0x00 through would break the protocol) |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
79 |
std::cout << convertor.to_bytes(s.c_str()); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
80 |
std::cout.put(SEPARATOR); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
81 |
if (std::cout.bad()) throw std::runtime_error("Unable to write to sub-process."); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
82 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
83 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
84 |
void writeInt(int i) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
85 |
writeString(std::to_wstring(i)); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
86 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
87 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
88 |
void flush() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
89 |
std::cout.flush(); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
92 |
Message read() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
Message m; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
m.code = readInt(); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
int count = readInt(); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
for (int i = 0; i < count; i++) m.parameters.push_back(readString()); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
97 |
return m; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
98 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
|
67
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
100 |
/** |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
101 |
* The std::wsmatch contains only references to original string, |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
102 |
* so we need to copy it in order to make it persistent and independent from variables that may evaporate. |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
103 |
*/ |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
104 |
void copyMatches(std::wsmatch& source, std::vector<std::wstring>& destination) { |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
105 |
for (std::wstring s : source) destination.emplace_back(s); |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
106 |
} |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
107 |
|
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
108 |
void processMessages() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
109 |
while (true) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
110 |
Message m = read(); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
111 |
if (m.code == S::VERSION_SUPPORTED) processVersionSupported(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
112 |
else if (m.code == S::WAITING_FOR_VERSION) processWaitingForVersion(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
113 |
else if (m.code == S::RELATION_START) processRelationStart(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
114 |
else if (m.code == S::INPUT_ATTRIBUTE_METADATA) processInputAttributeMetadata(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
115 |
else if (m.code == S::OUTPUT_ATTRIBUTE_ALIAS) processOutputAttributeAlias(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
116 |
else if (m.code == S::OPTION) processOption(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
117 |
else if (m.code == S::INPUT_ATTRIBUTE) processInputAttribute(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
118 |
else if (m.code == S::WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA) processWaitingForOutputAttributesMetadata(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
119 |
else if (m.code == S::WAITING_FOR_OUTPUT_ATTRIBUTES) processWaitingForOutputAttributes(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
120 |
else if (m.code == S::RELATION_END) break; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
121 |
else processUnsupportedMessage(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
122 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
123 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
124 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
125 |
protected: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
126 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
127 |
class AttributeMetadata { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
128 |
public: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
129 |
std::wstring name; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
130 |
std::wstring type; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
131 |
}; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
132 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
133 |
class OutputAttribute { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
134 |
public: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
135 |
std::wstring value; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
136 |
bool isNull; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
137 |
}; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
138 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
139 |
class Option { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
140 |
public: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
141 |
std::wstring name; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
142 |
std::wstring value; |
67
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
143 |
std::vector<std::wstring> nameMatch; |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
144 |
std::vector<std::wstring> valueMatch; |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
145 |
|
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
146 |
Option(std::wstring name, std::wstring value) : name(name), value(value) { |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
147 |
} |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
148 |
}; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
149 |
|
75
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
150 |
private: |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
151 |
std::vector<std::wstring> versionsSupported; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
152 |
std::vector<AttributeMetadata> inputAttributes; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
153 |
std::vector<std::wstring> outputAttributeAliases; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
154 |
std::vector<Option> options; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
155 |
std::wstring currentRelation; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
156 |
std::wstring currentFile; |
75
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
157 |
std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. Or use always UTF-8 for communication with subprocesses. |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
158 |
|
75
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
159 |
protected: |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
160 |
|
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
161 |
/** |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
162 |
* n.b. generic streamlet (later in relpipe-tr-streamler) will not have currentFile |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
163 |
*/ |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
164 |
std::wstring getCurrentFile() { |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
165 |
return currentFile; |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
166 |
} |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
167 |
|
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
168 |
std::wstring getCurrentRelation() { |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
169 |
return currentRelation; |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
170 |
} |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
171 |
|
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
172 |
std::wstring fromBytes(std::string s) { |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
173 |
return convertor.from_bytes(s); |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
174 |
} |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
175 |
|
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
176 |
std::string toBytes(std::wstring s) { |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
177 |
return convertor.to_bytes(s); |
ecbf6504915c
streamlet examples: encapsulate and hide abstract class fields
František Kučera <franta-hg@frantovo.cz>
parents:
69
diff
changeset
|
178 |
} |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
179 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
180 |
static const std::wstring BOOLEAN; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
181 |
static const std::wstring INTEGER; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
182 |
static const std::wstring STRING; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
183 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
184 |
virtual void write(Message m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
185 |
writeInt(m.code); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
186 |
writeInt(m.parameters.size()); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
187 |
for (auto p : m.parameters) writeString(p); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
188 |
flush(); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
189 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
190 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
191 |
virtual void processVersionSupported(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
192 |
versionsSupported.push_back(m.parameters[0]); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
193 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
194 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
195 |
virtual void processWaitingForVersion(Message& m) { |
66
8a8b6434e4bb
streamlet examples: common functions in C/C++: check supported version
František Kučera <franta-hg@frantovo.cz>
parents:
63
diff
changeset
|
196 |
for (std::wstring v : versionsSupported) if (v == L"1") return write({S::VERSION_ACCEPTED, L"1"}); |
8a8b6434e4bb
streamlet examples: common functions in C/C++: check supported version
František Kučera <franta-hg@frantovo.cz>
parents:
63
diff
changeset
|
197 |
write({S::STREAMLET_ERROR, L"INCOMPATIBLE_VERSION", L"Only version 1 is supported by this streamlet."}); |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
198 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
199 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
200 |
virtual void processRelationStart(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
201 |
currentRelation = m.parameters[0]; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
202 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
203 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
204 |
virtual void processInputAttributeMetadata(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
205 |
inputAttributes.push_back({m.parameters[0], m.parameters[1]}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
206 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
207 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
208 |
virtual void processOutputAttributeAlias(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
209 |
outputAttributeAliases.push_back(m.parameters[0]); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
210 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
211 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
212 |
virtual void processOption(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
213 |
options.push_back({m.parameters[0], m.parameters[1]}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
214 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
215 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
216 |
virtual void processInputAttribute(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
217 |
int index = std::stoi(m.parameters[0]); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
218 |
std::wstring value = m.parameters[1]; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
219 |
bool isNull = m.parameters[2] == L"true"; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
220 |
if (inputAttributes[index].name == L"path") currentFile = value; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
221 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
222 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
223 |
virtual void processWaitingForOutputAttributesMetadata(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
224 |
for (AttributeMetadata am : getOutputAttributesMetadata()) write({S::OUTPUT_ATTRIBUTE_METADATA, am.name, am.type}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
225 |
write({S::WAITING_FOR_INPUT_ATTRIBUTES}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
226 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
227 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
228 |
virtual void processWaitingForOutputAttributes(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
229 |
for (OutputAttribute oa : getOutputAttributes()) write({S::OUTPUT_ATTRIBUTE, oa.value, oa.isNull ? L"true" : L"false"}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
230 |
write({S::WAITING_FOR_INPUT_ATTRIBUTES}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
231 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
232 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
233 |
virtual void processUnsupportedMessage(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
234 |
write({S::STREAMLET_ERROR, L"UNSUPPORTED_MESSAGE"}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
235 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
236 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
237 |
virtual std::wstring getAlias(int index, const std::wstring& defaultValue) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
238 |
if (outputAttributeAliases.size() > index) return outputAttributeAliases[index]; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
239 |
else return defaultValue; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
240 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
241 |
|
67
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
242 |
virtual std::vector<Option> getOptions(std::wstring name) { |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
243 |
std::vector<Option> result; |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
244 |
for (Option o : options) if (o.name == name) result.push_back(o); |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
245 |
return result; |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
246 |
} |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
247 |
|
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
248 |
virtual std::vector<Option> getOptions(std::wregex namePattern) { |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
249 |
std::vector<Option> result; |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
250 |
std::wsmatch nameMatch; |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
251 |
for (Option o : options) if (std::regex_match(o.name, nameMatch, namePattern)) { |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
252 |
copyMatches(nameMatch, o.nameMatch); |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
253 |
result.push_back(o); |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
254 |
} |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
255 |
return result; |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
256 |
} |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
257 |
|
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
258 |
virtual std::vector<Option> getOptions(std::wregex namePattern, std::wregex valuePattern) { |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
259 |
// TODO: support multiple modes: |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
260 |
// a) throw an exception if valuePattern does not match |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
261 |
// b) return option even if valuePattern does not match (valueMatch will be empty) |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
262 |
// c) skip options with value not matching (current behavior) |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
263 |
std::wsmatch nameMatch; |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
264 |
std::wsmatch valueMatch; |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
265 |
std::vector<Option> result; |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
266 |
for (Option o : options) if (std::regex_match(o.name, nameMatch, namePattern) && std::regex_match(o.value, valueMatch, valuePattern)) { |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
267 |
copyMatches(nameMatch, o.nameMatch); |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
268 |
copyMatches(valueMatch, o.valueMatch); |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
269 |
result.push_back(o); |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
270 |
} |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
271 |
return result; |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
272 |
} |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
273 |
|
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
274 |
virtual std::vector<AttributeMetadata> getOutputAttributesMetadata() = 0; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
275 |
virtual std::vector<OutputAttribute> getOutputAttributes() = 0; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
276 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
277 |
public: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
278 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
279 |
virtual ~Streamlet() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
280 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
281 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
282 |
int run() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
283 |
try { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
284 |
processMessages(); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
285 |
return 0; |
67
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
286 |
} catch (std::exception& e) { |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
287 |
write({S::STREAMLET_ERROR, L"xxxx", L"Exception in streamlet: " + convertor.from_bytes(e.what())}); // FIXME: correct error codes |
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
288 |
return 1; |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
289 |
} catch (...) { |
67
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
290 |
write({S::STREAMLET_ERROR, L"xxxx", L"Unknown exception in streamlet."}); // FIXME: correct error codes |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
291 |
return 1; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
292 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
293 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
294 |
}; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
295 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
296 |
const std::wstring Streamlet::BOOLEAN = L"boolean"; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
297 |
const std::wstring Streamlet::INTEGER = L"integer"; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
298 |
const std::wstring Streamlet::STRING = L"string"; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
299 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
300 |
#define STREAMLET_RUN(clazz) \ |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
301 |
int main(int argc, char** argv) { \ |
67
0766d298eb1c
streamlet examples: xpath
František Kučera <franta-hg@frantovo.cz>
parents:
66
diff
changeset
|
302 |
setlocale(LC_ALL, ""); \ |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
303 |
clazz s; \ |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
304 |
return s.run(); \ |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
305 |
} |