xoreos  0.0.5
readstream.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos 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 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 /* Based on ScummVM (<http://scummvm.org>) code, which is released
26  * under the terms of version 2 or later of the GNU General Public
27  * License.
28  *
29  * The original copyright note in ScummVM reads as follows:
30  *
31  * ScummVM is the legal property of its developers, whose names
32  * are too numerous to list here. Please refer to the COPYRIGHT
33  * file distributed with this source distribution.
34  *
35  * This program is free software; you can redistribute it and/or
36  * modify it under the terms of the GNU General Public License
37  * as published by the Free Software Foundation; either version 2
38  * of the License, or (at your option) any later version.
39  *
40  * This program is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43  * GNU General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
48  */
49 
50 #include <cassert>
51 
52 #include "src/common/readstream.h"
54 #include "src/common/error.h"
55 #include "src/common/scopedptr.h"
56 
57 namespace Common {
58 
60 
62 }
63 
65 }
66 
68  ScopedArray<byte> buf(new byte[dataSize]);
69 
70  if (read(buf.get(), dataSize) != dataSize)
71  throw Exception(kReadError);
72 
73  return new MemoryReadStream(buf.release(), dataSize, true);
74 }
75 
76 
78 }
79 
81 }
82 
83 size_t SeekableReadStream::evalSeek(ptrdiff_t offset, Origin whence, size_t pos, size_t begin, size_t size) {
84  switch (whence) {
85  case kOriginEnd:
86  offset = size + offset;
88  case kOriginBegin:
89  return begin + offset;
90  case kOriginCurrent:
91  return pos + offset;
92 
93  default:
94  break;
95  }
96 
97  throw Exception("Invalid whence (%d)", (int) whence);
98 }
99 
100 
101 SubReadStream::SubReadStream(ReadStream *parentStream, size_t end, bool disposeParentStream) :
102  _parentStream(parentStream, disposeParentStream), _pos(0), _end(end), _eos(false) {
103 
104  assert(parentStream);
105 }
106 
108 }
109 
110 bool SubReadStream::eos() const {
111  return _eos | _parentStream->eos();
112 }
113 
114 size_t SubReadStream::read(void *dataPtr, size_t dataSize) {
115  if (dataSize > (size_t)(_end - _pos)) {
116  dataSize = _end - _pos;
117  _eos = true;
118  }
119 
120  dataSize = _parentStream->read(dataPtr, dataSize);
121  _pos += dataSize;
122 
123  return dataSize;
124 }
125 
126 
128  size_t end, bool disposeParentStream) :
129  SubReadStream(parentStream, end, disposeParentStream), _parentStream(parentStream), _begin(begin) {
130 
131  assert(_begin <= _end);
132 
133  _pos = begin;
135 }
136 
138 }
139 
141  return _pos - _begin;
142 }
143 
145  return _end - _begin;
146 }
147 
148 size_t SeekableSubReadStream::seek(ptrdiff_t offset, Origin whence) {
149  assert(_pos >= _begin);
150  assert(_pos <= _end);
151 
152  const size_t oldPos = _pos;
153  const size_t newPos = evalSeek(offset, whence, _pos, _begin, size());
154  if ((newPos < _begin) || (newPos > _end))
155  throw Exception(kSeekError);
156 
157  _pos = newPos;
158 
160  _eos = false; // reset eos on successful seek
161 
162  return oldPos;
163 }
164 
165 
167  size_t begin, size_t end, bool bigEndian, bool disposeParentStream) :
168  SeekableSubReadStream(parentStream, begin, end, disposeParentStream), _bigEndian(bigEndian) {
169 
170 }
171 
173 }
174 
175 } // End of namespace Common
Generic interface for a readable data stream.
Definition: readstream.h:64
Definition: 2dafile.h:39
virtual size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)=0
Sets the stream position indicator for the stream.
PointerType release()
Returns the plain pointer value and releases ScopedPtr.
Definition: scopedptr.h:103
DisposablePtr< ReadStream > _parentStream
Definition: readstream.h:343
MemoryReadStream * readStream(size_t dataSize)
Read the specified amount of data into a new[]&#39;ed buffer which then is wrapped into a MemoryReadStrea...
Definition: readstream.cpp:67
Implementing the reading stream interfaces for plain memory blocks.
size_t pos() const
Obtains the current value of the stream position indicator of the stream.
Definition: readstream.cpp:140
static const uint32 kEOF
Return value for end-of-file.
Definition: readstream.h:67
size_t size() const
Obtains the total size of the stream, measured in bytes.
Definition: readstream.cpp:144
Seek from the end of the stream.
Definition: readstream.h:271
A simple scoped smart pointer template.
Origin
The position a seeking offset takes as a base.
Definition: readstream.h:268
Basic exceptions to throw.
size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)
Sets the stream position indicator for the stream.
Definition: readstream.cpp:148
SubReadStream(ReadStream *parentStream, size_t end, bool disposeParentStream=false)
Definition: readstream.cpp:101
virtual size_t read(void *dataPtr, size_t dataSize)=0
Read data from the stream.
Simple memory based &#39;stream&#39;, which implements the ReadStream interface for a plain memory block...
Definition: memreadstream.h:66
virtual ~ReadStream()
Definition: readstream.cpp:64
StackException Exception
Definition: error.h:59
const Exception kReadError("Read error")
Exception when reading from a stream failed.
Definition: error.h:62
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
Basic reading stream interfaces.
virtual size_t pos() const =0
Obtains the current value of the stream position indicator of the stream.
size_t read(void *dataPtr, size_t dataSize)
Read data from the stream.
Definition: readstream.cpp:114
Seek from the current position of the stream.
Definition: readstream.h:270
#define XOREOS_FALLTHROUGH
Definition: fallthrough.h:60
SeekableSubReadStreamEndian(SeekableReadStream *parentStream, size_t begin, size_t end, bool bigEndian=false, bool disposeParentStream=false)
Definition: readstream.cpp:166
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
const Exception kSeekError("Seek error")
Exception when seeking a stream failed.
Definition: error.h:63
uint32_t uint32
Definition: types.h:204
static size_t evalSeek(ptrdiff_t offset, Origin whence, size_t pos, size_t begin, size_t size)
Evaluate the seek offset relative to whence into a position from the beginning.
Definition: readstream.cpp:83
SeekableSubReadStream(SeekableReadStream *parentStream, size_t begin, size_t end, bool disposeParentStream=false)
Definition: readstream.cpp:127
bool eos() const
Returns true if a read failed because the stream has been reached.
Definition: readstream.cpp:110
SeekableSubReadStream provides access to a SeekableReadStream restricted to the range [begin...
Definition: readstream.h:359
Seek from the begin of the stream.
Definition: readstream.h:269
SeekableReadStream * _parentStream
Definition: readstream.h:371
SubReadStream provides access to a ReadStream restricted to the range [currentPosition, currentPosition+end).
Definition: readstream.h:333
Interface for a seekable & readable data stream.
Definition: readstream.h:265
uint8 byte
Definition: types.h:209