src/relpipe-in-qr.cpp
branchv_0
changeset 0 0a0cb749c10f
equal deleted inserted replaced
-1:000000000000 0:0a0cb749c10f
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2021 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 #include <cstdlib>
       
    18 #include <memory>
       
    19 #include <functional>
       
    20 
       
    21 #include <relpipe/writer/RelationalWriter.h>
       
    22 #include <relpipe/writer/RelpipeWriterException.h>
       
    23 #include <relpipe/writer/Factory.h>
       
    24 #include <relpipe/writer/TypeId.h>
       
    25 
       
    26 #include <relpipe/cli/CLI.h>
       
    27 #include <relpipe/cli/RelpipeCLIException.h>
       
    28 
       
    29 #include "CLIParser.h"
       
    30 #include "Configuration.h"
       
    31 
       
    32 #include "QRCommand.h"
       
    33 
       
    34 using namespace relpipe::cli;
       
    35 using namespace relpipe::in::qr;
       
    36 using namespace relpipe::writer;
       
    37 
       
    38 int main(int argc, char** argv) {
       
    39 	setlocale(LC_ALL, "");
       
    40 	CLI::untieStdIO();
       
    41 	CLI cli(argc, argv);
       
    42 
       
    43 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
       
    44 
       
    45 	try {
       
    46 		CLIParser cliParser;
       
    47 		Configuration configuration = cliParser.parse(cli.arguments());
       
    48 		QRCommand command;
       
    49 		std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
       
    50 		command.process(configuration, writer, std::bind(fflush, stdout)); // std::bind(fflush, XXX) Factory::create(XXX) must be the same stream XXX
       
    51 		resultCode = CLI::EXIT_CODE_SUCCESS;
       
    52 	} catch (RelpipeWriterException& e) {
       
    53 		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());
       
    54 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    55 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
       
    56 	} catch (RelpipeCLIException& e) {
       
    57 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
       
    58 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    59 		resultCode = e.getExitCode();
       
    60 	}
       
    61 
       
    62 	return resultCode;
       
    63 }