/* Scumm Tools * Copyright (C) 2004-2006 The ScummVM Team * * 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 2 * 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * $URL: https://scummvm.svn.sourceforge.net/svnroot/scummvm/tools/tags/release-0-10-0/utils/stream.h $ * $Id: stream.h 23138 2006-06-15 15:44:06Z h00ligan $ * */ #ifndef COMMON_STREAM_H #define COMMON_STREAM_H #include "../util.h" namespace Common { /** * Virtual base class for both ReadStream and WriteStream. */ class Stream { public: virtual ~Stream() {} /** * Returns true if any I/O failure occured. * This flag is never cleared automatically. In order to clear it, * client code has to call clearIOFailed() explicitly. * * @todo Instead of returning a plain bool, maybe we should define * a list of error codes which can be returned here. */ virtual bool ioFailed() const { return false; } /** * Reset the I/O error status. */ virtual void clearIOFailed() {} }; /** * Generic interface for a readable data stream. */ class ReadStream : virtual public Stream { public: /** * Returns true if the end of the stream has been reached. */ virtual bool eos() const = 0; /** * Read data from the stream. Subclasses must implement this * method; all other read methods are implemented using it. * * @param dataPtr pointer to a buffer into which the data is read * @param dataSize number of bytes to be read * @return the number of bytes which were actually read. */ virtual uint32 read(void *dataPtr, uint32 dataSize) = 0; // The remaining methods all have default implementations; subclasses // need not (and should not) overload them. byte readByte() { byte b = 0; read(&b, 1); return b; } int8 readSByte() { int8 b = 0; read(&b, 1); return b; } uint16 readUint16LE() { uint16 a = readByte(); uint16 b = readByte(); return a | (b << 8); } uint32 readUint32LE() { uint32 a = readUint16LE(); uint32 b = readUint16LE(); return (b << 16) | a; } uint16 readUint16BE() { uint16 b = readByte(); uint16 a = readByte(); return a | (b << 8); } uint32 readUint32BE() { uint32 b = readUint16BE(); uint32 a = readUint16BE(); return (b << 16) | a; } int16 readSint16LE() { return (int16)readUint16LE(); } int32 readSint32LE() { return (int32)readUint32LE(); } int16 readSint16BE() { return (int16)readUint16BE(); } int32 readSint32BE() { return (int32)readUint32BE(); } }; /** * Interface for a seekable & readable data stream. * * @todo We really need better error handling here! * Like seek should somehow indicate whether it failed. */ class SeekableReadStream : public ReadStream { public: virtual uint32 pos() const = 0; virtual uint32 size() const = 0; virtual void seek(int32 offset, int whence = SEEK_SET) = 0; void skip(uint32 offset) { seek(offset, SEEK_CUR); } }; } // End of namespace Common #endif