src/FreeformASN1ContentHandler.h
branchv_0
changeset 2 7128fabeede0
child 3 807f8543d10e
equal deleted inserted replaced
1:68a281aefa76 2:7128fabeede0
       
     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 <relpipe/writer/RelationalWriter.h>
       
    20 #include "lib/ASN1ContentHandler.h"
       
    21 #include "Configuration.h"
       
    22 
       
    23 namespace relpipe {
       
    24 namespace in {
       
    25 namespace asn1 {
       
    26 
       
    27 class FreeformASN1ContentHandler : public lib::ASN1ContentHandler {
       
    28 private:
       
    29 	std::shared_ptr<relpipe::writer::RelationalWriter> writer;
       
    30 	Configuration configuration;
       
    31 	std::vector<relpipe::common::type::Integer> position;
       
    32 
       
    33 	class Record {
       
    34 	public:
       
    35 		const Header* header;
       
    36 		const std::vector<relpipe::common::type::Integer>& position;
       
    37 		relpipe::common::type::Integer level = 0;
       
    38 		relpipe::common::type::StringX event;
       
    39 
       
    40 		Record(const Header* header, std::vector<relpipe::common::type::Integer>& position, relpipe::common::type::StringX event) : header(header), position(position), event(event) {
       
    41 		}
       
    42 
       
    43 		virtual ~Record() {
       
    44 		}
       
    45 	};
       
    46 
       
    47 	void write(const Record& r) {
       
    48 		relpipe::common::type::Integer tag = r.header ? r.header->tag : -1; // TODO: null + possible integer overflow
       
    49 		relpipe::common::type::Integer id = r.position.back();
       
    50 		relpipe::common::type::Integer parent = r.position[r.position.size() - 2];
       
    51 		relpipe::common::type::Integer level = r.position.size() - 2;
       
    52 
       
    53 		writer->writeAttribute(&id, typeid (id));
       
    54 		writer->writeAttribute(&parent, typeid (parent));
       
    55 		writer->writeAttribute(&level, typeid (level));
       
    56 		writer->writeAttribute(&r.event, typeid (r.event));
       
    57 		writer->writeAttribute(&tag, typeid (tag));
       
    58 	}
       
    59 
       
    60 public:
       
    61 
       
    62 	FreeformASN1ContentHandler(std::shared_ptr<relpipe::writer::RelationalWriter> writer, Configuration configuration) : writer(writer), configuration(configuration) {
       
    63 	}
       
    64 
       
    65 	bool setOption(const std::string& uri, const std::string& value) override {
       
    66 		return false;
       
    67 	}
       
    68 
       
    69 	void writeStreamStart() override {
       
    70 		writer->startRelation(configuration.relation,{
       
    71 			{L"id", relpipe::writer::TypeId::INTEGER},
       
    72 			{L"parent", relpipe::writer::TypeId::INTEGER},
       
    73 			{L"level", relpipe::writer::TypeId::INTEGER},
       
    74 			{L"event", relpipe::writer::TypeId::STRING},
       
    75 			//{L"pc", relpipe::writer::TypeId::STRING},
       
    76 			//{L"tag_class", relpipe::writer::TypeId::STRING},
       
    77 			{L"tag", relpipe::writer::TypeId::INTEGER},
       
    78 		}, true);
       
    79 
       
    80 		position.push_back(-1); // TODO: null
       
    81 		position.push_back(1);
       
    82 		Record r(nullptr, position, L"stream-start");
       
    83 		write(r);
       
    84 		position.push_back(position.back());
       
    85 	}
       
    86 
       
    87 	void writeStreamEnd() override {
       
    88 		auto id = position.back() + 1;
       
    89 		position.pop_back();
       
    90 		position.back() = id;
       
    91 		Record r(nullptr, position, L"stream-end");
       
    92 		write(r);
       
    93 	}
       
    94 
       
    95 	void writeCollectionStart(const Header& header) override {
       
    96 		position.back()++;
       
    97 		Record r(&header, position, L"collection-start");
       
    98 		write(r);
       
    99 		position.push_back(position.back());
       
   100 	}
       
   101 
       
   102 	void writeCollectionEnd() override {
       
   103 		auto id = position.back() + 1;
       
   104 		position.pop_back();
       
   105 		position.back() = id;
       
   106 		Record r(nullptr, position, L"collection-end");
       
   107 		write(r);
       
   108 	}
       
   109 
       
   110 	void writeBitString(const Header& header, std::vector<bool> value) override {
       
   111 		position.back()++;
       
   112 		Record r(&header, position, L"bit-string");
       
   113 		write(r);
       
   114 	}
       
   115 
       
   116 	void writeBoolean(const Header& header, bool value) override {
       
   117 		position.back()++;
       
   118 		Record r(&header, position, L"boolean");
       
   119 		write(r);
       
   120 	}
       
   121 
       
   122 	void writeDateTime(const Header& header, DateTime value) override {
       
   123 		position.back()++;
       
   124 		Record r(&header, position, L"date-time");
       
   125 		write(r);
       
   126 	}
       
   127 
       
   128 	void writeInteger(const Header& header, Integer value) override {
       
   129 		position.back()++;
       
   130 		Record r(&header, position, L"integer");
       
   131 		write(r);
       
   132 	}
       
   133 
       
   134 	void writeNull(const Header& header) override {
       
   135 		position.back()++;
       
   136 		Record r(&header, position, L"null");
       
   137 		write(r);
       
   138 	}
       
   139 
       
   140 	void writeOID(const Header& header, ObjectIdentifier value) override {
       
   141 		position.back()++;
       
   142 		Record r(&header, position, L"oid");
       
   143 		write(r);
       
   144 	}
       
   145 
       
   146 	void writeOctetString(const Header& header, std::string value) override {
       
   147 		position.back()++;
       
   148 		Record r(&header, position, L"octet-string");
       
   149 		write(r);
       
   150 	}
       
   151 
       
   152 	void writeTextString(const Header& header, std::string value) override {
       
   153 		position.back()++;
       
   154 		Record r(&header, position, L"text-string");
       
   155 		write(r);
       
   156 	}
       
   157 
       
   158 	void writeSpecific(const Header& header, std::string value) override {
       
   159 		position.back()++;
       
   160 		Record r(&header, position, L"specific");
       
   161 		write(r);
       
   162 	}
       
   163 
       
   164 
       
   165 
       
   166 };
       
   167 
       
   168 }
       
   169 }
       
   170 }