author | František Kučera <franta-hg@frantovo.cz> |
Sat, 04 Dec 2021 21:14:53 +0100 | |
branch | v_0 |
changeset 28 | 8c532dab3c42 |
parent 27 | 1bf047cc60f5 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
18
5a3602fb1649
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
0 | 8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
||
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
|
18 |
#include <cstdio> |
2
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
19 |
#include <cstdlib> |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
20 |
#include <memory> |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
21 |
|
0 | 22 |
#include <relpipe/cli/CLI.h> |
2
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
23 |
#include <relpipe/cli/RelpipeCLIException.h> |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
24 |
|
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
25 |
#include <relpipe/reader/Factory.h> |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
26 |
#include <relpipe/reader/RelationalReader.h> |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
27 |
#include <relpipe/reader/RelpipeReaderException.h> |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
28 |
|
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
29 |
#include <relpipe/writer/RelationalWriter.h> |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
30 |
#include <relpipe/writer/RelpipeWriterException.h> |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
31 |
#include <relpipe/writer/Factory.h> |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
32 |
#include <relpipe/writer/TypeId.h> |
0 | 33 |
|
24
c69670b7b4ef
new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
34 |
#include "Configuration.h" |
c69670b7b4ef
new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
35 |
#include "CLIParser.h" |
7 | 36 |
#include "GrepHandler.h" |
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
37 |
|
0 | 38 |
using namespace relpipe::cli; |
7 | 39 |
using namespace relpipe::tr::grep; |
0 | 40 |
|
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 | 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 { |
25
98be80d2e65b
new CLI options: --case-sensitive, --invert-match
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
49 |
CLIParser cliParser; |
98be80d2e65b
new CLI options: --case-sensitive, --invert-match
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
50 |
Configuration configuration = cliParser.parse(cli.arguments()); |
98be80d2e65b
new CLI options: --case-sensitive, --invert-match
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
51 |
|
24
c69670b7b4ef
new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
52 |
std::shared_ptr<writer::RelationalWriter> writer(relpipe::writer::Factory::create(std::cout)); |
c69670b7b4ef
new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
53 |
std::shared_ptr<RelationalReader> reader(relpipe::reader::Factory::create(std::cin)); |
c69670b7b4ef
new CLI interface: --relation, --attribute, --pattern
František Kučera <franta-hg@frantovo.cz>
parents:
18
diff
changeset
|
54 |
GrepHandler handler(writer, configuration); |
3
8731263d44f1
PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
55 |
reader->addHandler(&handler); |
2
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
reader->process(); |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
|
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
resultCode = CLI::EXIT_CODE_SUCCESS; |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
|
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
60 |
} catch (RelpipeCLIException& e) { |
27
1bf047cc60f5
exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
61 |
fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str()); |
2
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 = e.getExitCode(); |
8
f66c759d1111
first working grep version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
64 |
} catch (RelpipeReaderException& e) { |
27
1bf047cc60f5
exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
65 |
fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessage().c_str()); |
2
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
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
|
67 |
resultCode = CLI::EXIT_CODE_DATA_ERROR; |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
} |
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
|
eb2066405f79
reader without any output
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
return resultCode; |
0 | 71 |
} |