StdInCommand.h
branchv_0
changeset 18 9e543fd0254c
parent 17 f543468a7e49
child 19 22f493401ac0
equal deleted inserted replaced
17:f543468a7e49 18:9e543fd0254c
    21 /**
    21 /**
    22  * TODO: consider code merge with ArgumentsCommand
    22  * TODO: consider code merge with ArgumentsCommand
    23  */
    23  */
    24 class StdInCommand : public Command {
    24 class StdInCommand : public Command {
    25 private:
    25 private:
    26 	wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
    26 	relpipe::writer::boolean_t readStdIn;
       
    27 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
    27 
    28 
    28 	/**
    29 	/**
    29 	 * Reads next value from arguments and (if no arguments left) from input
    30 	 * Reads next value from arguments and (if no arguments left) from input
    30 	 * @param input
    31 	 * @param input
    31 	 * @param arguments
    32 	 * @param arguments
    37 		using namespace relpipe::writer;
    38 		using namespace relpipe::writer;
    38 		using namespace relpipe::cli;
    39 		using namespace relpipe::cli;
    39 
    40 
    40 		if (i < arguments.size()) {
    41 		if (i < arguments.size()) {
    41 			return arguments[i++];
    42 			return arguments[i++];
    42 		} else {
    43 		} else if (readStdIn) {
    43 			std::stringstream value;
    44 			std::stringstream value;
    44 
    45 
    45 			while (true) {
    46 			while (true) {
    46 				char ch;
    47 				char ch;
    47 				input.get(ch);
    48 				input.get(ch);
    48 				if (ch == 0 || input.eof() || input.fail()) break;
    49 				if (ch == 0 || input.eof() || input.fail()) break;
    49 				else value << ch;
    50 				else value << ch;
    50 			}
    51 			}
    51 			
    52 
    52 			if (required && value.str().empty()) throw RelpipeCLIException(L"Missing value on STDIN.", CLI::EXIT_CODE_BAD_SYNTAX);
    53 			if (required && value.str().empty()) throw RelpipeCLIException(L"Missing value on STDIN.", CLI::EXIT_CODE_BAD_SYNTAX);
    53 
    54 
    54 			return convertor.from_bytes(value.str());
    55 			return convertor.from_bytes(value.str());
       
    56 		} else {
       
    57 			if (required) throw RelpipeCLIException(L"Missing value on CLI.", CLI::EXIT_CODE_BAD_SYNTAX);
       
    58 			return L"";
    55 		}
    59 		}
    56 	}
    60 	}
    57 
    61 
    58 
    62 
    59 public:
    63 public:
       
    64 
       
    65 	StdInCommand(relpipe::writer::boolean_t readStdIn) :
       
    66 	readStdIn(readStdIn) {
       
    67 	}
    60 
    68 
    61 	void process(std::istream& input, std::ostream& output, const relpipe::writer::string_t& command, const std::vector<relpipe::writer::string_t>& arguments) override {
    69 	void process(std::istream& input, std::ostream& output, const relpipe::writer::string_t& command, const std::vector<relpipe::writer::string_t>& arguments) override {
    62 		using namespace relpipe::writer;
    70 		using namespace relpipe::writer;
    63 
    71 
    64 		size_t i = 0;
    72 		size_t i = 0;
    78 
    86 
    79 		writer->startRelation(relationName, attributes, writeHeader);
    87 		writer->startRelation(relationName, attributes, writeHeader);
    80 
    88 
    81 		while (true) {
    89 		while (true) {
    82 			string_t value = readNext(input, arguments, i, false);
    90 			string_t value = readNext(input, arguments, i, false);
    83 			if (value.empty() && (input.eof() || input.fail())) break;
    91 			if (value.empty() && (input.eof() || input.fail() || (!readStdIn && i >= arguments.size()))) break;
    84 			else writer->writeAttribute(value);
    92 			else writer->writeAttribute(value);
    85 			// TODO: check attribute count (avoid unfinished records)
    93 			// TODO: check attribute count (avoid unfinished records)
    86 		}
    94 		}
    87 	}
    95 	}
    88 };
    96 };