src/StdInCommand.h
branchv_0
changeset 43 3c8ea5dcf793
parent 42 09cd32a65709
child 44 dd7094457e44
equal deleted inserted replaced
42:09cd32a65709 43:3c8ea5dcf793
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 #pragma once
       
    18 
       
    19 #include <cstdlib>
       
    20 #include <iostream>
       
    21 #include <string>
       
    22 #include <vector>
       
    23 #include <algorithm>
       
    24 #include <string>
       
    25 #include <locale>
       
    26 
       
    27 #include <relpipe/writer/typedefs.h>
       
    28 #include <relpipe/writer/AttributeMetadata.h>
       
    29 #include <relpipe/cli/CLI.h>
       
    30 #include <relpipe/cli/RelpipeCLIException.h>
       
    31 
       
    32 #include "Command.h"
       
    33 
       
    34 namespace relpipe {
       
    35 namespace in {
       
    36 namespace cli {
       
    37 
       
    38 /**
       
    39  * TODO: consider code merge with ArgumentsCommand
       
    40  */
       
    41 class StdInCommand : public Command {
       
    42 private:
       
    43 	relpipe::writer::boolean_t readStdIn;
       
    44 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
       
    45 
       
    46 	/**
       
    47 	 * Reads next value from arguments and (if no arguments left) from input
       
    48 	 * @param input
       
    49 	 * @param arguments
       
    50 	 * @param i current index in the arguments vector
       
    51 	 * @param required if true, exception is thrown when no data found
       
    52 	 * @return 
       
    53 	 */
       
    54 	relpipe::writer::string_t readNext(std::istream& input, const std::vector<relpipe::writer::string_t>& arguments, size_t& i, relpipe::writer::boolean_t required) {
       
    55 		using namespace relpipe::writer;
       
    56 		using namespace relpipe::cli;
       
    57 
       
    58 		if (i < arguments.size()) {
       
    59 			return arguments[i++];
       
    60 		} else if (readStdIn) {
       
    61 			std::stringstream value;
       
    62 
       
    63 			while (true) {
       
    64 				char ch;
       
    65 				input.get(ch);
       
    66 				if (ch == 0 || input.eof() || input.fail()) break;
       
    67 				else value << ch;
       
    68 			}
       
    69 
       
    70 			if (required && value.str().empty()) throw RelpipeCLIException(L"Missing value on STDIN.", CLI::EXIT_CODE_BAD_SYNTAX);
       
    71 
       
    72 			return convertor.from_bytes(value.str());
       
    73 		} else {
       
    74 			if (required) throw RelpipeCLIException(L"Missing value on CLI.", CLI::EXIT_CODE_BAD_SYNTAX);
       
    75 			return L"";
       
    76 		}
       
    77 	}
       
    78 
       
    79 
       
    80 public:
       
    81 
       
    82 	StdInCommand(relpipe::writer::boolean_t readStdIn) :
       
    83 	readStdIn(readStdIn) {
       
    84 	}
       
    85 
       
    86 	void process(std::istream& input, std::ostream& output, const relpipe::writer::string_t& command, const std::vector<relpipe::writer::string_t>& arguments) override {
       
    87 		using namespace relpipe::writer;
       
    88 
       
    89 		size_t i = 0;
       
    90 		string_t relationName = readNext(input, arguments, i, true);
       
    91 		integer_t attributeCount = std::stol(readNext(input, arguments, i, true)); // TODO: use integer data type's method? + unsigned type
       
    92 		boolean_t writeHeader = true; // TODO: add option for header omitting
       
    93 
       
    94 		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
       
    95 
       
    96 		std::vector<AttributeMetadata> attributes(attributeCount);
       
    97 
       
    98 		for (size_t j = 0; j < attributeCount; j++) {
       
    99 			string_t attributeName = readNext(input, arguments, i, true);
       
   100 			TypeId attributeType = writer->toTypeId(readNext(input, arguments, i, true));
       
   101 			attributes[j] = {attributeName, attributeType};
       
   102 		}
       
   103 
       
   104 		writer->startRelation(relationName, attributes, writeHeader);
       
   105 
       
   106 		while (true) {
       
   107 			string_t value = readNext(input, arguments, i, false);
       
   108 			if (value.empty() && (input.eof() || input.fail() || (!readStdIn && i >= arguments.size()))) break;
       
   109 			else writer->writeAttribute(value);
       
   110 			// TODO: check attribute count (avoid unfinished records)
       
   111 		}
       
   112 	}
       
   113 };
       
   114 
       
   115 }
       
   116 }
       
   117 }