StreamReader.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. StreamReader(IOStream* _stream)
  59. {
  60. if (!_stream)
  61. throw new ImportErrorException("StreamReader: Unable to open file");
  62. stream = _stream;
  63. size_t s = stream->FileSize();
  64. if (!s)
  65. throw new ImportErrorException("StreamReader: File is empty");
  66. current = buffer = new int8_t[s];
  67. stream->Read(current,s,1);
  68. end = limit = &buffer[s];
  69. }
  70. ~StreamReader()
  71. {
  72. delete[] buffer;
  73. delete stream;
  74. }
  75. /** Read a float from the stream
  76. */
  77. float GetF4()
  78. {
  79. return Get<float>();
  80. }
  81. /** Read a double from the stream
  82. */
  83. double GetF8()
  84. {
  85. return Get<double>();
  86. }
  87. /** Read a short from the stream
  88. */
  89. int16_t GetI2()
  90. {
  91. return Get<int16_t>();
  92. }
  93. /** Read a char from the stream
  94. */
  95. int8_t GetI1()
  96. {
  97. if (current >= end)
  98. throw new ImportErrorException("End of file was reached");
  99. return *current++;
  100. }
  101. /** Read an int from the stream
  102. */
  103. int32_t GetI4()
  104. {
  105. return Get<int32_t>();
  106. }
  107. /** Read a long from the stream
  108. */
  109. int64_t GetI8()
  110. {
  111. return Get<int64_t>();
  112. }
  113. /** Get the remaining stream size (to the end of the srream)
  114. */
  115. unsigned int GetRemainingSize()
  116. {
  117. return (unsigned int)(end - current);
  118. }
  119. /** Get the remaining stream size (to the current read limit)
  120. */
  121. unsigned int GetRemainingSizeToLimit()
  122. {
  123. return (unsigned int)(limit - current);
  124. }
  125. /** Increase the file pointer
  126. */
  127. void IncPtr(unsigned int plus)
  128. {
  129. current += plus;
  130. if (current > end)
  131. {
  132. throw new ImportErrorException("End of file was reached");
  133. }
  134. }
  135. /** Get the current file pointer
  136. */
  137. int8_t* GetPtr() const
  138. {
  139. return current;
  140. }
  141. /** Set current file pointer
  142. */
  143. void SetPtr(int8_t* p)
  144. {
  145. current = p;
  146. if (current > end || current < buffer)
  147. {
  148. throw new ImportErrorException("End of file was reached");
  149. }
  150. }
  151. /** Get the current offset from the beginning of the file
  152. */
  153. int GetCurrentPos() const
  154. {
  155. return (unsigned int)(current - buffer);
  156. }
  157. /** Setup a temporary read limit
  158. *
  159. * @param limit Maximum number of bytes to be read from
  160. * the beginning of the file. Passing 0xffffffff
  161. * resets the limit.
  162. */
  163. void SetReadLimit(unsigned int _limit)
  164. {
  165. if (0xffffffff == _limit)
  166. {
  167. limit = end;
  168. return;
  169. }
  170. limit = buffer + _limit;
  171. if (limit > end)
  172. throw new ImportErrorException("StreamReader: Invalid read limit");
  173. }
  174. /** Get the current read limit
  175. */
  176. int GetReadLimit() const
  177. {
  178. return (unsigned int)(limit - buffer);
  179. }
  180. /** Skip to the read limit
  181. */
  182. void SkipToReadLimit()
  183. {
  184. current = limit;
  185. }
  186. // overload operator>> for those who prefer this way ...
  187. void operator >> (float& f)
  188. {f = GetF4();}
  189. void operator >> (double& f)
  190. {f = GetF8();}
  191. void operator >> (int16_t& f)
  192. {f = GetI2();}
  193. void operator >> (int32_t& f)
  194. {f = GetI4();}
  195. void operator >> (int64_t& f)
  196. {f = GetI8();}
  197. void operator >> (int8_t& f)
  198. {f = GetI1();}
  199. private:
  200. /** Generic read method. ByteSwap::Swap(T*) must exist.
  201. */
  202. template <typename T>
  203. T Get()
  204. {
  205. if (current + sizeof(T) > limit)
  206. throw new ImportErrorException("End of file or stream limit was reached");
  207. T f = *((const T*)current);
  208. if (SwapEndianess)
  209. {
  210. ByteSwap::Swap(&f);
  211. }
  212. current += sizeof(T);
  213. return f;
  214. }
  215. IOStream* stream;
  216. int8_t *buffer, *current, *end, *limit;
  217. };
  218. #ifdef AI_BUILD_BIG_ENDIAN
  219. typedef StreamReader<true> StreamReaderLE;
  220. typedef StreamReader<false> StreamReaderBE;
  221. #else
  222. typedef StreamReader<true> StreamReaderBE;
  223. typedef StreamReader<false> StreamReaderLE;
  224. #endif
  225. } // end namespace Assimp
  226. #endif // !! AI_STREAMREADER_H_INCLUDED