/**
* 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/>.
*/
#include <cstdlib>
#include <vector>
#include <sstream>
#include <memory>
#include <relpipe/writer/RelationalWriter.h>
#include <relpipe/writer/RelpipeWriterException.h>
#include <relpipe/writer/AttributeMetadata.h>
#include <relpipe/common/type/typedefs.h>
#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;
namespace relpipe {
namespace in {
namespace asn1 {
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();
}
}
}
}