src/lib/AbstractParser.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 22 Jul 2021 01:06:14 +0200
branchv_0
changeset 39 6ef41443211e
parent 30 e27e133731ee
permissions -rw-r--r--
add @text attribute to octet-string if bytes are ASCII text

/**
 * 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 "AbstractParser.h"
#include "TransactionalBuffer.h"

namespace relpipe {
namespace in {
namespace asn1 {
namespace lib {

class AbstractParserImpl {
private:
	friend AbstractParser;
	AbstractParser* interface;
	TransactionalBuffer buffer;
public:

	AbstractParserImpl(AbstractParser* interface) : interface(interface) {
	}

	virtual ~AbstractParserImpl() {
	}
};

AbstractParser::AbstractParser() {
	implementation = new AbstractParserImpl(this);
}

AbstractParser::~AbstractParser() {
	delete implementation;
}

void AbstractParser::write(const uint8_t* buffer, const size_t length) {
	try {
		// TODO: do not write to the buffer, just append in read()/peek() and write just the part that was not read during this cycle
		implementation->buffer.write(buffer, length);

		// TODO: call an overridable method to get preferred minimum block size and run cycle only if we have enough data or EOF
		// and/or remember the length of last failed (rollbacked) read() call
		update();
		commit();
	} catch (const TransactionalBuffer::ReadBufferUnderflowException& e) {
		rollback();
	} catch (const AbstractParser::ExplicitRollbackException& e) {
		rollback();
	}
}

void AbstractParser::close() {
	// TODO: check remaining data + call update() or just let parsers override this method?
}

void AbstractParser::rollback() {
	// TODO: notify rollback listeners? (they can monitor the performance / frequency of rollbacks)
	implementation->buffer.rollbackRead();
}

void AbstractParser::commit() {
	implementation->buffer.commitRead();
}

void AbstractParser::read(uint8_t* buffer, const size_t length) {
	implementation->buffer.read(buffer, length);
}

void AbstractParser::peek(uint8_t* buffer, const size_t length) {
	implementation->buffer.peek(buffer, length);
}

size_t AbstractParser::getBytesRead() {
	return implementation->buffer.getBytesRead();
}

}
}
}
}