src/relpipe-tr-infertypes.cpp
author František Kučera <franta-hg@frantovo.cz>
Fri, 21 May 2021 20:16:03 +0200
branchv_0
changeset 0 5d1c556ff7db
child 10 840db0029136
permissions -rw-r--r--
project skeleton
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <cstdio>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <cstdlib>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/CLI.h>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/RelpipeCLIException.h>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <relpipe/reader/Factory.h>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <relpipe/reader/RelationalReader.h>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/reader/RelpipeReaderException.h>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/writer/RelationalWriter.h>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/writer/RelpipeWriterException.h>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/writer/Factory.h>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/writer/TypeId.h>
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include "Configuration.h"
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include "CLIParser.h"
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include "InferTypesHandler.h"
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
using namespace relpipe::cli;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace relpipe::tr::infertypes;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
int main(int argc, char**argv) {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	setlocale(LC_ALL, "");
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	CLI::untieStdIO();
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	CLI cli(argc, argv);
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	try {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		CLIParser cliParser;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		Configuration configuration = cliParser.parse(cli.arguments());
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		std::shared_ptr<writer::RelationalWriter> writer(relpipe::writer::Factory::create(std::cout));
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		std::shared_ptr<RelationalReader> reader(relpipe::reader::Factory::create(std::cin));
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		InferTypesHandler handler(writer, configuration);
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		reader->addHandler(&handler);
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		reader->process();
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		resultCode = CLI::EXIT_CODE_SUCCESS;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	} catch (RelpipeCLIException& e) {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		resultCode = e.getExitCode();
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	} catch (RelpipeReaderException& e) {
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str());
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	}
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	return resultCode;
5d1c556ff7db project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
}