src/relpipe-tr-guile.cpp
branchv_0
changeset 33 e87c231afb77
parent 32 2354c9058fb6
child 34 6a16b36ab852
equal deleted inserted replaced
32:2354c9058fb6 33:e87c231afb77
     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, 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 #include <relpipe/writer/TypeId.h>
       
    33 
       
    34 #include "GuileHandler.h"
       
    35 #include "CLIParser.h"
       
    36 
       
    37 using namespace relpipe::cli;
       
    38 using namespace relpipe::tr::guile;
       
    39 
       
    40 static void relpipeMain(void *closure, int argc, char **argv) {
       
    41 	setlocale(LC_ALL, "");
       
    42 	CLI::untieStdIO();
       
    43 	CLI cli(argc, argv);
       
    44 
       
    45 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
       
    46 
       
    47 	try {
       
    48 		CLIParser cliParser;
       
    49 		Configuration configuration = cliParser.parse(cli.arguments());
       
    50 		std::shared_ptr<reader::RelationalReader> reader(reader::Factory::create(std::cin));
       
    51 		std::shared_ptr<writer::RelationalWriter> writer(writer::Factory::create(std::cout));
       
    52 		GuileHandler handler(writer.get(), configuration);
       
    53 		reader->addHandler(&handler);
       
    54 		reader->process();
       
    55 
       
    56 		resultCode = CLI::EXIT_CODE_SUCCESS;
       
    57 
       
    58 	} catch (RelpipeCLIException& e) {
       
    59 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
       
    60 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    61 		resultCode = e.getExitCode();
       
    62 	} catch (RelpipeReaderException& e) {
       
    63 		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str());
       
    64 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    65 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
       
    66 	} catch (GuileException& e) {
       
    67 		fwprintf(stderr, L"Caught Guile exception: %ls\n", e.getMessge().c_str());
       
    68 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    69 		resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
       
    70 	}
       
    71 
       
    72 	exit(resultCode);
       
    73 }
       
    74 
       
    75 int main(int argc, char**argv) {
       
    76 	scm_boot_guile(argc, argv, relpipeMain, nullptr);
       
    77 	return 222; // never reached – see exit(resultCode) above
       
    78 }