vrshoot

view libs/assimp/StreamReader.h @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line source
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
6 Copyright (c) 2006-2012, assimp team
8 All rights reserved.
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the
20 following disclaimer in the documentation and/or other
21 materials provided with the distribution.
23 * Neither the name of the assimp team, nor the names of its
24 contributors may be used to endorse or promote products
25 derived from this software without specific prior
26 written permission of the assimp team.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
42 /** @file Defines the StreamReader class which reads data from
43 * a binary stream with a well-defined endianess. */
45 #ifndef AI_STREAMREADER_H_INCLUDED
46 #define AI_STREAMREADER_H_INCLUDED
48 #include "ByteSwap.h"
50 namespace Assimp {
52 // --------------------------------------------------------------------------------------------
53 /** Wrapper class around IOStream to allow for consistent reading of binary data in both
54 * little and big endian format. Don't attempt to instance the template directly. Use
55 * StreamReaderLE to read from a little-endian stream and StreamReaderBE to read from a
56 * BE stream. The class expects that the endianess of any input data is known at
57 * compile-time, which should usually be true (#BaseImporter::ConvertToUTF8 implements
58 * runtime endianess conversions for text files).
59 *
60 * XXX switch from unsigned int for size types to size_t? or ptrdiff_t?*/
61 // --------------------------------------------------------------------------------------------
62 template <bool SwapEndianess = false, bool RuntimeSwitch = false>
63 class StreamReader
64 {
66 public:
68 // FIXME: use these data types throughout the whole library,
69 // then change them to 64 bit values :-)
71 typedef int diff;
72 typedef unsigned int pos;
74 public:
77 // ---------------------------------------------------------------------
78 /** Construction from a given stream with a well-defined endianess.
79 *
80 * The StreamReader holds a permanent strong reference to the
81 * stream, which is released upon destruction.
82 * @param stream Input stream. The stream is not restarted if
83 * its file pointer is not at 0. Instead, the stream reader
84 * reads from the current position to the end of the stream.
85 * @param le If @c RuntimeSwitch is true: specifies whether the
86 * stream is in little endian byte order. Otherwise the
87 * endianess information is contained in the @c SwapEndianess
88 * template parameter and this parameter is meaningless. */
89 StreamReader(boost::shared_ptr<IOStream> stream, bool le = false)
90 : stream(stream)
91 , le(le)
92 {
93 ai_assert(stream);
94 InternBegin();
95 }
97 // ---------------------------------------------------------------------
98 StreamReader(IOStream* stream, bool le = false)
99 : stream(boost::shared_ptr<IOStream>(stream))
100 , le(le)
101 {
102 ai_assert(stream);
103 InternBegin();
104 }
106 // ---------------------------------------------------------------------
107 ~StreamReader() {
108 delete[] buffer;
109 }
111 public:
113 // deprecated, use overloaded operator>> instead
115 // ---------------------------------------------------------------------
116 /** Read a float from the stream */
117 float GetF4()
118 {
119 return Get<float>();
120 }
122 // ---------------------------------------------------------------------
123 /** Read a double from the stream */
124 double GetF8() {
125 return Get<double>();
126 }
128 // ---------------------------------------------------------------------
129 /** Read a signed 16 bit integer from the stream */
130 int16_t GetI2() {
131 return Get<int16_t>();
132 }
134 // ---------------------------------------------------------------------
135 /** Read a signed 8 bit integer from the stream */
136 int8_t GetI1() {
137 return Get<int8_t>();
138 }
140 // ---------------------------------------------------------------------
141 /** Read an signed 32 bit integer from the stream */
142 int32_t GetI4() {
143 return Get<int32_t>();
144 }
146 // ---------------------------------------------------------------------
147 /** Read a signed 64 bit integer from the stream */
148 int64_t GetI8() {
149 return Get<int64_t>();
150 }
152 // ---------------------------------------------------------------------
153 /** Read a unsigned 16 bit integer from the stream */
154 uint16_t GetU2() {
155 return Get<uint16_t>();
156 }
158 // ---------------------------------------------------------------------
159 /** Read a unsigned 8 bit integer from the stream */
160 uint8_t GetU1() {
161 return Get<uint8_t>();
162 }
164 // ---------------------------------------------------------------------
165 /** Read an unsigned 32 bit integer from the stream */
166 uint32_t GetU4() {
167 return Get<uint32_t>();
168 }
170 // ---------------------------------------------------------------------
171 /** Read a unsigned 64 bit integer from the stream */
172 uint64_t GetU8() {
173 return Get<uint64_t>();
174 }
176 public:
178 // ---------------------------------------------------------------------
179 /** Get the remaining stream size (to the end of the srream) */
180 unsigned int GetRemainingSize() const {
181 return (unsigned int)(end - current);
182 }
185 // ---------------------------------------------------------------------
186 /** Get the remaining stream size (to the current read limit). The
187 * return value is the remaining size of the stream if no custom
188 * read limit has been set. */
189 unsigned int GetRemainingSizeToLimit() const {
190 return (unsigned int)(limit - current);
191 }
194 // ---------------------------------------------------------------------
195 /** Increase the file pointer (relative seeking) */
196 void IncPtr(int plus) {
197 current += plus;
198 if (current > limit) {
199 throw DeadlyImportError("End of file or read limit was reached");
200 }
201 }
203 // ---------------------------------------------------------------------
204 /** Get the current file pointer */
205 int8_t* GetPtr() const {
206 return current;
207 }
210 // ---------------------------------------------------------------------
211 /** Set current file pointer (Get it from #GetPtr). This is if you
212 * prefer to do pointer arithmetics on your own or want to copy
213 * large chunks of data at once.
214 * @param p The new pointer, which is validated against the size
215 * limit and buffer boundaries. */
216 void SetPtr(int8_t* p) {
218 current = p;
219 if (current > limit || current < buffer) {
220 throw DeadlyImportError("End of file or read limit was reached");
221 }
222 }
224 // ---------------------------------------------------------------------
225 /** Copy n bytes to an external buffer
226 * @param out Destination for copying
227 * @param bytes Number of bytes to copy */
228 void CopyAndAdvance(void* out, size_t bytes) {
230 int8_t* ur = GetPtr();
231 SetPtr(ur+bytes); // fire exception if eof
233 memcpy(out,ur,bytes);
234 }
237 // ---------------------------------------------------------------------
238 /** Get the current offset from the beginning of the file */
239 int GetCurrentPos() const {
240 return (unsigned int)(current - buffer);
241 }
243 void SetCurrentPos(size_t pos) {
244 SetPtr(buffer + pos);
245 }
247 // ---------------------------------------------------------------------
248 /** Setup a temporary read limit
249 *
250 * @param limit Maximum number of bytes to be read from
251 * the beginning of the file. Specifying UINT_MAX
252 * resets the limit to the original end of the stream. */
253 void SetReadLimit(unsigned int _limit) {
255 if (UINT_MAX == _limit) {
256 limit = end;
257 return;
258 }
260 limit = buffer + _limit;
261 if (limit > end) {
262 throw DeadlyImportError("StreamReader: Invalid read limit");
263 }
264 }
266 // ---------------------------------------------------------------------
267 /** Get the current read limit in bytes. Reading over this limit
268 * accidentially raises an exception. */
269 int GetReadLimit() const {
270 return (unsigned int)(limit - buffer);
271 }
273 // ---------------------------------------------------------------------
274 /** Skip to the read limit in bytes. Reading over this limit
275 * accidentially raises an exception. */
276 void SkipToReadLimit() {
277 current = limit;
278 }
280 // ---------------------------------------------------------------------
281 /** overload operator>> and allow chaining of >> ops. */
282 template <typename T>
283 StreamReader& operator >> (T& f) {
284 f = Get<T>();
285 return *this;
286 }
288 private:
290 // ---------------------------------------------------------------------
291 /** Generic read method. ByteSwap::Swap(T*) *must* be defined */
292 template <typename T>
293 T Get() {
294 if (current + sizeof(T) > limit) {
295 throw DeadlyImportError("End of file or stream limit was reached");
296 }
298 #ifdef __arm__
299 T f;
300 memcpy (&f, current, sizeof(T));
301 #else
302 T f = *((const T*)current);
303 #endif
304 Intern :: Getter<SwapEndianess,T,RuntimeSwitch>() (&f,le);
306 current += sizeof(T);
307 return f;
308 }
310 // ---------------------------------------------------------------------
311 void InternBegin() {
312 if (!stream) {
313 // incase someone wonders: StreamReader is frequently invoked with
314 // no prior validation whether the input stream is valid. Since
315 // no one bothers changing the error message, this message here
316 // is passed down to the caller and 'unable to open file'
317 // simply describes best what happened.
318 throw DeadlyImportError("StreamReader: Unable to open file");
319 }
321 const size_t s = stream->FileSize() - stream->Tell();
322 if (!s) {
323 throw DeadlyImportError("StreamReader: File is empty or EOF is already reached");
324 }
326 current = buffer = new int8_t[s];
327 const size_t read = stream->Read(current,1,s);
328 // (read < s) can only happen if the stream was opened in text mode, in which case FileSize() is not reliable
329 ai_assert(read <= s);
330 end = limit = &buffer[read];
331 }
333 private:
336 boost::shared_ptr<IOStream> stream;
337 int8_t *buffer, *current, *end, *limit;
338 bool le;
339 };
342 // --------------------------------------------------------------------------------------------
343 // `static` StreamReaders. Their byte order is fixed and they might be a little bit faster.
344 #ifdef AI_BUILD_BIG_ENDIAN
345 typedef StreamReader<true> StreamReaderLE;
346 typedef StreamReader<false> StreamReaderBE;
347 #else
348 typedef StreamReader<true> StreamReaderBE;
349 typedef StreamReader<false> StreamReaderLE;
350 #endif
352 // `dynamic` StreamReader. The byte order of the input data is specified in the
353 // c'tor. This involves runtime branching and might be a little bit slower.
354 typedef StreamReader<true,true> StreamReaderAny;
356 } // end namespace Assimp
358 #endif // !! AI_STREAMREADER_H_INCLUDED