src/relpipe-tr-cut.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 06 Jan 2019 15:20:51 +0100
branchv_0
changeset 15 63e804b63321
parent 9 f06781a5071b
child 22 d07ac873cc89
permissions -rw-r--r--
fix the class name
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
a1301a106504 empty cmake project
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
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
4
fc58054da255 CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    19
#include <cstdio>
2
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    20
#include <cstdlib>
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    21
#include <memory>
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    22
0
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/CLI.h>
2
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    24
#include <relpipe/cli/RelpipeCLIException.h>
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    25
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    26
#include <relpipe/reader/Factory.h>
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    27
#include <relpipe/reader/RelationalReader.h>
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    28
#include <relpipe/reader/RelpipeReaderException.h>
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    29
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    30
#include <relpipe/writer/RelationalWriter.h>
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    31
#include <relpipe/writer/RelpipeWriterException.h>
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    32
#include <relpipe/writer/Factory.h>
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    33
#include <relpipe/writer/TypeId.h>
0
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
9
f06781a5071b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    35
#include "CutHandler.h"
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    36
0
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
using namespace relpipe::cli;
2
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    38
using namespace relpipe::reader;
7
aebaf590a838 use smart pointer
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    39
using namespace relpipe::tr::grep;
0
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
int main(int argc, char**argv) {
4
fc58054da255 CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    42
	setlocale(LC_ALL, "");
fc58054da255 CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    43
	CLI::untieStdIO();
0
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	CLI cli(argc, argv);
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    45
2
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    47
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
	try {
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
15
63e804b63321 fix the class name
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    50
		CutHandler handler(std::cout, cli.arguments());
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    51
		reader->addHandler(&handler);
2
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
		reader->process();
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
		resultCode = CLI::EXIT_CODE_SUCCESS;
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    56
	} catch (RelpipeCLIException& e) {
2
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
		resultCode = e.getExitCode();
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    60
	} catch (RelpipeReaderException& e) {
2
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str());
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
	}
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
eb2066405f79 reader without any output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
	return resultCode;
0
a1301a106504 empty cmake project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
}