xoreos  0.0.5
memwritestream.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 
53 #include "src/common/types.h"
55 
56 namespace Common {
57 
58 size_t MemoryWriteStream::write(const void *dataPtr, size_t dataSize) {
59  assert(dataPtr);
60 
61  // Write at most as many bytes as are still available...
62  if (dataSize > _bufSize - _pos)
63  dataSize = _bufSize - _pos;
64 
65  std::memcpy(_ptr, dataPtr, dataSize);
66 
67  _ptr += dataSize;
68  _pos += dataSize;
69 
70  return dataSize;
71 }
72 
73 size_t MemoryWriteStream::pos() const {
74  return _pos;
75 }
76 
77 size_t MemoryWriteStream::size() const {
78  return _bufSize;
79 }
80 
81 size_t MemoryWriteStream::seek(ptrdiff_t offset, SeekableWriteStream::Origin whence) {
82  assert((size_t)_pos <= _bufSize);
83 
84  const size_t oldPos = _pos;
85  const size_t newPos = evalSeek(offset, whence, _pos, 0, size());
86  if (newPos > _bufSize)
87  throw Exception(kSeekError);
88 
89  _pos = newPos;
90  _ptr = _ptr - oldPos + newPos;
91 
92  return oldPos;
93 }
94 
95 
96 MemoryWriteStreamDynamic::MemoryWriteStreamDynamic(bool disposeMemory, size_t capacity) :
97  _data(0, disposeMemory), _ptr(0), _capacity(0), _size(0) {
98 
99  reserve(capacity);
100 }
101 
103 }
104 
106  if (s <= _capacity)
107  return;
108 
109  while (_capacity < s)
110  _capacity = MAX<size_t>(2, _capacity * 2);
111 
112  byte *newData = new byte[_capacity];
113  if (_data)
114  memcpy(newData, _data.get(), _size);
115 
116  const size_t oldPos = pos();
117  _data.dispose();
118  _data.reset(newData);
119  _ptr = _data.get() + oldPos;
120 }
121 
123  if (newLen <= _capacity)
124  return;
125 
126  reserve(newLen);
127 }
128 
129 size_t MemoryWriteStreamDynamic::write(const void *dataPtr, size_t dataSize) {
130  assert(dataPtr);
131 
132  ensureCapacity(pos() + dataSize);
133 
134  std::memcpy(_ptr, dataPtr, dataSize);
135 
136  if (pos() + dataSize > _size)
137  _size = MAX(pos() + dataSize, _size);
138 
139  _ptr += dataSize;
140 
141  return dataSize;
142 }
143 
144 void MemoryWriteStreamDynamic::setDisposable(bool disposeMemory) {
145  _data.setDisposable(disposeMemory);
146 }
147 
149  _data.dispose();
150 
151  _ptr = 0;
152  _size = 0;
153  _capacity = 0;
154 }
155 
157  return _ptr - _data.get();
158 }
159 
161  return _size;
162 }
163 
165  assert((size_t)pos() <= _size);
166 
167  const size_t oldPos = pos();
168  const size_t newPos = evalSeek(offset, whence, pos(), 0, size());
169  if (newPos > _size)
170  throw Exception(kSeekError);
171 
172  _ptr = _data.get() + newPos;
173 
174  return oldPos;
175 }
176 
178  return _data.get();
179 }
180 
181 } // End of namespace Common
Definition: 2dafile.h:39
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.
size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)
Seek offset bytes from the origin whence.
MemoryWriteStreamDynamic(bool disposeMemory=false, size_t capacity=0)
size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)
Seek offset bytes from the origin whence.
DisposableArray< byte > _data
size_t size() const
Return the number of bytes written to this stream in total.
Implementing the writing stream interfaces for memory blocks.
Low-level type definitions to handle fixed width types portably.
StackException Exception
Definition: error.h:59
void setDisposable(bool disposeMemory)
size_t write(const void *dataPtr, size_t dataSize)
Write data into the stream.
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: disposableptr.h:87
size_t size() const
Return the total size of the memory block.
size_t write(const void *dataPtr, size_t dataSize)
Write data into the stream.
void setDisposable(bool d)
Change the disposable flag.
const Exception kSeekError("Seek error")
Exception when seeking a stream failed.
Definition: error.h:63
Origin
The position a seeking offset takes as a base.
Definition: writestream.h:218
PointerType get() const
Returns the plain pointer value.
Definition: disposableptr.h:98
size_t pos() const
Return the current writing position within the stream.
size_t pos() const
Return the current writing position within the memory block.
T MAX(T a, T b)
Definition: util.h:71
uint8 byte
Definition: types.h:209
void dispose()
Unconditionally dispose of the pointer, destroying the old object.