src/ASN1Command.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 26 Jul 2021 23:41:18 +0200
branchv_0
changeset 6 523c9c39c996
parent 2 7128fabeede0
permissions -rw-r--r--
RelpipeASN1ContentHandler: parse one or more relations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
28294b895e5e project skeleton + configuration
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
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <cstdlib>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <vector>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <sstream>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/writer/RelationalWriter.h>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/writer/RelpipeWriterException.h>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/writer/AttributeMetadata.h>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <relpipe/common/type/typedefs.h>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/cli/CLI.h>
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
2
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    29
#include "lib/ASN1ContentHandler.h"
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    30
#include "lib/BasicASN1Reader.h"
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    31
#include "lib/TransactionalBuffer.h"
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    32
0
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include "ASN1Command.h"
2
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    34
#include "RelpipeASN1ContentHandler.h"
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    35
#include "FreeformASN1ContentHandler.h"
0
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
using namespace std;
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
using namespace relpipe::writer;
2
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    39
using namespace relpipe::in::asn1::lib;
0
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
namespace relpipe {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
namespace in {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
namespace asn1 {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
void ASN1Command::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	// TODO: parse ASN.1 and write relational data
2
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    47
	BasicASN1Reader reader;
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
	std::shared_ptr<ASN1ContentHandler> asn1handler;
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
	if (configuration.mode == Mode::Relpipe) asn1handler = std::make_shared<RelpipeASN1ContentHandler>(writer, configuration);
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
	else if (configuration.mode == Mode::Freeform) asn1handler = std::make_shared<FreeformASN1ContentHandler>(writer, configuration);
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
	else throw RelpipeWriterException(L"Unsupported mode in ASN1Command: " + std::to_wstring((int) configuration.mode));
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
	for (ParserOptionRecipe o : configuration.parserOptions) {
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
		int n = 0;
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    56
		n += reader.setOption(convertor.to_bytes(o.uri), convertor.to_bytes(o.value));
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
		n += asn1handler->setOption(convertor.to_bytes(o.uri), convertor.to_bytes(o.value));
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
		if (n == 0) throw RelpipeWriterException(L"Invalid parser option: „" + o.uri + L"“ with value: „" + o.value + L"“");
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
	}
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    60
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
	reader.addHandler(asn1handler);
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
	try {
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
		// TODO: buffering? (reader itself also buffers)
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
		for (uint8_t b = input.get(); input.good(); b = input.get()) reader.write(&b, 1);
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
	} catch (const relpipe::in::asn1::lib::TransactionalBuffer::WriteBufferOverflowException& e) {
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
		// TODO: avoid leaky abstraction and use different exception
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
		throw relpipe::writer::RelpipeWriterException(L"Transactional buffer for ASN.1 input is too small");
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
	}
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
	reader.close();
7128fabeede0 FreeformASN1ContentHandler: basic ASN.1 tree in a relation
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
0
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
}
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
}
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
}
28294b895e5e project skeleton + configuration
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
}