src/relpipe-in-csv.cpp
author František Kučera <franta-hg@frantovo.cz>
Wed, 23 Sep 2020 16:58:48 +0200
branchv_0
changeset 16 15ee963675af
parent 15 157bb1d5e08a
child 19 0d858e0eedf8
permissions -rw-r--r--
change CLI interface: options: --relation --attribute
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
eca0b23802e8 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
eca0b23802e8 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
1ae185cac1f3 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
0
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
eca0b23802e8 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,
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
eca0b23802e8 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
eca0b23802e8 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/>.
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <cstdlib>
1
5eb4d149c6e2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    18
#include <vector>
0
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <regex>
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <algorithm>
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <unistd.h>
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/writer/RelationalWriter.h>
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <relpipe/writer/RelpipeWriterException.h>
1
5eb4d149c6e2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    26
#include <relpipe/writer/AttributeMetadata.h>
0
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/writer/Factory.h>
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/writer/TypeId.h>
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/cli/CLI.h>
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
14
012d491e219a separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    32
#include "CSVCommand.h"
16
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    33
#include "CLIParser.h"
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    34
#include "Configuration.h"
14
012d491e219a separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    35
0
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
using namespace std;
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
using namespace relpipe::cli;
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
using namespace relpipe::writer;
14
012d491e219a separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    39
using namespace relpipe::in::csv;
0
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
int main(int argc, char** argv) {
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	setlocale(LC_ALL, "");
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	CLI::untieStdIO();
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	CLI cli(argc, argv);
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	try {
16
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    49
		CLIParser cliParser;
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    50
		Configuration configuration = cliParser.parse(cli.arguments());
14
012d491e219a separate code into CSVCommand.h and CSVCommand.h
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    51
		CSVCommand command;
15
157bb1d5e08a work with writer instead of ostream
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    52
		std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
16
15ee963675af change CLI interface: options: --relation --attribute
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    53
		command.process(cin, writer, configuration);
0
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		resultCode = CLI::EXIT_CODE_SUCCESS;
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	} catch (RelpipeWriterException e) {
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	}
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	return resultCode;
eca0b23802e8 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
}