src/relpipe-out-xml.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 04 Dec 2021 21:14:52 +0100
branchv_0
changeset 34 03a167952dcd
parent 33 a1024068410c
permissions -rw-r--r--
Added tag v0.18 for changeset a1024068410c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     1
/**
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     2
 * Relational pipes
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     4
 *
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
23
47a7d86ad810 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
3
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     8
 *
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    12
 * GNU General Public License for more details.
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    13
 *
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    14
 * You should have received a copy of the GNU General Public License
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
878648aa663f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    16
 */
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <cstdlib>
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    18
#include <unistd.h>
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <relpipe/cli/CLI.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/RelpipeCLIException.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/reader/Factory.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/reader/RelationalReader.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#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
    26
#include <relpipe/xmlwriter/RelpipeXMLWriterException.h>
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include "XmlHandler.h"
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
using namespace relpipe::cli;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
using namespace relpipe::reader;
4
7e0211f00413 fix names
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    32
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
    33
using namespace relpipe::xmlwriter;
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
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
    36
	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
    37
	CLI::untieStdIO();
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	CLI cli(argc, argv);
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	try {
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    43
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    44
		// 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
    45
		// 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
    46
		// 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
    47
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		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
    49
		XmlHandler handler(std::cout);
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		reader->addHandler(&handler);
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		reader->process();
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		resultCode = CLI::EXIT_CODE_SUCCESS;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	} catch (RelpipeCLIException e) {
33
a1024068410c exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    54
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str());
1
82ba555a97d1 relpipe-out-xml 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());
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		resultCode = e.getExitCode();
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	} catch (RelpipeReaderException e) {
33
a1024068410c exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    58
		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessage().c_str());
1
82ba555a97d1 relpipe-out-xml 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());
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		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
    61
	} catch (RelpipeXMLWriterException e) {
33
a1024068410c exception: fix typo: getMessge() → getMessage()
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    62
		fwprintf(stderr, L"Error while writing XML: %ls\n", e.getMessage().c_str());
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
    63
		resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	}
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
	return resultCode;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
}