1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef COMMON_SUBSTREAM_H
24 #define COMMON_SUBSTREAM_H
25 
26 #include "common/ptr.h"
27 #include "common/stream.h"
28 #include "common/types.h"
29 
30 namespace Common {
31 
32 /**
33  * SubReadStream provides access to a ReadStream restricted to the range
34  * [currentPosition, currentPosition+end).
35  *
36  * Manipulating the parent stream directly /will/ mess up a substream.
37  * Likewise, manipulating two substreams of a parent stream will cause them to
38  * step on each others toes.
39  */
40 class SubReadStream : virtual public ReadStream {
41 protected:
42 	DisposablePtr<ReadStream> _parentStream;
43 	uint32 _pos;
44 	uint32 _end;
45 	bool _eos;
46 public:
47 	SubReadStream(ReadStream *parentStream, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO)
_parentStream(parentStream,disposeParentStream)48 		: _parentStream(parentStream, disposeParentStream),
49 		  _pos(0),
50 		  _end(end),
51 		  _eos(false) {
52 		assert(parentStream);
53 	}
54 
eos()55 	virtual bool eos() const { return _eos | _parentStream->eos(); }
err()56 	virtual bool err() const { return _parentStream->err(); }
clearErr()57 	virtual void clearErr() { _eos = false; _parentStream->clearErr(); }
58 	virtual uint32 read(void *dataPtr, uint32 dataSize);
59 };
60 
61 /*
62  * SeekableSubReadStream provides access to a SeekableReadStream restricted to
63  * the range [begin, end).
64  * The same caveats apply to SeekableSubReadStream as do to SeekableReadStream.
65  *
66  * Manipulating the parent stream directly /will/ mess up a substream.
67  * @see SubReadStream
68  */
69 class SeekableSubReadStream : public SubReadStream, public SeekableReadStream {
70 protected:
71 	SeekableReadStream *_parentStream;
72 	uint32 _begin;
73 public:
74 	SeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
75 
pos()76 	virtual int32 pos() const { return _pos - _begin; }
size()77 	virtual int32 size() const { return _end - _begin; }
78 
79 	virtual bool seek(int32 offset, int whence = SEEK_SET);
80 };
81 
82 /**
83  * This is a SeekableSubReadStream subclass which adds non-endian
84  * read methods whose endianness is set on the stream creation.
85  *
86  * Manipulating the parent stream directly /will/ mess up a substream.
87  * @see SubReadStream
88  */
89 class SeekableSubReadStreamEndian : public SeekableSubReadStream, public ReadStreamEndian {
90 public:
91 	SeekableSubReadStreamEndian(SeekableReadStream *parentStream, uint32 begin, uint32 end, bool bigEndian, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO)
SeekableSubReadStream(parentStream,begin,end,disposeParentStream)92 		: SeekableSubReadStream(parentStream, begin, end, disposeParentStream),
93 		  ReadStreamEndian(bigEndian) {
94 	}
95 };
96 
97 /**
98  * A seekable substream that removes the exclusivity demand required by the
99  * normal SeekableSubReadStream, at the cost of seek()ing the parent stream
100  * before each read().
101  *
102  * More than one SafeSeekableSubReadStream to the same parent stream can be used
103  * at the same time; they won't mess up each other. They will, however,
104  * reposition the parent stream, so don't depend on its position to be
105  * the same after a read() or seek() on one of its SafeSeekableSubReadStream.
106  *
107  * Note that this stream is *not* threading safe. Calling read from the audio
108  * thread and from the main thread might mess up the data retrieved.
109  */
110 class SafeSeekableSubReadStream : public SeekableSubReadStream {
111 public:
112 	SafeSeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO)
SeekableSubReadStream(parentStream,begin,end,disposeParentStream)113 		: SeekableSubReadStream(parentStream, begin, end, disposeParentStream) {
114 	}
115 
116 	virtual uint32 read(void *dataPtr, uint32 dataSize);
117 };
118 
119 
120 } // End of namespace Common
121 
122 #endif
123