xoreos  0.0.5
memreadstream.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 #include <cstring>
52 
54 #include "src/common/error.h"
55 #include "src/common/util.h"
56 
57 namespace Common {
58 
59 size_t MemoryReadStream::read(void *dataPtr, size_t dataSize) {
60  assert(dataPtr);
61 
62  // Read at most as many bytes as are still available...
63  if (dataSize > _size - _pos) {
64  dataSize = _size - _pos;
65  _eos = true;
66  }
67  std::memcpy(dataPtr, _ptr, dataSize);
68 
69  _ptr += dataSize;
70  _pos += dataSize;
71 
72  return dataSize;
73 }
74 
75 size_t MemoryReadStream::seek(ptrdiff_t offset, Origin whence) {
76  assert((size_t)_pos <= _size);
77 
78  const size_t oldPos = _pos;
79  const size_t newPos = evalSeek(offset, whence, _pos, 0, size());
80  if (newPos > _size)
81  throw Exception(kSeekError);
82 
83  _pos = newPos;
84  _ptr = _ptrOrig.get() + newPos;
85 
86  // Reset end-of-stream flag on a successful seek
87  _eos = false;
88 
89  return oldPos;
90 }
91 
92 bool MemoryReadStream::eos() const {
93  return _eos;
94 }
95 
96 size_t MemoryReadStream::pos() const {
97  return _pos;
98 }
99 
100 size_t MemoryReadStream::size() const {
101  return _size;
102 }
103 
105  return _ptrOrig.get();
106 }
107 
108 
109 MemoryReadStreamEndian::MemoryReadStreamEndian(const byte *dataPtr, size_t dataSize,
110  bool bigEndian, bool disposeMemory) :
111  MemoryReadStream(dataPtr, dataSize, disposeMemory), _bigEndian(bigEndian) {
112 
113 }
114 
116 }
117 
118 } // End of namespace Common
MemoryReadStreamEndian(const byte *dataPtr, size_t dataSize, bool bigEndian=false, bool disposeMemory=false)
Definition: 2dafile.h:39
Implementing the reading stream interfaces for plain memory blocks.
DisposableArray< const byte > _ptrOrig
size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)
Sets the stream position indicator for the stream.
Origin
The position a seeking offset takes as a base.
Definition: readstream.h:268
Basic exceptions to throw.
const byte * getData() const
Utility templates and functions.
bool eos() const
Returns true if a read failed because the stream has been reached.
Simple memory based &#39;stream&#39;, which implements the ReadStream interface for a plain memory block...
Definition: memreadstream.h:66
StackException Exception
Definition: error.h:59
const Exception kSeekError("Seek error")
Exception when seeking a stream failed.
Definition: error.h:63
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
size_t size() const
Obtains the total size of the stream, measured in bytes.
PointerType get() const
Returns the plain pointer value.
Definition: disposableptr.h:98
size_t pos() const
Obtains the current value of the stream position indicator of the stream.
uint8 byte
Definition: types.h:209
size_t read(void *dataPtr, size_t dataSize)
Read data from the stream.