diff -r 000000000000 -r f4f9cdf7ed59 src/ASN1Writer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ASN1Writer.h Sat Mar 30 01:12:42 2019 +0100 @@ -0,0 +1,97 @@ +/** + * Relational pipes + * Copyright © 2018 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, either version 3 of the License, or + * (at your option) any later version. + * + * 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 . + */ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include +#include + +using namespace std; + +namespace relpipe { +namespace out { +namespace asn1 { + +using namespace relpipe::reader; + +/** + * A simple library for writing ASN.1 BER encoded files. + */ +class ASN1Writer { +private: + ostream& output; + wstring_convert> convertor; // only UTF8String is supported + int sequenceLevel = 0; + + void xxx_indent() { + // FIXME: remove + for (int i = 0; i < sequenceLevel; i++) output << " "; + } + +public: + + ASN1Writer(std::ostream& output) : output(output) { + } + + virtual ~ASN1Writer() { + if (sequenceLevel) output << "Unable to close ASN1Writer because there are not ended sequences." << endl; // FIXME: better error handling + output.flush(); + } + + void writeStartSequence() { + xxx_indent(); + output << "sequence start" << endl; + + sequenceLevel++; + } + + void writeEndSequence() { + if (sequenceLevel == 0) throw cli::RelpipeCLIException(L"Unable to writeEndSequence() if not sequence is currently started.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // should not happen, error in the program + + sequenceLevel--; + + xxx_indent(); + output << "sequence end" << endl; + } + + void writeBoolean(const boolean_t& value) { + xxx_indent(); + output << "boolean" << (value ? "true" : "false") << endl; + } + + void writeInteger(const integer_t& value) { + xxx_indent(); + output << "integer: " << value << endl; + } + + void writeString(const string_t& value) { + xxx_indent(); + output << "string: " << convertor.to_bytes(value) << endl; + } + +}; + +} +} +} \ No newline at end of file