src/FreeformASN1ContentHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 25 Jul 2021 11:53:55 +0200
branchv_0
changeset 2 7128fabeede0
child 3 807f8543d10e
permissions -rw-r--r--
FreeformASN1ContentHandler: basic ASN.1 tree in a relation

/**
 * Relational pipes
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <relpipe/writer/RelationalWriter.h>
#include "lib/ASN1ContentHandler.h"
#include "Configuration.h"

namespace relpipe {
namespace in {
namespace asn1 {

class FreeformASN1ContentHandler : public lib::ASN1ContentHandler {
private:
	std::shared_ptr<relpipe::writer::RelationalWriter> writer;
	Configuration configuration;
	std::vector<relpipe::common::type::Integer> position;

	class Record {
	public:
		const Header* header;
		const std::vector<relpipe::common::type::Integer>& position;
		relpipe::common::type::Integer level = 0;
		relpipe::common::type::StringX event;

		Record(const Header* header, std::vector<relpipe::common::type::Integer>& position, relpipe::common::type::StringX event) : header(header), position(position), event(event) {
		}

		virtual ~Record() {
		}
	};

	void write(const Record& r) {
		relpipe::common::type::Integer tag = r.header ? r.header->tag : -1; // TODO: null + possible integer overflow
		relpipe::common::type::Integer id = r.position.back();
		relpipe::common::type::Integer parent = r.position[r.position.size() - 2];
		relpipe::common::type::Integer level = r.position.size() - 2;

		writer->writeAttribute(&id, typeid (id));
		writer->writeAttribute(&parent, typeid (parent));
		writer->writeAttribute(&level, typeid (level));
		writer->writeAttribute(&r.event, typeid (r.event));
		writer->writeAttribute(&tag, typeid (tag));
	}

public:

	FreeformASN1ContentHandler(std::shared_ptr<relpipe::writer::RelationalWriter> writer, Configuration configuration) : writer(writer), configuration(configuration) {
	}

	bool setOption(const std::string& uri, const std::string& value) override {
		return false;
	}

	void writeStreamStart() override {
		writer->startRelation(configuration.relation,{
			{L"id", relpipe::writer::TypeId::INTEGER},
			{L"parent", relpipe::writer::TypeId::INTEGER},
			{L"level", relpipe::writer::TypeId::INTEGER},
			{L"event", relpipe::writer::TypeId::STRING},
			//{L"pc", relpipe::writer::TypeId::STRING},
			//{L"tag_class", relpipe::writer::TypeId::STRING},
			{L"tag", relpipe::writer::TypeId::INTEGER},
		}, true);

		position.push_back(-1); // TODO: null
		position.push_back(1);
		Record r(nullptr, position, L"stream-start");
		write(r);
		position.push_back(position.back());
	}

	void writeStreamEnd() override {
		auto id = position.back() + 1;
		position.pop_back();
		position.back() = id;
		Record r(nullptr, position, L"stream-end");
		write(r);
	}

	void writeCollectionStart(const Header& header) override {
		position.back()++;
		Record r(&header, position, L"collection-start");
		write(r);
		position.push_back(position.back());
	}

	void writeCollectionEnd() override {
		auto id = position.back() + 1;
		position.pop_back();
		position.back() = id;
		Record r(nullptr, position, L"collection-end");
		write(r);
	}

	void writeBitString(const Header& header, std::vector<bool> value) override {
		position.back()++;
		Record r(&header, position, L"bit-string");
		write(r);
	}

	void writeBoolean(const Header& header, bool value) override {
		position.back()++;
		Record r(&header, position, L"boolean");
		write(r);
	}

	void writeDateTime(const Header& header, DateTime value) override {
		position.back()++;
		Record r(&header, position, L"date-time");
		write(r);
	}

	void writeInteger(const Header& header, Integer value) override {
		position.back()++;
		Record r(&header, position, L"integer");
		write(r);
	}

	void writeNull(const Header& header) override {
		position.back()++;
		Record r(&header, position, L"null");
		write(r);
	}

	void writeOID(const Header& header, ObjectIdentifier value) override {
		position.back()++;
		Record r(&header, position, L"oid");
		write(r);
	}

	void writeOctetString(const Header& header, std::string value) override {
		position.back()++;
		Record r(&header, position, L"octet-string");
		write(r);
	}

	void writeTextString(const Header& header, std::string value) override {
		position.back()++;
		Record r(&header, position, L"text-string");
		write(r);
	}

	void writeSpecific(const Header& header, std::string value) override {
		position.back()++;
		Record r(&header, position, L"specific");
		write(r);
	}



};

}
}
}