Thu, 11 Dec 2014 16:17:35 +0200
- now with more license headers
/* 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 "../main.h" class String; enum { MAX_NETWORK_STRING = 0x800 }; // TODO: Make able to handle big-endian too class Bytestream { public: static bool sink; Bytestream (unsigned long length = 0x800); Bytestream (const unsigned char* data, unsigned long length); Bytestream (const Vector<unsigned char>& bytes); ~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, bool* ok = &sink) -> void; METHOD read_byte (bool* ok = &sink) -> char; METHOD read_short (bool* ok = &sink) -> short int; METHOD read_long (bool* ok = &sink) -> long int; METHOD read_string (bool* ok = &sink) -> String; METHOD read_float (bool* ok = &sink) -> float; METHOD resize (unsigned long length) -> void; inline METHOD rewind() -> void; inline METHOD seek (unsigned long pos) -> void; 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; 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; 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; }