Thu, 23 Jul 2015 01:52:04 +0300
Now works with MSVC 2010/pdcurses-win32a
/* Copyright 2014, 2015 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" BEGIN_ZFC_NAMESPACE 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) {} const char* what() const throw() { return m_message; } }; Bytestream (unsigned long length = 0x800); Bytestream (const unsigned char* data, unsigned long length); Bytestream (const Vector<unsigned char>& bytes); Bytestream (const Bytestream& other); ~Bytestream(); void clear(); void grow_to_fit (unsigned long bytes); void read (unsigned char* buffer, unsigned long length); char read_byte(); short int read_short(); long int read_long(); String read_string(); float read_float(); void resize (unsigned long length); void write (const unsigned char* val, unsigned int length); void write_buffer (const Bytestream& other); void write_buffer (const Vector<unsigned char>& other); void write_byte (char val); void write_double (double val); void write_float (float val); void write_long (long int val); void write_short (short int val); void write_string (const String& val); Bytestream& operator= (const Bytestream& other); unsigned long allocated_size() const { return m_allocatedSize; } unsigned long bytes_left() const { return m_writtenLength - (m_cursor - &m_data[0]); } inline unsigned char* data() { return m_data; } inline const unsigned char* data() const { return m_data; } unsigned long position() const { return m_cursor - m_data; } void rewind() { m_cursor = m_data; } void seek (unsigned long pos) { m_cursor = m_data + pos; } Vector<unsigned char> to_vector() const { return Vector<unsigned char> (m_data, m_writtenLength); } unsigned long written_length() const { return m_writtenLength; } unsigned char& operator[] (unsigned long idx) { return m_data[idx]; } unsigned char operator[] (unsigned long idx) const { return m_data[idx]; } private: unsigned char* m_data; unsigned char* m_cursor; unsigned long m_allocatedSize; unsigned long m_writtenLength; void init (const unsigned char* data, unsigned long length); void write (unsigned char val); void ensure_read_space (unsigned int bytes); unsigned long space_left() const { return m_allocatedSize - m_writtenLength; } }; END_ZFC_NAMESPACE