src/relpipe-tr-deserialize.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 21 Apr 2022 00:12:47 +0200
branchv_0
changeset 2 d586db80b6cd
parent 0 7e986fcf0d8f
permissions -rw-r--r--
use common hex function, read value from the 'data' attribute
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
7e986fcf0d8f establish project
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
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <cstdio>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <cstdlib>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/CLI.h>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/RelpipeCLIException.h>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <relpipe/reader/Factory.h>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <relpipe/reader/RelationalReader.h>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/reader/RelpipeReaderException.h>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/writer/RelationalWriter.h>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/writer/RelpipeWriterException.h>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/writer/Factory.h>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <unistd.h>
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include "DeserializeHandler.h"
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include "CLIParser.h"
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include "Configuration.h"
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
using namespace relpipe::cli;
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace relpipe::reader;
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
using namespace relpipe::writer;
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
using namespace relpipe::tr::deserialize;
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
int main(int argc, char**argv) {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	setlocale(LC_ALL, "");
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	CLI::untieStdIO();
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	CLI cli(argc, argv);
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	try {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		CLIParser cliParser;
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		Configuration configuration = cliParser.parse(cli.arguments());
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		std::shared_ptr<RelationalWriter> writer(relpipe::writer::Factory::create(std::cout));
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		std::shared_ptr<RelationalReader> reader(relpipe::reader::Factory::create(std::cin));
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		DeserializeHandler handler(writer, configuration);
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		reader->addHandler(&handler);
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		reader->process();
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		resultCode = CLI::EXIT_CODE_SUCCESS;
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	} catch (RelpipeCLIException& e) {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str());
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		resultCode = e.getExitCode();
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	} catch (RelpipeReaderException& e) {
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessage().c_str());
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	}
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	return resultCode;
7e986fcf0d8f establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
}