src/X11Handler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 28 Mar 2021 18:41:21 +0200
branchv_0
changeset 0 17fc678e0a94
child 1 043a3e2e5f0c
permissions -rw-r--r--
project skeleton
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
17fc678e0a94 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
17fc678e0a94 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
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
17fc678e0a94 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,
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
17fc678e0a94 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
17fc678e0a94 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/>.
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <stdexcept>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/typedefs.h>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/TypeId.h>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/handlers/AttributeMetadata.h>
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include "Configuration.h"
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace relpipe {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace out {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace x11 {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace relpipe;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
using namespace relpipe::reader;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
using namespace relpipe::reader::handlers;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
class X11Handler : public RelationalReaderStringHandler {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
private:
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	std::ostream& output;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	Configuration& configuration;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	const char QUOTE = '"';
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: probably not needed
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	std::vector<AttributeMetadata> firstAttributes;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	integer_t valueCount = 0;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
public:
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	X11Handler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	void startRelation(string_t name, std::vector<AttributeMetadata> attributes) override {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		if (firstAttributes.empty()) {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
			firstAttributes = attributes;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
			if (configuration.writeHeader) for (auto attr : attributes) attribute(attr.getAttributeName());
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		} else {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
			throw std::logic_error("Only a single relation can be converted to the X11 format.");
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	void attribute(const string_t& value) override {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		valueCount++;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		if (value.size() > 0) {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
			output << QUOTE;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
			for (auto ch : convertor.to_bytes(value)) {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
				if (ch == QUOTE) output << QUOTE << QUOTE;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
				else output << ch;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
			}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
			output << QUOTE;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		if (valueCount % firstAttributes.size()) {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			output << ",";
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		} else {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
			output << "\r\n";
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
			valueCount = 0;
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
		}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	void endOfPipe() {
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
		output.flush();
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
};
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
}
17fc678e0a94 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
}