do not stop at null byte 0x00; replace them with configurable string (default '^@') v_0 tip
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 23 Apr 2022 11:52:41 +0200
branchv_0
changeset 25 58de33c1af03
parent 24 d698d34baf9b
do not stop at null byte 0x00; replace them with configurable string (default '^@')
bash-completion.sh
nbproject/configurations.xml
src/CLIParser.h
src/CMakeLists.txt
src/Configuration.h
src/NullByteHandler.h
src/relpipe-out-nullbyte.cpp
--- /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 <http://www.gnu.org/licenses/>.
+
+_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
--- 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 @@
           <ccTool>
             <incDir>
               <pElem>../relpipe-lib-reader.cpp/include</pElem>
+              <pElem>../relpipe-lib-common.cpp/include</pElem>
               <pElem>../relpipe-lib-cli.cpp/include</pElem>
               <pElem>build/Debug/src</pElem>
             </incDir>
--- /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 <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <vector>
+#include <iostream>
+
+#include <relpipe/common/type/typedefs.h>
+#include <relpipe/cli/CLI.h>
+#include <relpipe/cli/RelpipeCLIException.h>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace out {
+namespace nullbyte {
+
+class CLIParser {
+private:
+
+	relpipe::common::type::StringX readNext(const std::vector<relpipe::common::type::StringX>& 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<relpipe::common::type::StringX>& 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";
+
+}
+}
+}
--- 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})
 
--- /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 <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <vector>
+#include <iostream>
+
+#include <relpipe/common/type/typedefs.h>
+
+
+namespace relpipe {
+namespace out {
+namespace nullbyte {
+
+class Configuration {
+public:
+
+	relpipe::common::type::StringX nullByteReplacement = L"^@";
+
+	virtual ~Configuration() {
+	}
+};
+
+}
+}
+}
\ No newline at end of file
--- 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 <relpipe/reader/handlers/RelationalReaderStringHandler.h>
 #include <relpipe/reader/handlers/AttributeMetadata.h>
 
+#include "Configuration.h"
+
 namespace relpipe {
 namespace out {
 namespace nullbyte {
@@ -40,10 +42,12 @@
 class NullByteHandler : public RelationalReaderStringHandler {
 private:
 	std::ostream& output;
-	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
+	std::wstring_convert<std::codecvt_utf8<wchar_t>> 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<AttributeMetadata> 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);
 	}
--- 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 <relpipe/reader/RelpipeReaderException.h>
 
 #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<RelationalReader> reader(Factory::create(std::cin));
-		NullByteHandler handler(std::cout);
+		NullByteHandler handler(configuration, std::cout);
 		reader->addHandler(&handler);
 		reader->process();