src/relpipe-out-recfile.cpp
branchv_0
changeset 0 9005fdd81bca
child 7 d59f31fa7009
equal deleted inserted replaced
-1:000000000000 0:9005fdd81bca
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2019 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 #include <cstdlib>
       
    19 #include <unistd.h>
       
    20 #include <memory>
       
    21 
       
    22 #include <relpipe/cli/CLI.h>
       
    23 #include <relpipe/cli/RelpipeCLIException.h>
       
    24 #include <relpipe/reader/Factory.h>
       
    25 #include <relpipe/reader/RelationalReader.h>
       
    26 #include <relpipe/reader/RelpipeReaderException.h>
       
    27 
       
    28 #include "RecfileHandler.h"
       
    29 
       
    30 using namespace relpipe::cli;
       
    31 using namespace relpipe::reader;
       
    32 using namespace relpipe::out::recfile;
       
    33 
       
    34 int main(int argc, char** argv) {
       
    35 	setlocale(LC_ALL, "");
       
    36 	CLI::untieStdIO();
       
    37 	CLI cli(argc, argv);
       
    38 
       
    39 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
       
    40 
       
    41 	try {
       
    42 		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
       
    43 		RecfileHandler handler(std::cout);
       
    44 		reader->addHandler(&handler);
       
    45 		reader->process();
       
    46 		resultCode = CLI::EXIT_CODE_SUCCESS;
       
    47 	} catch (RelpipeCLIException e) {
       
    48 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
       
    49 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    50 		resultCode = e.getExitCode();
       
    51 	} catch (RelpipeReaderException e) {
       
    52 		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str());
       
    53 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    54 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
       
    55 	}
       
    56 
       
    57 	return resultCode;
       
    58 }