src/relpipe-out-csv.cpp
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 21:54:27 +0200
branchv_0
changeset 10 4bcf3fb7cc48
parent 2 75aeeb022302
child 14 a7596589a5b0
permissions -rw-r--r--
fix license version: GNU GPLv3
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
10
4bcf3fb7cc48 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
0
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
97967db4b95b 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,
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
97967db4b95b 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
97967db4b95b 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/>.
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
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
#include <cstdlib>
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <relpipe/cli/CLI.h>
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/RelpipeCLIException.h>
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/reader/Factory.h>
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <relpipe/reader/RelationalReader.h>
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <relpipe/reader/RelpipeReaderException.h>
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include "CSVHandler.h"
1
82f86dc48339 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    29
#include "RelpipeCSVWriterException.h"
0
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);
2
75aeeb022302 untie STDIO → utilize buffering
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    37
	CLI::untieStdIO();
0
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	try {
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		CSVHandler handler(std::cout);
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		reader->addHandler(&handler);
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		reader->process();
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		resultCode = CLI::EXIT_CODE_SUCCESS;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	} catch (RelpipeCLIException e) {
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		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
    51
		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
    52
		resultCode = e.getExitCode();
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	} catch (RelpipeReaderException e) {
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		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
    55
		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
    56
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
1
82f86dc48339 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
	} catch (RelpipeCSVWriterException e) {
82f86dc48339 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
		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
    59
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
0
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	}
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
	return resultCode;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
}