parallel processing: use directly file descriptors (FD) instead of STDIO streams v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 21 Jan 2020 00:19:56 +0100
branchv_0
changeset 57 c40a241d6e0c
parent 56 81a53e7cf0ab
child 58 4679f67a8324
parallel processing: use directly file descriptors (FD) instead of STDIO streams
src/FilesystemCommand.h
src/ParallelFilesystemCommand.h
src/PlainFilesystemCommand.h
src/relpipe-in-filesystem.cpp
--- a/src/FilesystemCommand.h	Mon Jan 20 23:47:54 2020 +0100
+++ b/src/FilesystemCommand.h	Tue Jan 21 00:19:56 2020 +0100
@@ -75,7 +75,7 @@
 
 	virtual ~FilesystemCommand() = default;
 
-	virtual void process(std::istream& input, std::ostream& output, Configuration& configuration) = 0;
+	virtual void process(int inputFD, int outputFD, Configuration& configuration) = 0;
 };
 
 }
--- a/src/ParallelFilesystemCommand.h	Mon Jan 20 23:47:54 2020 +0100
+++ b/src/ParallelFilesystemCommand.h	Tue Jan 21 00:19:56 2020 +0100
@@ -18,6 +18,7 @@
 
 #include <mqueue.h>
 #include <limits.h>
+#include <ext/stdio_filebuf.h>
 
 #include "FilesystemCommand.h"
 
@@ -125,7 +126,7 @@
 class ParallelFilesystemCommand : public FilesystemCommand {
 public:
 
-	void process(std::istream& input, std::ostream& output, Configuration& configuration) {
+	void process(int inputFD, int outputFD, Configuration& configuration) {
 		// TODO: ParallelFilesystemCommand
 
 		{ // TODO: demo code – remove:
@@ -146,7 +147,7 @@
 			mqWriter.send(&writeBuffer);
 
 			mqReader.receive(&readBuffer);
-			
+
 			std::string readData(readBuffer.data, readBuffer.dataLength);
 			std::wstring_convert < codecvt_utf8<wchar_t>> convertor;
 
--- a/src/PlainFilesystemCommand.h	Mon Jan 20 23:47:54 2020 +0100
+++ b/src/PlainFilesystemCommand.h	Tue Jan 21 00:19:56 2020 +0100
@@ -16,6 +16,8 @@
  */
 #pragma once
 
+#include <ext/stdio_filebuf.h>
+
 #include "FilesystemCommand.h"
 
 namespace relpipe {
@@ -31,7 +33,12 @@
 
 public:
 
-	void process(std::istream& input, std::ostream& output, Configuration& configuration) {
+	void process(int inputFD, int outputFD, Configuration& configuration) {
+		__gnu_cxx::stdio_filebuf<char> inputBuffer(inputFD, std::ios::in);
+		__gnu_cxx::stdio_filebuf<char> outputBuffer(outputFD, std::ios::out);
+		std::istream input(&inputBuffer);
+		std::ostream output(&outputBuffer);
+
 		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
 
 		string_t relationName = configuration.relation.empty() ? L"filesystem" : configuration.relation;
--- a/src/relpipe-in-filesystem.cpp	Mon Jan 20 23:47:54 2020 +0100
+++ b/src/relpipe-in-filesystem.cpp	Tue Jan 21 00:19:56 2020 +0100
@@ -50,7 +50,7 @@
 		std::unique_ptr<FilesystemCommand> command;
 		if (configuration.parallelism == 1) command = std::make_unique<PlainFilesystemCommand>();
 		else command = std::make_unique<ParallelFilesystemCommand>();
-		command->process(cin, cout, configuration);
+		command->process(STDIN_FILENO, STDOUT_FILENO, configuration);
 		resultCode = CLI::EXIT_CODE_SUCCESS;
 	} catch (RelpipeWriterException e) {
 		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());