src/relpipe-out-recfile.cpp
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 21:59:01 +0200
branchv_0
changeset 7 d59f31fa7009
parent 0 9005fdd81bca
child 13 6bcc0cce7b75
permissions -rw-r--r--
fix license version: GNU GPLv3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
9005fdd81bca project and code skeleton: text output
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
7
d59f31fa7009 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <cstdlib>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <unistd.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <relpipe/cli/CLI.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/RelpipeCLIException.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/reader/Factory.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/reader/RelationalReader.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <relpipe/reader/RelpipeReaderException.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include "RecfileHandler.h"
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
using namespace relpipe::cli;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
using namespace relpipe::reader;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
using namespace relpipe::out::recfile;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
int main(int argc, char** argv) {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	setlocale(LC_ALL, "");
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	CLI::untieStdIO();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	CLI cli(argc, argv);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	try {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		RecfileHandler handler(std::cout);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		reader->addHandler(&handler);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		reader->process();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		resultCode = CLI::EXIT_CODE_SUCCESS;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	} catch (RelpipeCLIException e) {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		resultCode = e.getExitCode();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	} catch (RelpipeReaderException e) {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str());
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	return resultCode;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
}