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