src/ASN1Command.cpp
branchv_0
changeset 2 7128fabeede0
parent 0 28294b895e5e
--- a/src/ASN1Command.cpp	Sat Jul 24 17:35:27 2021 +0200
+++ b/src/ASN1Command.cpp	Sun Jul 25 11:53:55 2021 +0200
@@ -26,11 +26,17 @@
 
 #include <relpipe/cli/CLI.h>
 
+#include "lib/ASN1ContentHandler.h"
+#include "lib/BasicASN1Reader.h"
+#include "lib/TransactionalBuffer.h"
+
 #include "ASN1Command.h"
+#include "RelpipeASN1ContentHandler.h"
+#include "FreeformASN1ContentHandler.h"
 
 using namespace std;
 using namespace relpipe::writer;
-//using namespace relpipe::in::asn1::lib;
+using namespace relpipe::in::asn1::lib;
 
 namespace relpipe {
 namespace in {
@@ -38,6 +44,32 @@
 
 void ASN1Command::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
 	// TODO: parse ASN.1 and write relational data
+	BasicASN1Reader reader;
+	std::shared_ptr<ASN1ContentHandler> asn1handler;
+
+	if (configuration.mode == Mode::Relpipe) asn1handler = std::make_shared<RelpipeASN1ContentHandler>(writer, configuration);
+	else if (configuration.mode == Mode::Freeform) asn1handler = std::make_shared<FreeformASN1ContentHandler>(writer, configuration);
+	else throw RelpipeWriterException(L"Unsupported mode in ASN1Command: " + std::to_wstring((int) configuration.mode));
+
+	for (ParserOptionRecipe o : configuration.parserOptions) {
+		int n = 0;
+		n += reader.setOption(convertor.to_bytes(o.uri), convertor.to_bytes(o.value));
+		n += asn1handler->setOption(convertor.to_bytes(o.uri), convertor.to_bytes(o.value));
+		if (n == 0) throw RelpipeWriterException(L"Invalid parser option: „" + o.uri + L"“ with value: „" + o.value + L"“");
+	}
+
+	reader.addHandler(asn1handler);
+
+	try {
+		// TODO: buffering? (reader itself also buffers)
+		for (uint8_t b = input.get(); input.good(); b = input.get()) reader.write(&b, 1);
+	} catch (const relpipe::in::asn1::lib::TransactionalBuffer::WriteBufferOverflowException& e) {
+		// TODO: avoid leaky abstraction and use different exception
+		throw relpipe::writer::RelpipeWriterException(L"Transactional buffer for ASN.1 input is too small");
+	}
+
+	reader.close();
+
 
 }