src/relpipe-out-xml.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 16 Sep 2018 18:12:31 +0200
branchv_0
changeset 2 13a41e435ea0
parent 1 82ba555a97d1
child 3 878648aa663f
permissions -rw-r--r--
quick and dirty XML output
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#include <cstdlib>
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
     2
#include <unistd.h>
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
#include <memory>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
#include <relpipe/cli/CLI.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
#include <relpipe/cli/RelpipeCLIException.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
#include <relpipe/reader/Factory.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
#include <relpipe/reader/RelationalReader.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
#include <relpipe/reader/RelpipeReaderException.h>
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
#include "XmlHandler.h"
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
using namespace relpipe::cli;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
using namespace relpipe::reader;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
using namespace relpipe::out::tabular;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
int main(int argc, char** argv) {
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
	CLI cli(argc, argv);
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
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
	try {
2
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    23
13a41e435ea0 quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    24
		// 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
    25
		// 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
    26
		// 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
    27
1
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
		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
    29
		XmlHandler handler(std::cout);
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
		reader->addHandler(&handler);
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
		reader->process();
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
		resultCode = CLI::EXIT_CODE_SUCCESS;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	} catch (RelpipeCLIException e) {
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
		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
    35
		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
    36
		resultCode = e.getExitCode();
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	} catch (RelpipeReaderException e) {
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		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
    39
		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
    40
		resultCode = CLI::EXIT_CODE_DATA_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
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	return resultCode;
82ba555a97d1 relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
}