# HG changeset patch # User František Kučera # Date 1650707561 -7200 # Node ID 58de33c1af03f4bb283dbd4ab93e0075044a50e5 # Parent d698d34baf9b99f7537db5ff84f2eb6a3a5f6dec do not stop at null byte 0x00; replace them with configurable string (default '^@') diff -r d698d34baf9b -r 58de33c1af03 bash-completion.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bash-completion.sh Sat Apr 23 11:52:41 2022 +0200 @@ -0,0 +1,34 @@ +# Relational pipes +# Copyright © 2022 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 . + +_relpipe_out_nullbyte_completion() { + local w0 w1 w2 w3 + + COMPREPLY=() + w0=${COMP_WORDS[COMP_CWORD]} + w1=${COMP_WORDS[COMP_CWORD-1]} + w2=${COMP_WORDS[COMP_CWORD-2]} + w3=${COMP_WORDS[COMP_CWORD-3]} + + if [[ "$w1" == "--null-byte-replacement" && "x$w0" == "x" ]]; then COMPREPLY=("'^@'") + else + OPTIONS=( + "--null-byte-replacement" + ) + COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0")) + fi +} + +complete -F _relpipe_out_nullbyte_completion relpipe-out-nullbyte diff -r d698d34baf9b -r 58de33c1af03 nbproject/configurations.xml --- a/nbproject/configurations.xml Sat Apr 23 11:31:30 2022 +0200 +++ b/nbproject/configurations.xml Sat Apr 23 11:52:41 2022 +0200 @@ -80,6 +80,7 @@ ../relpipe-lib-reader.cpp/include + ../relpipe-lib-common.cpp/include ../relpipe-lib-cli.cpp/include build/Debug/src diff -r d698d34baf9b -r 58de33c1af03 src/CLIParser.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/CLIParser.h Sat Apr 23 11:52:41 2022 +0200 @@ -0,0 +1,66 @@ +/** + * Relational pipes + * Copyright © 2022 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 "Configuration.h" + +namespace relpipe { +namespace out { +namespace nullbyte { + +class CLIParser { +private: + + relpipe::common::type::StringX readNext(const std::vector& arguments, int& i) { + if (i < arguments.size()) return arguments[i++]; + else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + +public: + + static const relpipe::common::type::StringX OPTION_NULL_BYTE_REPLACEMENT; + + Configuration parse(const std::vector& arguments) { + Configuration c; + + for (int i = 0; i < arguments.size();) { + relpipe::common::type::StringX option = readNext(arguments, i); + + if (option == OPTION_NULL_BYTE_REPLACEMENT) { + c.nullByteReplacement = readNext(arguments, i); + } else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS); + } + + return c; + } + + virtual ~CLIParser() { + } +}; + +const relpipe::common::type::StringX CLIParser::OPTION_NULL_BYTE_REPLACEMENT = L"--null-byte-replacement"; + +} +} +} diff -r d698d34baf9b -r 58de33c1af03 src/CMakeLists.txt --- a/src/CMakeLists.txt Sat Apr 23 11:31:30 2022 +0200 +++ b/src/CMakeLists.txt Sat Apr 23 11:52:41 2022 +0200 @@ -17,7 +17,7 @@ # Relpipe libraries: INCLUDE(FindPkgConfig) -pkg_check_modules (RELPIPE_LIBS relpipe-lib-reader.cpp relpipe-lib-cli.cpp) +pkg_check_modules (RELPIPE_LIBS relpipe-lib-reader.cpp relpipe-lib-common.cpp relpipe-lib-cli.cpp) include_directories(${RELPIPE_LIBS_INCLUDE_DIRS}) link_directories(${RELPIPE_LIBS_LIBRARY_DIRS}) diff -r d698d34baf9b -r 58de33c1af03 src/Configuration.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Configuration.h Sat Apr 23 11:52:41 2022 +0200 @@ -0,0 +1,40 @@ +/** + * Relational pipes + * Copyright © 2022 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 + + +namespace relpipe { +namespace out { +namespace nullbyte { + +class Configuration { +public: + + relpipe::common::type::StringX nullByteReplacement = L"^@"; + + virtual ~Configuration() { + } +}; + +} +} +} \ No newline at end of file diff -r d698d34baf9b -r 58de33c1af03 src/NullByteHandler.h --- a/src/NullByteHandler.h Sat Apr 23 11:31:30 2022 +0200 +++ b/src/NullByteHandler.h Sat Apr 23 11:52:41 2022 +0200 @@ -29,6 +29,8 @@ #include #include +#include "Configuration.h" + namespace relpipe { namespace out { namespace nullbyte { @@ -40,10 +42,12 @@ class NullByteHandler : public RelationalReaderStringHandler { private: std::ostream& output; - std::wstring_convert> convertor; // TODO: support also other encodings. + std::wstring_convert> convertor; // TODO: support also other encodings? + Configuration configuration; + std::string nullByteReplacement; public: - NullByteHandler(std::ostream& output) : output(output) { + NullByteHandler(Configuration configuration, std::ostream& output) : configuration(configuration), output(output), nullByteReplacement(convertor.to_bytes(configuration.nullByteReplacement)) { } void startRelation(string_t name, std::vector attributes) override { @@ -54,7 +58,7 @@ const std::string octets = convertor.to_bytes(value); for (char ch : octets) { if (ch) output.put(ch); - else output.put('@'); // TODO: configurable replacement, maybe a string instead of a single char + else output << nullByteReplacement; } output.put(0); } diff -r d698d34baf9b -r 58de33c1af03 src/relpipe-out-nullbyte.cpp --- a/src/relpipe-out-nullbyte.cpp Sat Apr 23 11:31:30 2022 +0200 +++ b/src/relpipe-out-nullbyte.cpp Sat Apr 23 11:52:41 2022 +0200 @@ -26,6 +26,7 @@ #include #include "NullByteHandler.h" +#include "CLIParser.h" using namespace relpipe::cli; using namespace relpipe::reader; @@ -37,8 +38,10 @@ int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; try { + CLIParser cliParser; + Configuration configuration = cliParser.parse(cli.arguments()); std::shared_ptr reader(Factory::create(std::cin)); - NullByteHandler handler(std::cout); + NullByteHandler handler(configuration, std::cout); reader->addHandler(&handler); reader->process();