src/ASN1Handler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 21:53:25 +0200
branchv_0
changeset 7 c155b441306f
parent 3 0fdfbf39f19b
permissions -rw-r--r--
fix license version: GNU GPLv3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
f4f9cdf7ed59 project and code skeleton: text output
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
7
c155b441306f fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
0
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <string>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <vector>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <iostream>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <sstream>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <locale>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <codecvt>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <regex>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <cassert>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/typedefs.h>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/TypeId.h>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/RelationalReaderValueHandler.h>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/handlers/AttributeMetadata.h>
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include "ASN1Writer.h"
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace relpipe {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace out {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace asn1 {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace relpipe::reader;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
3
0fdfbf39f19b fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    41
class ASN1Handler : public handlers::RelationalReaderValueHandler {
0
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
private:
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	shared_ptr<ASN1Writer> asn1Writer;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	std::vector<TypeId> columnTypes;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	std::vector<string_t> columnTypeCodes;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	std::vector<string_t> columnNames;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	integer_t valueCount = 0;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	integer_t columnCount = 0;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	integer_t relationCount = 0;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
public:
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	ASN1Handler(std::ostream& output) : asn1Writer(new ASN1Writer(output)) {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		valueCount = 0;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		columnCount = 0;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		if (relationCount == 0) {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
			asn1Writer->writeStartSequence(); // root
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		} else {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
			asn1Writer->writeEndSequence();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
			asn1Writer->writeEndSequence();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		relationCount++;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		asn1Writer->writeStartSequence(); // relation
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		asn1Writer->writeString(name);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		asn1Writer->writeStartSequence(); // relation metadata
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		columnCount = attributes.size();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		columnTypes.resize(columnCount);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		columnTypeCodes.resize(columnCount);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		columnNames.resize(columnCount);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		for (int i = 0; i < attributes.size(); i++) {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			columnNames[i] = attributes[i].getAttributeName();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
			columnTypes[i] = attributes[i].getTypeId();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
			columnTypeCodes[i] = attributes[i].getTypeName();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
			
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
			asn1Writer->writeStartSequence(); // attribute-metadata
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
			asn1Writer->writeString(columnNames[i]);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
			asn1Writer->writeString(columnTypeCodes[i]);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
			asn1Writer->writeEndSequence();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
		}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		asn1Writer->writeEndSequence();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
	void attribute(const void* value, const std::type_info& type) override {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
		integer_t i = valueCount % columnCount;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
		if (i == 0 && valueCount) asn1Writer->writeEndSequence();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
		if (i == 0) asn1Writer->writeStartSequence();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
		valueCount++;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
		
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
		switch (columnTypes[i]) {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
			case TypeId::BOOLEAN:
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
			{
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
				assert(type == typeid (boolean_t));
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
				auto* typedValue = static_cast<const boolean_t*> (value);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
				asn1Writer->writeBoolean(*typedValue);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
				break;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
			}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
			case TypeId::INTEGER:
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
			{
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
				assert(type == typeid (integer_t));
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
				auto* typedValue = static_cast<const integer_t*> (value);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
				asn1Writer->writeInteger(*typedValue);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
				break;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
			}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
			case TypeId::STRING:
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
			{
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
				assert(type == typeid (string_t));
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
				auto* typedValue = static_cast<const string_t*> (value);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
				asn1Writer->writeString(*typedValue);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
				break;
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
			}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
			default:
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
				throw cli::RelpipeCLIException(L"Unsupported type in ASN1Handler.attribute()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR);
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
		}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
		
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
	}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
	void endOfPipe() {
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
		if (valueCount) asn1Writer->writeEndSequence();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
		if (relationCount) asn1Writer->writeEndSequence();
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
		asn1Writer->writeEndSequence();
1
e9b4f3a9e436 first working version with ASN.1 BER output
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   131
		asn1Writer->end();
0
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
	}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   133
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   134
};
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   135
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   136
}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   137
}
f4f9cdf7ed59 project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
}