src/relpipe-out-ods.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 06 Jan 2019 22:15:38 +0100
branchv_0
changeset 8 0ba80e9b711f
parent 0 2f3c9e508827
child 16 3ea2a5a25531
permissions -rw-r--r--
move common XML code to relpipe-lib-xmlwriter (a header-only library)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <cstdlib>
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <unistd.h>
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/CLI.h>
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/RelpipeCLIException.h>
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/reader/Factory.h>
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <relpipe/reader/RelationalReader.h>
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <relpipe/reader/RelpipeReaderException.h>
8
0ba80e9b711f move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    27
#include <relpipe/xmlwriter/XMLWriter.h>
0
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include "OdsHandler.h"
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
using namespace relpipe::cli;
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
using namespace relpipe::reader;
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
using namespace relpipe::out::ods;
8
0ba80e9b711f move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    34
using namespace relpipe::xmlwriter;
0
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
int main(int argc, char** argv) {
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	setlocale(LC_ALL, "");
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	CLI::untieStdIO();
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	CLI cli(argc, argv);
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	try {
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		// TODO: color syntax highlighting if stdout is TTY:
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		// if (isatty(fileno(stdout))) std::cout << "color" << std::endl;
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		// else std::cout << "no-color" << std::endl;
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		OdsHandler handler(std::cout);
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		reader->addHandler(&handler);
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		reader->process();
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		resultCode = CLI::EXIT_CODE_SUCCESS;
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	} catch (RelpipeCLIException e) {
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
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());
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		resultCode = e.getExitCode();
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	} catch (RelpipeReaderException e) {
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str());
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
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());
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
8
0ba80e9b711f move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
	} catch (RelpipeXMLWriterException e) {
0ba80e9b711f move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
		fwprintf(stderr, L"Error while writing XML: %ls\n", e.getMessge().c_str());
0ba80e9b711f move common XML code to relpipe-lib-xmlwriter (a header-only library)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
		resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
0
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	}
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	return resultCode;
2f3c9e508827 project skeleton: cmake, netbeans, code from relpipe-out-xml
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
}