StreamReader.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Defines the StreamReader class which reads data from
  35. * a binary stream with a well-defined endianess.
  36. */
  37. #ifndef AI_STREAMREADER_H_INCLUDED
  38. #define AI_STREAMREADER_H_INCLUDED
  39. #include "ByteSwap.h"
  40. namespace Assimp
  41. {
  42. /** Wrapper class around IOStream to allow for consistent reading
  43. * of binary data in both little and big endian format.
  44. * Don't use this type directly. Use StreamReaderLE to read
  45. * from a little-endian stream and StreamReaderBE to read from a
  46. * BE stream. This class expects that the endianess of the data
  47. * is known at compile-time.
  48. */
  49. template <bool SwapEndianess = false>
  50. class StreamReader
  51. {
  52. public:
  53. /** Construction from a given stream with a well-defined endianess
  54. *
  55. * The stream will be deleted afterwards.
  56. * @param stream Input stream
  57. */
  58. inline StreamReader(IOStream* stream)
  59. {
  60. ai_assert(NULL != stream);
  61. this->stream = stream;
  62. size_t s = stream->FileSize();
  63. if (!s)throw new ImportErrorException("File is empty");
  64. current = buffer = new int8_t[s];
  65. stream->Read(current,s,1);
  66. end = limit = &buffer[s];
  67. }
  68. inline ~StreamReader()
  69. {
  70. delete[] buffer;
  71. delete stream;
  72. }
  73. /** Read a float from the stream
  74. */
  75. inline float GetF4()
  76. {
  77. return Get<float>();
  78. }
  79. /** Read a double from the stream
  80. */
  81. inline double GetF8()
  82. {
  83. return Get<double>();
  84. }
  85. /** Read a short from the stream
  86. */
  87. inline int16_t GetI2()
  88. {
  89. return Get<int16_t>();
  90. }
  91. /** Read a char from the stream
  92. */
  93. inline int8_t GetI1()
  94. {
  95. if (current >= end)
  96. throw new ImportErrorException("End of file was reached");
  97. return *current++;
  98. }
  99. /** Read an int from the stream
  100. */
  101. inline int32_t GetI4()
  102. {
  103. return Get<int32_t>();
  104. }
  105. /** Read a long from the stream
  106. */
  107. inline int64_t GetI8()
  108. {
  109. return Get<int64_t>();
  110. }
  111. /** Get the remaining stream size (to the end of the srream)
  112. */
  113. inline unsigned int GetRemainingSize()
  114. {
  115. return (unsigned int)(end - current);
  116. }
  117. /** Get the remaining stream size (to the current read limit)
  118. */
  119. inline unsigned int GetRemainingSizeToLimit()
  120. {
  121. return (unsigned int)(limit - current);
  122. }
  123. /** Increase the file pointer
  124. */
  125. inline void IncPtr(unsigned int plus)
  126. {
  127. current += plus;
  128. if (current > end)
  129. {
  130. throw new ImportErrorException("End of file was reached");
  131. }
  132. }
  133. /** Get the current file pointer
  134. */
  135. inline int8_t* GetPtr() const
  136. {
  137. return current;
  138. }
  139. /** Set current file pointer
  140. */
  141. inline void SetPtr(int8_t* p)
  142. {
  143. current = p;
  144. if (current > end || current < buffer)
  145. {
  146. throw new ImportErrorException("End of file was reached");
  147. }
  148. }
  149. /** Get the current offset from the beginning of the file
  150. */
  151. inline int GetCurrentPos() const
  152. {
  153. return (unsigned int)(current - buffer);
  154. }
  155. /** Setup a temporary read limit
  156. *
  157. * @param limit Maximum number of bytes to be read from
  158. * the beginning of the file. Passing 0xffffffff
  159. * resets the limit.
  160. */
  161. inline void SetReadLimit(unsigned int _limit)
  162. {
  163. if (0xffffffff == _limit)
  164. {
  165. limit = end;
  166. return;
  167. }
  168. limit = buffer + _limit;
  169. if (limit > end)
  170. throw new ImportErrorException("StreamReader: Invalid read limit");
  171. }
  172. /** Get the current read limit
  173. */
  174. inline int GetReadLimit() const
  175. {
  176. return (unsigned int)(limit - buffer);
  177. }
  178. /** Skip to the read limit
  179. */
  180. inline void SkipToReadLimit()
  181. {
  182. current = limit;
  183. }
  184. // overload operator>> for those who prefer this way ...
  185. inline void operator >> (float& f)
  186. {f = GetF4();}
  187. inline void operator >> (double& f)
  188. {f = GetF8();}
  189. inline void operator >> (int16_t& f)
  190. {f = GetI2();}
  191. inline void operator >> (int32_t& f)
  192. {f = GetI4();}
  193. inline void operator >> (int64_t& f)
  194. {f = GetI8();}
  195. inline void operator >> (int8_t& f)
  196. {f = GetI1();}
  197. private:
  198. /** Generic read method. ByteSwap::Swap(T*) must exist.
  199. */
  200. template <typename T>
  201. inline T Get()
  202. {
  203. if (current + sizeof(T) > limit)
  204. throw new ImportErrorException("End of file or stream limit was reached");
  205. T f = *((const T*)current);
  206. if (SwapEndianess)
  207. {
  208. ByteSwap::Swap(&f);
  209. }
  210. current += sizeof(T);
  211. return f;
  212. }
  213. IOStream* stream;
  214. int8_t *buffer, *current, *end, *limit;
  215. };
  216. #ifdef AI_BUILD_BIG_ENDIAN
  217. typedef StreamReader<true> StreamReaderLE;
  218. typedef StreamReader<false> StreamReaderBE;
  219. #else
  220. typedef StreamReader<true> StreamReaderBE;
  221. typedef StreamReader<false> StreamReaderLE;
  222. #endif
  223. } // end namespace Assimp
  224. #endif // !! AI_STREAMREADER_H_INCLUDED