src/X11Handler.h
branchv_0
changeset 0 17fc678e0a94
child 1 043a3e2e5f0c
equal deleted inserted replaced
-1:000000000000 0:17fc678e0a94
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 #pragma once
       
    18 
       
    19 #include <memory>
       
    20 #include <string>
       
    21 #include <vector>
       
    22 #include <iostream>
       
    23 #include <sstream>
       
    24 #include <locale>
       
    25 #include <codecvt>
       
    26 #include <stdexcept>
       
    27 
       
    28 #include <relpipe/reader/typedefs.h>
       
    29 #include <relpipe/reader/TypeId.h>
       
    30 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
       
    31 #include <relpipe/reader/handlers/AttributeMetadata.h>
       
    32 
       
    33 #include "Configuration.h"
       
    34 
       
    35 namespace relpipe {
       
    36 namespace out {
       
    37 namespace x11 {
       
    38 
       
    39 using namespace relpipe;
       
    40 using namespace relpipe::reader;
       
    41 using namespace relpipe::reader::handlers;
       
    42 
       
    43 class X11Handler : public RelationalReaderStringHandler {
       
    44 private:
       
    45 	std::ostream& output;
       
    46 	Configuration& configuration;
       
    47 	const char QUOTE = '"';
       
    48 	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: probably not needed
       
    49 	std::vector<AttributeMetadata> firstAttributes;
       
    50 	integer_t valueCount = 0;
       
    51 public:
       
    52 
       
    53 	X11Handler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
       
    54 	}
       
    55 
       
    56 	void startRelation(string_t name, std::vector<AttributeMetadata> attributes) override {
       
    57 		if (firstAttributes.empty()) {
       
    58 			firstAttributes = attributes;
       
    59 			if (configuration.writeHeader) for (auto attr : attributes) attribute(attr.getAttributeName());
       
    60 		} else {
       
    61 			throw std::logic_error("Only a single relation can be converted to the X11 format.");
       
    62 		}
       
    63 	}
       
    64 
       
    65 	void attribute(const string_t& value) override {
       
    66 		valueCount++;
       
    67 
       
    68 		if (value.size() > 0) {
       
    69 			output << QUOTE;
       
    70 			for (auto ch : convertor.to_bytes(value)) {
       
    71 				if (ch == QUOTE) output << QUOTE << QUOTE;
       
    72 				else output << ch;
       
    73 			}
       
    74 			output << QUOTE;
       
    75 		}
       
    76 
       
    77 		if (valueCount % firstAttributes.size()) {
       
    78 			output << ",";
       
    79 		} else {
       
    80 			output << "\r\n";
       
    81 			valueCount = 0;
       
    82 		}
       
    83 	}
       
    84 
       
    85 	void endOfPipe() {
       
    86 		output.flush();
       
    87 	}
       
    88 
       
    89 };
       
    90 
       
    91 }
       
    92 }
       
    93 }