src/relpipe-tr-xpath.cpp
branchv_0
changeset 0 73e60c77be23
child 9 42a62e3c8938
equal deleted inserted replaced
-1:000000000000 0:73e60c77be23
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2020 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 
       
    18 #include <cstdio>
       
    19 #include <cstdlib>
       
    20 #include <memory>
       
    21 
       
    22 #include <relpipe/cli/CLI.h>
       
    23 #include <relpipe/cli/RelpipeCLIException.h>
       
    24 
       
    25 #include <relpipe/reader/Factory.h>
       
    26 #include <relpipe/reader/RelationalReader.h>
       
    27 #include <relpipe/reader/RelpipeReaderException.h>
       
    28 
       
    29 #include <relpipe/writer/RelationalWriter.h>
       
    30 #include <relpipe/writer/RelpipeWriterException.h>
       
    31 #include <relpipe/writer/Factory.h>
       
    32 
       
    33 #include "XPathHandler.h"
       
    34 #include "CLIParser.h"
       
    35 #include "Configuration.h"
       
    36 
       
    37 using namespace relpipe::cli;
       
    38 using namespace relpipe::reader;
       
    39 using namespace relpipe::writer;
       
    40 using namespace relpipe::tr::xpath;
       
    41 
       
    42 int main(int argc, char**argv) {
       
    43 	setlocale(LC_ALL, "");
       
    44 	CLI::untieStdIO();
       
    45 	CLI cli(argc, argv);
       
    46 
       
    47 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
       
    48 
       
    49 	try {
       
    50 		CLIParser cliParser;
       
    51 		Configuration configuration = cliParser.parse(cli.arguments());
       
    52 		std::shared_ptr<RelationalWriter> writer(relpipe::writer::Factory::create(std::cout));
       
    53 		std::shared_ptr<RelationalReader> reader(relpipe::reader::Factory::create(std::cin));
       
    54 		XPathHandler handler(writer, configuration);
       
    55 		reader->addHandler(&handler);
       
    56 		reader->process();
       
    57 
       
    58 		resultCode = CLI::EXIT_CODE_SUCCESS;
       
    59 
       
    60 	} catch (RelpipeCLIException& e) {
       
    61 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
       
    62 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    63 		resultCode = e.getExitCode();
       
    64 	} catch (RelpipeReaderException& e) {
       
    65 		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str());
       
    66 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    67 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
       
    68 	}
       
    69 
       
    70 	return resultCode;
       
    71 }