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

#include <cstdlib>
#include <unistd.h>
#include <memory>

#include <relpipe/cli/CLI.h>
#include <relpipe/cli/RelpipeCLIException.h>
#include <relpipe/reader/Factory.h>
#include <relpipe/reader/RelationalReader.h>
#include <relpipe/reader/RelpipeReaderException.h>

#include "XmlHandler.h"

using namespace relpipe::cli;
using namespace relpipe::reader;
using namespace relpipe::out::tabular;

int main(int argc, char** argv) {
	CLI cli(argc, argv);

	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;

	try {

		// TODO: color syntax highlighting if stdout is TTY:
		// if (isatty(fileno(stdout))) std::cout << "color" << std::endl;
		// else std::cout << "no-color" << std::endl;

		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
		XmlHandler handler(std::cout);
		reader->addHandler(&handler);
		reader->process();
		resultCode = CLI::EXIT_CODE_SUCCESS;
	} catch (RelpipeCLIException e) {
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
		resultCode = e.getExitCode();
	} catch (RelpipeReaderException e) {
		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str());
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
	}

	return resultCode;
}