sources/network/bytestream.h

Tue, 16 Dec 2014 04:08:31 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 16 Dec 2014 04:08:31 +0200
changeset 65
8f47c682e248
parent 13
09dcaeaa216b
child 73
07dda51a7a8e
permissions
-rw-r--r--

Added tag 1.0 for changeset 5e968dc8d552

/*
	Copyright 2014 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. Neither the name of the copyright holder nor the names of its
	   contributors may be used to endorse or promote products derived from
	   this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
	PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
	OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once
#include <stdexcept>
#include "../main.h"

class String;

enum { MAX_NETWORK_STRING = 0x800 };

// TODO: Make able to handle big-endian too
class Bytestream
{
public:
	class IOError : public std::exception
	{
		String m_message;

	public:
		IOError (String message) :
			m_message (message) {}

		inline METHOD
		what() const throw() -> const char*
		{
			return m_message.chars();
		}
	};

	Bytestream (unsigned long length = 0x800);
	Bytestream (const unsigned char* data, unsigned long length);
	Bytestream (const Vector<unsigned char>& bytes);
	Bytestream (const Bytestream& other);
	~Bytestream();

	inline METHOD allocated_size() const -> unsigned long;
	inline METHOD bytes_left() const -> unsigned long;
	       METHOD clear() -> void;
	inline METHOD data() -> unsigned char*;
	inline METHOD data() const -> const unsigned char*;
	       METHOD grow_to_fit (unsigned long bytes) -> void;
	inline METHOD position() const -> unsigned long;
	       METHOD read (unsigned char* buffer, unsigned long length) -> void;
	       METHOD read_byte() -> char;
	       METHOD read_short() -> short int;
	       METHOD read_long() -> long int;
	       METHOD read_string() -> String;
	       METHOD read_float() -> float;
	       METHOD resize (unsigned long length) -> void;
	inline METHOD rewind() -> void;
	inline METHOD seek (unsigned long pos) -> void;
	inline METHOD to_vector() const -> Vector<unsigned char>;
	       METHOD write (const unsigned char* val, unsigned int length) -> void;
	       METHOD write_buffer (const Bytestream& other) -> void;
	       METHOD write_buffer (const Vector<unsigned char>& other) -> void;
	       METHOD write_byte (char val) -> void;
	       METHOD write_double (double val) -> void;
	       METHOD write_float (float val) -> void;
	       METHOD write_long (long int val) -> void;
	       METHOD write_short (short int val) -> void;
	       METHOD write_string (const String& val) -> void;
	inline METHOD written_length() const -> unsigned long;

	inline METHOD operator[] (unsigned long idx) -> unsigned char&;
	inline METHOD operator[] (unsigned long idx) const -> unsigned char;
	       METHOD operator= (const Bytestream& other) -> Bytestream&;

private:
	unsigned char* m_data;
	unsigned char* m_cursor;
	unsigned long m_allocatedSize;
	unsigned long m_writtenLength;

	METHOD init (const unsigned char* data, unsigned long length) -> void;
	METHOD write (unsigned char val) -> void;
	METHOD ensure_read_space (unsigned int bytes) -> void;
	inline METHOD space_left() const -> unsigned long;
};

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::allocated_size() const -> unsigned long
{
	return m_allocatedSize;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::written_length() const -> unsigned long
{
	return m_writtenLength;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::operator[] (unsigned long idx) -> unsigned char&
{
	return m_data[idx];
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::operator[] (unsigned long idx) const -> unsigned char
{
	return m_data[idx];
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::position() const -> unsigned long
{
	return m_cursor - m_data;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::seek (unsigned long pos) -> void
{
	m_cursor = m_data + pos;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::rewind() -> void
{
	m_cursor = m_data;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::bytes_left() const -> unsigned long
{
	return (m_writtenLength - (m_cursor - &m_data[0]));
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::space_left() const -> unsigned long
{
	return (m_allocatedSize - m_writtenLength);
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::data() -> unsigned char*
{
	return m_data;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::data() const -> const unsigned char*
{
	return m_data;
}

// -------------------------------------------------------------------------------------------------
//
inline METHOD
Bytestream::to_vector() const -> Vector<unsigned char>
{
	return Vector<unsigned char> (m_data, m_writtenLength);
}

mercurial