--- a/.hgignore Sun Jan 26 21:35:02 2020 +0100
+++ b/.hgignore Mon Jan 27 00:03:14 2020 +0100
@@ -11,3 +11,5 @@
^dist/
^build/
^nbproject/private/
+
+^streamlet-examples/(xpath|pid)$
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/streamlet-examples/Makefile Mon Jan 27 00:03:14 2020 +0100
@@ -0,0 +1,13 @@
+all: xpath pid
+
+.PHONY: all clean
+
+xpath: streamlet-common.h xpath.cpp
+ g++ -g -fno-omit-frame-pointer -fsanitize=address xpath.cpp -o xpath $(shell pkg-config --libs --cflags libxml++-2.6)
+
+pid: streamlet-common.h pid.cpp
+ g++ -g -fno-omit-frame-pointer -fsanitize=address pid.cpp -o pid
+
+clean:
+ rm -f xpath
+ rm -f pid
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/streamlet-examples/streamlet-common.h Mon Jan 27 00:03:14 2020 +0100
@@ -0,0 +1,230 @@
+/**
+ * Relational pipes
+ * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <iostream>
+#include <exception>
+#include <vector>
+#include <string>
+#include <sstream>
+#include <codecvt>
+#include <locale>
+
+#include "../src/StreamletMsg.h"
+
+/**
+ * Unlike the protocol and the message format,
+ * these helper classes and functions are not part of the public API.
+ * Thus when writing custom streamlets, it is better to copy this file
+ * and review its changes while upgrading to new upstream version.
+ */
+
+using S = relpipe::in::filesystem::StreamletMsg;
+
+class Streamlet {
+private:
+
+ class Message {
+ public:
+ int code;
+ std::vector<std::wstring> parameters;
+
+ Message() {
+ }
+
+ Message(int code) : code(code) {
+ }
+
+ Message(int code, std::vector<std::wstring> parameters) : code(code), parameters(parameters) {
+ }
+
+ Message(int code, std::wstring p1) : code(code), parameters({p1}) {
+ }
+
+ Message(int code, std::wstring p1, std::wstring p2) : code(code), parameters({p1, p2}) {
+ }
+ };
+
+ static const char SEPARATOR = '\0';
+
+ int readInt() {
+ return std::stoi(readString());
+ }
+
+ std::wstring readString() {
+ std::stringstream s;
+ for (char ch; std::cin.read(&ch, 1).good() && ch != SEPARATOR;) s.put(ch);
+ return convertor.from_bytes(s.str());
+ }
+
+ void writeString(std::wstring s) {
+ std::cout << convertor.to_bytes(s.c_str());
+ std::cout.put(SEPARATOR);
+ if (std::cout.bad()) throw std::runtime_error("Unable to write to sub-process.");
+ }
+
+ void writeInt(int i) {
+ writeString(std::to_wstring(i));
+ }
+
+ void flush() {
+ std::cout.flush();
+ }
+
+ Message read() {
+ Message m;
+ m.code = readInt();
+ int count = readInt();
+ for (int i = 0; i < count; i++) m.parameters.push_back(readString());
+ return m;
+ }
+
+ void processMessages() {
+ while (true) {
+ Message m = read();
+ if (m.code == S::VERSION_SUPPORTED) processVersionSupported(m);
+ else if (m.code == S::WAITING_FOR_VERSION) processWaitingForVersion(m);
+ else if (m.code == S::RELATION_START) processRelationStart(m);
+ else if (m.code == S::INPUT_ATTRIBUTE_METADATA) processInputAttributeMetadata(m);
+ else if (m.code == S::OUTPUT_ATTRIBUTE_ALIAS) processOutputAttributeAlias(m);
+ else if (m.code == S::OPTION) processOption(m);
+ else if (m.code == S::INPUT_ATTRIBUTE) processInputAttribute(m);
+ else if (m.code == S::WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA) processWaitingForOutputAttributesMetadata(m);
+ else if (m.code == S::WAITING_FOR_OUTPUT_ATTRIBUTES) processWaitingForOutputAttributes(m);
+ else if (m.code == S::RELATION_END) break;
+ else processUnsupportedMessage(m);
+ }
+ }
+
+protected:
+
+ class AttributeMetadata {
+ public:
+ std::wstring name;
+ std::wstring type;
+ };
+
+ class OutputAttribute {
+ public:
+ std::wstring value;
+ bool isNull;
+ };
+
+ class Option {
+ public:
+ std::wstring name;
+ std::wstring value;
+ };
+
+ std::vector<std::wstring> versionsSupported;
+ std::vector<AttributeMetadata> inputAttributes;
+ std::vector<std::wstring> outputAttributeAliases;
+ std::vector<Option> options;
+ std::wstring currentRelation;
+ std::wstring currentFile;
+
+ std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. Or use always UTF-8 for communication with subprocesses.
+
+ static const std::wstring BOOLEAN;
+ static const std::wstring INTEGER;
+ static const std::wstring STRING;
+
+ virtual void write(Message m) {
+ writeInt(m.code);
+ writeInt(m.parameters.size());
+ for (auto p : m.parameters) writeString(p);
+ flush();
+ }
+
+ virtual void processVersionSupported(Message& m) {
+ versionsSupported.push_back(m.parameters[0]);
+ }
+
+ virtual void processWaitingForVersion(Message& m) {
+ // TODO: check that 1 is supported
+ write({S::VERSION_ACCEPTED, L"1"});
+ }
+
+ virtual void processRelationStart(Message& m) {
+ currentRelation = m.parameters[0];
+ }
+
+ virtual void processInputAttributeMetadata(Message& m) {
+ inputAttributes.push_back({m.parameters[0], m.parameters[1]});
+ }
+
+ virtual void processOutputAttributeAlias(Message& m) {
+ outputAttributeAliases.push_back(m.parameters[0]);
+ }
+
+ virtual void processOption(Message& m) {
+ options.push_back({m.parameters[0], m.parameters[1]});
+ }
+
+ virtual void processInputAttribute(Message& m) {
+ int index = std::stoi(m.parameters[0]);
+ std::wstring value = m.parameters[1];
+ bool isNull = m.parameters[2] == L"true";
+ if (inputAttributes[index].name == L"path") currentFile = value;
+ }
+
+ virtual void processWaitingForOutputAttributesMetadata(Message& m) {
+ for (AttributeMetadata am : getOutputAttributesMetadata()) write({S::OUTPUT_ATTRIBUTE_METADATA, am.name, am.type});
+ write({S::WAITING_FOR_INPUT_ATTRIBUTES});
+ }
+
+ virtual void processWaitingForOutputAttributes(Message& m) {
+ for (OutputAttribute oa : getOutputAttributes()) write({S::OUTPUT_ATTRIBUTE, oa.value, oa.isNull ? L"true" : L"false"});
+ write({S::WAITING_FOR_INPUT_ATTRIBUTES});
+ }
+
+ virtual void processUnsupportedMessage(Message& m) {
+ write({S::STREAMLET_ERROR, L"UNSUPPORTED_MESSAGE"});
+ }
+
+ virtual std::wstring getAlias(int index, const std::wstring& defaultValue) {
+ if (outputAttributeAliases.size() > index) return outputAttributeAliases[index];
+ else return defaultValue;
+ }
+
+ virtual std::vector<AttributeMetadata> getOutputAttributesMetadata() = 0;
+ virtual std::vector<OutputAttribute> getOutputAttributes() = 0;
+
+public:
+
+ virtual ~Streamlet() {
+ }
+
+ int run() {
+ try {
+ processMessages();
+ return 0;
+ } catch (...) {
+ return 1;
+ }
+ }
+};
+
+const std::wstring Streamlet::BOOLEAN = L"boolean";
+const std::wstring Streamlet::INTEGER = L"integer";
+const std::wstring Streamlet::STRING = L"string";
+
+#define STREAMLET_RUN(clazz) \
+int main(int argc, char** argv) { \
+ clazz s; \
+ return s.run(); \
+}