author | František Kučera <franta-hg@frantovo.cz> |
Wed, 20 Feb 2019 11:30:50 +0100 | |
branch | v_0 |
changeset 18 | 37aacccebed0 |
parent 13 | 816094aa1fff |
child 23 | 47a7d86ad810 |
permissions | -rw-r--r-- |
3 | 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 |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
#include <cstdlib> |
2
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
19 |
#include <unistd.h> |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <memory> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <relpipe/cli/CLI.h> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <relpipe/cli/RelpipeCLIException.h> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <relpipe/reader/Factory.h> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
#include <relpipe/reader/RelationalReader.h> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
#include <relpipe/reader/RelpipeReaderException.h> |
13
816094aa1fff
move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
27 |
#include <relpipe/xmlwriter/RelpipeXMLWriterException.h> |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
#include "XmlHandler.h" |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
using namespace relpipe::cli; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
using namespace relpipe::reader; |
4 | 33 |
using namespace relpipe::out::xml; |
13
816094aa1fff
move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
34 |
using namespace relpipe::xmlwriter; |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
int main(int argc, char** argv) { |
5
7c84323969d1
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
37 |
setlocale(LC_ALL, ""); |
7c84323969d1
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
38 |
CLI::untieStdIO(); |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
CLI cli(argc, argv); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
try { |
2
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
44 |
|
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
45 |
// TODO: color syntax highlighting if stdout is TTY: |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
46 |
// if (isatty(fileno(stdout))) std::cout << "color" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
47 |
// else std::cout << "no-color" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
48 |
|
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
std::shared_ptr<RelationalReader> reader(Factory::create(std::cin)); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
XmlHandler handler(std::cout); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
reader->addHandler(&handler); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
reader->process(); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
resultCode = CLI::EXIT_CODE_SUCCESS; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
} catch (RelpipeCLIException e) { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str()); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
resultCode = e.getExitCode(); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
} catch (RelpipeReaderException e) { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str()); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
resultCode = CLI::EXIT_CODE_DATA_ERROR; |
13
816094aa1fff
move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
62 |
} catch (RelpipeXMLWriterException e) { |
816094aa1fff
move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
63 |
fwprintf(stderr, L"Error while writing XML: %ls\n", e.getMessge().c_str()); |
816094aa1fff
move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
64 |
resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
return resultCode; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
} |