src/relpipe-out-csv.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 27 Nov 2021 23:15:16 +0100
branchv_0
changeset 18 4a0e3b828858
parent 14 a7596589a5b0
permissions -rw-r--r--
exception: fix typo: getMessge() → getMessage()
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
14
a7596589a5b0 change CLI interface: options: --write-header
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    28
#include "Configuration.h"
a7596589a5b0 change CLI interface: options: --write-header
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    29
#include "CLIParser.h"
0
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include "CSVHandler.h"
1
82f86dc48339 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    31
#include "RelpipeCSVWriterException.h"
0
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
using namespace relpipe::cli;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
using namespace relpipe::reader;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
using namespace relpipe::out::csv;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
int main(int argc, char**argv) {
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	CLI cli(argc, argv);
2
75aeeb022302 untie STDIO → utilize buffering
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    39
	CLI::untieStdIO();
0
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
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	try {
14
a7596589a5b0 change CLI interface: options: --write-header
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    44
		CLIParser cliParser;
a7596589a5b0 change CLI interface: options: --write-header
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    45
		Configuration configuration = cliParser.parse(cli.arguments());
0
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
14
a7596589a5b0 change CLI interface: options: --write-header
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    47
		CSVHandler handler(std::cout, configuration);
0
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		reader->addHandler(&handler);
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		reader->process();
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		resultCode = CLI::EXIT_CODE_SUCCESS;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	} catch (RelpipeCLIException e) {
18
4a0e3b828858 exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    54
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str());
0
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 = e.getExitCode();
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	} catch (RelpipeReaderException e) {
18
4a0e3b828858 exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    58
		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessage().c_str());
0
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		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
    60
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
1
82f86dc48339 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
	} catch (RelpipeCSVWriterException e) {
18
4a0e3b828858 exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    62
		fwprintf(stderr, L"Error while generating CSV: %ls\n", e.getMessage().c_str());
1
82f86dc48339 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
0
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	}
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	return resultCode;
97967db4b95b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
}