# HG changeset patch # User František Kučera # Date 1580079794 -3600 # Node ID 8c6885543e2c75f033487d5df9816d47c08c2f62 # Parent a467e8cbd16b922a6502ae1691d8d73ea15eab08 streamlet examples: common functions in C/C++ diff -r a467e8cbd16b -r 8c6885543e2c .hgignore --- 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 diff -r a467e8cbd16b -r 8c6885543e2c streamlet-examples/Makefile --- /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 diff -r a467e8cbd16b -r 8c6885543e2c streamlet-examples/streamlet-common.h --- /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 . + */ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#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 parameters; + + Message() { + } + + Message(int code) : code(code) { + } + + Message(int code, std::vector 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 versionsSupported; + std::vector inputAttributes; + std::vector outputAttributeAliases; + std::vector