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