src/RecfileWriter.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 30 Mar 2019 01:12:42 +0100
branchv_0
changeset 0 9005fdd81bca
permissions -rw-r--r--
project and code skeleton: text output

/**
 * 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 <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <codecvt>
#include <locale>

#include <relpipe/reader/typedefs.h>
#include <relpipe/cli/RelpipeCLIException.h>

using namespace std;

namespace relpipe {
namespace out {
namespace recfile {

using namespace relpipe::reader;

/**
 * A simple library for writing ASN.1 BER encoded files.
 */
class RecfileWriter {
private:
	ostream& output;
	wstring_convert<codecvt_utf8<wchar_t>> convertor; // only UTF8String is supported
	int sequenceLevel = 0;

	void xxx_indent() {
		// FIXME: remove
		for (int i = 0; i < sequenceLevel; i++) output << "  ";
	}

public:

	RecfileWriter(std::ostream& output) : output(output) {
	}

	virtual ~RecfileWriter() {
		if (sequenceLevel) output << "Unable to close RecfileWriter 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;
	}

};

}
}
}