Library of Assembled Shared Sources
binary_i_stream.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "lass_common.h"
44#include "binary_i_stream.h"
45#include "../num/basic_types.h"
46#include "../num/num_cast.h"
47
48// static_cast from TintPtr to pointer gives warning on MSVC, yet both are identical in size [Bramz]
49//
50#if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC
51# pragma warning(push)
52# pragma warning(disable: 4312) // 'reinterpret_cast' : conversion from 'lass::num::TintPtr' to 'void *' of greater size
53#endif
54
55namespace lass
56{
57
58namespace io
59{
60
61BinaryIStream::BinaryIStream():
63{
64}
65
66
67
68BinaryIStream::~BinaryIStream()
69{
70}
71
72
73
74BinaryIStream::pos_type BinaryIStream::tellg() const
75{
76 return doTellg();
77}
78
79
80
81BinaryIStream& BinaryIStream::seekg(pos_type position)
82{
83 clear(rdstate() & ~std::ios_base::eofbit); // clear eofbit
84 if (good())
85 {
86 doSeekg(position);
87 }
88 return *this;
89}
90
91
92
93BinaryIStream& BinaryIStream::seekg(off_type offset, std::ios_base::seekdir direction)
94{
95 clear(rdstate() & ~std::ios_base::eofbit); // clear eofbit
96 if (good())
97 {
98 doSeekg(offset, direction);
99 }
100 return *this;
101}
102
103
104#if !defined(LASS_HAVE_STDINT_H_INT8_T_IS_CHAR)
105
106BinaryIStream& BinaryIStream::operator>>( char& x )
107{
108 return readValue(x);
109}
110
111#endif
112
113
114BinaryIStream& BinaryIStream::operator>>( num::Tint8& x )
115{
116 return readValue(x);
117}
118
119
120
121BinaryIStream& BinaryIStream::operator>>( num::Tuint8& x )
122{
123 return readValue(x);
124}
125
126
127
128BinaryIStream& BinaryIStream::operator>>( num::Tint16& x )
129{
130 return readValue(x);
131}
132
133
134
135BinaryIStream& BinaryIStream::operator>>( num::Tuint16& x )
136{
137 return readValue(x);
138}
139
140
141
142BinaryIStream& BinaryIStream::operator>>( num::Tint32& x )
143{
144 return readValue(x);
145}
146
147
148
149BinaryIStream& BinaryIStream::operator>>( num::Tuint32& x )
150{
151 return readValue(x);
152}
153
154
155
156BinaryIStream& BinaryIStream::operator>>( num::Tint64& x )
157{
158 return readValue(x);
159}
160
161
162
163BinaryIStream& BinaryIStream::operator>>( num::Tuint64& x )
164{
165 return readValue(x);
166}
167
168
169
170BinaryIStream& BinaryIStream::operator>>( num::Tfloat32& x )
171{
172 return readValue(x);
173}
174
175
176
177BinaryIStream& BinaryIStream::operator>>( num::Tfloat64& x )
178{
179 return readValue(x);
180}
181
182
183
184BinaryIStream& BinaryIStream::operator>>(bool& x)
185{
186 num::Tuint8 temp;
187 *this >> temp;
188 if (good())
189 {
190 x = temp ? true : false;
191 }
192 return *this;
193}
194
195
196
197BinaryIStream& BinaryIStream::operator>>(void*& x)
198{
199 static_assert(sizeof(num::TintPtr) == sizeof(const void*), "TintPtr must have same size as pointers");
200 static_assert(sizeof(num::TintPtr) <= sizeof(num::Tint64), "TintPtr must be no wider than 64-bit");
201 num::Tint64 temp;
202 *this >> temp;
203 if (good())
204 {
205 num::TintPtr address = static_cast<num::TintPtr>(temp);
206 if (temp != static_cast<num::Tint64>(address))
207 {
208 LASS_THROW("address overflow" << std::hex << temp);
209 }
210 x = reinterpret_cast<void*>(address);
211 }
212 return *this;
213}
214
215
216
217BinaryIStream& BinaryIStream::operator>>( std::string& x )
218{
219 typedef std::string::size_type size_type;
220 num::Tuint64 n;
221 *this >> n;
222 if (good())
223 {
224 const size_type length = num::numCast<size_type>(n);
225 std::string buffer(length, '\0');
226 doRead(&buffer[0], length);
227 if (good())
228 {
229 x = std::move(buffer);
230 }
231 }
232 return *this;
233}
234
235
236
237#if LASS_HAVE_WCHAR_SUPPORT
238
239BinaryIStream& BinaryIStream::operator>>( std::wstring& x )
240{
241 std::string utf8;
242 *this >> utf8;
243 if (good())
244 {
245 util::utf8ToWchar( utf8.data(), utf8.length(), x );
246 }
247 return *this;
248}
249
250#endif
251
252
253
254/** read a number of bytes from stream to buffer
255 *
256 * @param iBytes
257 * pointer to buffer. Must be able to contain at least @a numBytes bytes.
258 * @param numBytes
259 * number of bytes to be read
260 */
261size_t BinaryIStream::read(void* output, size_t numBytes)
262{
263 return doRead(output, numBytes);
264}
265
266
267
268// --- private -------------------------------------------------------------------------------------
269
270template <typename T>
271BinaryIStream& BinaryIStream::readValue(T& x)
272{
273 if (good())
274 {
275 T temp;
276 doRead(&temp, sizeof(T));
277 if (good())
278 {
279 x = num::fixEndianness(temp, endianness());
280 }
281 }
282 return *this;
283}
284
285
286}
287
288}
289
base class of binary input streams.
size_t read(void *out, size_t numberOfBytes)
read a number of bytes from stream to buffer
Base class for LASS binary streams with byte-order househoulding.
streams, binary streams, vrmlstreams, ...
Library for Assembled Shared Sources.
Definition config.h:53