StreamReader.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, 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. #ifndef AI_STREAMREADER_H_INCLUDED
  37. #define AI_STREAMREADER_H_INCLUDED
  38. #include "ByteSwap.h"
  39. namespace Assimp {
  40. // --------------------------------------------------------------------------------------------
  41. /** Wrapper class around IOStream to allow for consistent reading of binary data in both
  42. * little and big endian format. Don't attempt to instance the template directly. Use
  43. * StreamReaderLE to read from a little-endian stream and StreamReaderBE to read from a
  44. * BE stream. The class expects that the endianess of any input data is known at
  45. * compile-time, which should usually be true (#BaseImporter::ConvertToUTF8 implements
  46. * runtime endianess conversions for text files).
  47. *
  48. * XXX switch from unsigned int for size types to size_t? or ptrdiff_t?*/
  49. // --------------------------------------------------------------------------------------------
  50. template <bool SwapEndianess = false>
  51. class StreamReader
  52. {
  53. public:
  54. // ---------------------------------------------------------------------
  55. /** Construction from a given stream with a well-defined endianess
  56. *
  57. * The stream will be deleted afterwards.
  58. * @param stream Input stream
  59. */
  60. StreamReader(IOStream* _stream)
  61. {
  62. if (!_stream) {
  63. throw DeadlyImportError("StreamReader: Unable to open file");
  64. }
  65. stream = _stream;
  66. const size_t s = stream->FileSize();
  67. if (!s) {
  68. throw DeadlyImportError("StreamReader: File is empty");
  69. }
  70. current = buffer = new int8_t[s];
  71. stream->Read(current,s,1);
  72. end = limit = &buffer[s];
  73. }
  74. ~StreamReader()
  75. {
  76. delete[] buffer;
  77. delete stream;
  78. }
  79. public:
  80. // deprecated, use overloaded operator>> instead
  81. // ---------------------------------------------------------------------
  82. /** Read a float from the stream */
  83. float GetF4()
  84. {
  85. return Get<float>();
  86. }
  87. // ---------------------------------------------------------------------
  88. /** Read a double from the stream */
  89. double GetF8() {
  90. return Get<double>();
  91. }
  92. // ---------------------------------------------------------------------
  93. /** Read a signed 16 bit integer from the stream */
  94. int16_t GetI2() {
  95. return Get<int16_t>();
  96. }
  97. // ---------------------------------------------------------------------
  98. /** Read a signed 8 bit integer from the stream */
  99. int8_t GetI1() {
  100. return Get<int8_t>();
  101. }
  102. // ---------------------------------------------------------------------
  103. /** Read an signed 32 bit integer from the stream */
  104. int32_t GetI4() {
  105. return Get<int32_t>();
  106. }
  107. // ---------------------------------------------------------------------
  108. /** Read a signed 64 bit integer from the stream */
  109. int64_t GetI8() {
  110. return Get<int64_t>();
  111. }
  112. // ---------------------------------------------------------------------
  113. /** Read a unsigned 16 bit integer from the stream */
  114. uint16_t GetU2() {
  115. return Get<uint16_t>();
  116. }
  117. // ---------------------------------------------------------------------
  118. /** Read a unsigned 8 bit integer from the stream */
  119. uint8_t GetU1() {
  120. return Get<uint8_t>();
  121. }
  122. // ---------------------------------------------------------------------
  123. /** Read an unsigned 32 bit integer from the stream */
  124. uint32_t GetU4() {
  125. return Get<uint32_t>();
  126. }
  127. // ---------------------------------------------------------------------
  128. /** Read a unsigned 64 bit integer from the stream */
  129. uint64_t GetU8() {
  130. return Get<uint64_t>();
  131. }
  132. public:
  133. // ---------------------------------------------------------------------
  134. /** Get the remaining stream size (to the end of the srream) */
  135. unsigned int GetRemainingSize() const {
  136. return (unsigned int)(end - current);
  137. }
  138. // ---------------------------------------------------------------------
  139. /** Get the remaining stream size (to the current read limit). The
  140. * return value is the remaining size of the stream if no custom
  141. * read limit has been set. */
  142. unsigned int GetRemainingSizeToLimit() const {
  143. return (unsigned int)(limit - current);
  144. }
  145. // ---------------------------------------------------------------------
  146. /** Increase the file pointer (relative seeking) */
  147. void IncPtr(int plus) {
  148. current += plus;
  149. if (current > end) {
  150. throw DeadlyImportError("End of file was reached");
  151. }
  152. }
  153. // ---------------------------------------------------------------------
  154. /** Get the current file pointer */
  155. int8_t* GetPtr() const {
  156. return current;
  157. }
  158. // ---------------------------------------------------------------------
  159. /** Set current file pointer (Get it from #GetPtr). This is if you
  160. * prefer to do pointer arithmetics on your own or want to copy
  161. * large chunks of data at once.
  162. * @param p The new pointer, which is validated against the size
  163. * limit and buffer boundaries. */
  164. void SetPtr(int8_t* p) {
  165. current = p;
  166. if (current > end || current < buffer) {
  167. throw DeadlyImportError("End of file was reached");
  168. }
  169. }
  170. // ---------------------------------------------------------------------
  171. /** Copy n bytes to an external buffer
  172. * @param out Destination for copying
  173. * @param bytes Number of bytes to copy */
  174. void CopyAndAdvance(void* out, size_t bytes) {
  175. int8_t* ur = GetPtr();
  176. SetPtr(ur+bytes); // fire exception if eof
  177. memcpy(out,ur,bytes);
  178. }
  179. // ---------------------------------------------------------------------
  180. /** Get the current offset from the beginning of the file */
  181. int GetCurrentPos() const {
  182. return (unsigned int)(current - buffer);
  183. }
  184. // ---------------------------------------------------------------------
  185. /** Setup a temporary read limit
  186. *
  187. * @param limit Maximum number of bytes to be read from
  188. * the beginning of the file. Passing 0xffffffff
  189. * resets the limit to the original end of the stream. */
  190. void SetReadLimit(unsigned int _limit) {
  191. if (0xffffffff == _limit) {
  192. limit = end;
  193. return;
  194. }
  195. limit = buffer + _limit;
  196. if (limit > end) {
  197. throw DeadlyImportError("StreamReader: Invalid read limit");
  198. }
  199. }
  200. // ---------------------------------------------------------------------
  201. /** Get the current read limit in bytes. Reading over this limit
  202. * accidentially raises an exception. */
  203. int GetReadLimit() const {
  204. return (unsigned int)(limit - buffer);
  205. }
  206. // ---------------------------------------------------------------------
  207. /** Skip to the read limit in bytes. Reading over this limit
  208. * accidentially raises an exception. */
  209. void SkipToReadLimit() {
  210. current = limit;
  211. }
  212. // ---------------------------------------------------------------------
  213. /** overload operator>> and allow chaining of >> ops. */
  214. template <typename T>
  215. StreamReader& operator >> (T& f) {
  216. f = Get<T>();
  217. return *this;
  218. }
  219. private:
  220. template <typename T, bool doit>
  221. struct ByteSwapper {
  222. void operator() (T* inout) {
  223. ByteSwap::Swap(inout);
  224. }
  225. };
  226. template <typename T>
  227. struct ByteSwapper<T,false> {
  228. void operator() (T*) {
  229. }
  230. };
  231. // ---------------------------------------------------------------------
  232. /** Generic read method. ByteSwap::Swap(T*) *must* be defined */
  233. template <typename T>
  234. T Get() {
  235. if (current + sizeof(T) > limit) {
  236. throw DeadlyImportError("End of file or stream limit was reached");
  237. }
  238. T f = *((const T*)current);
  239. ByteSwapper<T,(SwapEndianess && sizeof(T)>1)> swapper;
  240. swapper(&f);
  241. current += sizeof(T);
  242. return f;
  243. }
  244. IOStream* stream;
  245. int8_t *buffer, *current, *end, *limit;
  246. };
  247. // --------------------------------------------------------------------------------------------
  248. #ifdef AI_BUILD_BIG_ENDIAN
  249. typedef StreamReader<true> StreamReaderLE;
  250. typedef StreamReader<false> StreamReaderBE;
  251. #else
  252. typedef StreamReader<true> StreamReaderBE;
  253. typedef StreamReader<false> StreamReaderLE;
  254. #endif
  255. } // end namespace Assimp
  256. #endif // !! AI_STREAMREADER_H_INCLUDED