src/relpipe-out-csv.cpp
author František Kučera <franta-hg@frantovo.cz>
Tue, 08 Jan 2019 23:31:07 +0100
branchv_0
changeset 2 75aeeb022302
parent 1 82f86dc48339
child 10 4bcf3fb7cc48
permissions -rw-r--r--
untie STDIO → utilize buffering
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"
1
82f86dc48339 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    30
#include "RelpipeCSVWriterException.h"
0
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
using namespace relpipe::cli;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
using namespace relpipe::reader;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
using namespace relpipe::out::csv;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
int main(int argc, char**argv) {
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	CLI cli(argc, argv);
2
75aeeb022302 untie STDIO → utilize buffering
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    38
	CLI::untieStdIO();
0
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
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	try {
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		CSVHandler handler(std::cout);
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		reader->addHandler(&handler);
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		reader->process();
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
		resultCode = CLI::EXIT_CODE_SUCCESS;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	} catch (RelpipeCLIException e) {
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		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
    52
		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
    53
		resultCode = e.getExitCode();
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	} catch (RelpipeReaderException e) {
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		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
    56
		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
    57
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
1
82f86dc48339 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
	} catch (RelpipeCSVWriterException e) {
82f86dc48339 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
		fwprintf(stderr, L"Error while generating CSV: %ls\n", e.getMessge().c_str());
82f86dc48339 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    60
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
0
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	}
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	return resultCode;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
}