2010-11-19 17:03:07 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* 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.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2010-11-19 17:03:07 +00:00
|
|
|
* 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.
|
2014-02-18 01:34:18 +00:00
|
|
|
*
|
2010-11-19 17:03:07 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COMMON_SUBSTREAM_H
|
|
|
|
#define COMMON_SUBSTREAM_H
|
|
|
|
|
2011-08-06 14:30:52 +00:00
|
|
|
#include "common/ptr.h"
|
2010-11-19 17:03:07 +00:00
|
|
|
#include "common/stream.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/types.h"
|
2010-11-19 17:03:07 +00:00
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2020-07-08 21:30:36 +00:00
|
|
|
/**
|
|
|
|
* @defgroup common_substream Substreams
|
|
|
|
* @ingroup common_stream
|
|
|
|
*
|
|
|
|
* @brief API for managing readable data substreams.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-11-19 17:03:07 +00:00
|
|
|
/**
|
|
|
|
* SubReadStream provides access to a ReadStream restricted to the range
|
|
|
|
* [currentPosition, currentPosition+end).
|
|
|
|
*
|
|
|
|
* Manipulating the parent stream directly /will/ mess up a substream.
|
|
|
|
* Likewise, manipulating two substreams of a parent stream will cause them to
|
|
|
|
* step on each others toes.
|
|
|
|
*/
|
|
|
|
class SubReadStream : virtual public ReadStream {
|
|
|
|
protected:
|
2011-08-06 14:30:52 +00:00
|
|
|
DisposablePtr<ReadStream> _parentStream;
|
2010-11-19 17:03:07 +00:00
|
|
|
uint32 _pos;
|
|
|
|
uint32 _end;
|
|
|
|
bool _eos;
|
|
|
|
public:
|
|
|
|
SubReadStream(ReadStream *parentStream, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO)
|
2011-08-06 14:30:52 +00:00
|
|
|
: _parentStream(parentStream, disposeParentStream),
|
2010-11-19 17:03:07 +00:00
|
|
|
_pos(0),
|
|
|
|
_end(end),
|
|
|
|
_eos(false) {
|
|
|
|
assert(parentStream);
|
|
|
|
}
|
|
|
|
|
2011-04-19 16:08:47 +00:00
|
|
|
virtual bool eos() const { return _eos | _parentStream->eos(); }
|
2010-11-19 17:03:07 +00:00
|
|
|
virtual bool err() const { return _parentStream->err(); }
|
|
|
|
virtual void clearErr() { _eos = false; _parentStream->clearErr(); }
|
|
|
|
virtual uint32 read(void *dataPtr, uint32 dataSize);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SeekableSubReadStream provides access to a SeekableReadStream restricted to
|
|
|
|
* the range [begin, end).
|
|
|
|
* The same caveats apply to SeekableSubReadStream as do to SeekableReadStream.
|
|
|
|
*
|
|
|
|
* Manipulating the parent stream directly /will/ mess up a substream.
|
|
|
|
* @see SubReadStream
|
|
|
|
*/
|
2020-07-21 16:19:01 +00:00
|
|
|
class SeekableSubReadStream : public SubReadStream, virtual public SeekableReadStream {
|
2010-11-19 17:03:07 +00:00
|
|
|
protected:
|
|
|
|
SeekableReadStream *_parentStream;
|
|
|
|
uint32 _begin;
|
|
|
|
public:
|
|
|
|
SeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
|
|
|
|
|
|
|
|
virtual int32 pos() const { return _pos - _begin; }
|
|
|
|
virtual int32 size() const { return _end - _begin; }
|
|
|
|
|
|
|
|
virtual bool seek(int32 offset, int whence = SEEK_SET);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2010-11-23 22:26:27 +00:00
|
|
|
* This is a SeekableSubReadStream subclass which adds non-endian
|
2010-11-19 17:03:07 +00:00
|
|
|
* read methods whose endianness is set on the stream creation.
|
|
|
|
*
|
|
|
|
* Manipulating the parent stream directly /will/ mess up a substream.
|
|
|
|
* @see SubReadStream
|
|
|
|
*/
|
2020-07-21 16:19:01 +00:00
|
|
|
class SeekableSubReadStreamEndian : virtual public SeekableSubReadStream, virtual public SeekableReadStreamEndian {
|
2010-11-19 17:03:07 +00:00
|
|
|
public:
|
2010-11-23 22:27:20 +00:00
|
|
|
SeekableSubReadStreamEndian(SeekableReadStream *parentStream, uint32 begin, uint32 end, bool bigEndian, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO)
|
2010-11-23 22:26:27 +00:00
|
|
|
: SeekableSubReadStream(parentStream, begin, end, disposeParentStream),
|
2020-07-21 16:19:01 +00:00
|
|
|
SeekableReadStreamEndian(bigEndian),
|
2010-11-23 22:26:27 +00:00
|
|
|
ReadStreamEndian(bigEndian) {
|
2010-11-19 17:03:07 +00:00
|
|
|
}
|
2020-07-21 16:19:01 +00:00
|
|
|
|
2021-03-30 09:39:43 +00:00
|
|
|
virtual int32 pos() const override { return SeekableSubReadStream::pos(); }
|
|
|
|
virtual int32 size() const override { return SeekableSubReadStream::size(); }
|
2020-07-21 16:19:01 +00:00
|
|
|
|
2021-03-30 09:39:43 +00:00
|
|
|
virtual bool seek(int32 offset, int whence = SEEK_SET) override { return SeekableSubReadStream::seek(offset, whence); }
|
2020-07-21 16:19:01 +00:00
|
|
|
void hexdump(int len, int bytesPerLine = 16, int startOffset = 0) { SeekableSubReadStream::hexdump(len, bytesPerLine, startOffset); }
|
2021-03-30 09:39:43 +00:00
|
|
|
bool skip(uint32 offset) override { return SeekableSubReadStream::seek(offset, SEEK_CUR); }
|
2010-11-19 17:03:07 +00:00
|
|
|
};
|
|
|
|
|
2011-05-05 19:37:48 +00:00
|
|
|
/**
|
|
|
|
* A seekable substream that removes the exclusivity demand required by the
|
|
|
|
* normal SeekableSubReadStream, at the cost of seek()ing the parent stream
|
|
|
|
* before each read().
|
|
|
|
*
|
2012-03-10 23:51:20 +00:00
|
|
|
* More than one SafeSeekableSubReadStream to the same parent stream can be used
|
2011-05-05 19:37:48 +00:00
|
|
|
* at the same time; they won't mess up each other. They will, however,
|
|
|
|
* reposition the parent stream, so don't depend on its position to be
|
2012-03-10 23:51:20 +00:00
|
|
|
* the same after a read() or seek() on one of its SafeSeekableSubReadStream.
|
2012-03-10 23:53:23 +00:00
|
|
|
*
|
|
|
|
* Note that this stream is *not* threading safe. Calling read from the audio
|
|
|
|
* thread and from the main thread might mess up the data retrieved.
|
2011-05-05 19:37:48 +00:00
|
|
|
*/
|
2012-03-10 23:51:20 +00:00
|
|
|
class SafeSeekableSubReadStream : public SeekableSubReadStream {
|
2011-05-05 19:37:48 +00:00
|
|
|
public:
|
2012-03-10 23:51:20 +00:00
|
|
|
SafeSeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO)
|
2012-03-10 01:01:05 +00:00
|
|
|
: SeekableSubReadStream(parentStream, begin, end, disposeParentStream) {
|
2011-05-05 19:37:48 +00:00
|
|
|
}
|
|
|
|
|
2012-03-10 01:01:05 +00:00
|
|
|
virtual uint32 read(void *dataPtr, uint32 dataSize);
|
2011-05-05 19:37:48 +00:00
|
|
|
};
|
|
|
|
|
2020-07-08 21:30:36 +00:00
|
|
|
/** @} */
|
2010-11-19 17:03:07 +00:00
|
|
|
|
2012-03-10 01:01:05 +00:00
|
|
|
} // End of namespace Common
|
2010-11-19 17:03:07 +00:00
|
|
|
|
|
|
|
#endif
|