src/X11Command.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 20 Mar 2021 19:18:10 +0100
branchv_0
changeset 0 3493d6e7016e
child 1 3bbf848b3565
permissions -rw-r--r--
project skeleton
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
3493d6e7016e project skeleton
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
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <codecvt>
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <iostream>
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include "Configuration.h"
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
namespace relpipe {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
namespace in {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace x11 {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
class X11Command {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
private:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings and use platform encoding as default
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	void processRelation(RelationConfiguration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		// Write header / metadata:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		std::vector<relpipe::writer::AttributeMetadata> attributesMetadata;
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
		for (AttributeRecipe ar : configuration.attributes) attributesMetadata.push_back({ar.name, ar.type});
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		writer->startRelation(configuration.relation, attributesMetadata, configuration.writeHeader);
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		// Write records from CLI:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		for (auto value : configuration.values) writer->writeAttribute(value);
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		// Write records from STDIN:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		if (configuration.valueStream) {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
			std::stringstream rawValue;
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
			while (true) {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
				char ch = 0;
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
				configuration.valueStream->get(ch);
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
				if (ch == 0 && configuration.valueStream->good()) {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
					writer->writeAttribute(convertor.from_bytes(rawValue.str()));
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
					rawValue.str("");
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
					rawValue.clear();
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
				} else if (configuration.valueStream->good()) {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
					rawValue << ch;
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
				} else if (configuration.valueStream->eof()) {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
					return;
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
				} else {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
					throw relpipe::cli::RelpipeCLIException(L"Error while reading values from the input stream.", relpipe::cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exception
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
				}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
			}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
public:
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer) {
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		for (RelationConfiguration& relationConfiguration : configuration.relationConfigurations) processRelation(relationConfiguration, writer);
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
};
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
}
3493d6e7016e project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
}