StreamReader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. namespace Intern {
  41. // --------------------------------------------------------------------------------------------
  42. template <typename T, bool doit>
  43. struct ByteSwapper {
  44. void operator() (T* inout) {
  45. ByteSwap::Swap(inout);
  46. }
  47. };
  48. template <typename T>
  49. struct ByteSwapper<T,false> {
  50. void operator() (T*) {
  51. }
  52. };
  53. // --------------------------------------------------------------------------------------------
  54. template <bool SwapEndianess, typename T, bool RuntimeSwitch>
  55. struct Getter {
  56. void operator() (T* inout, bool le) {
  57. #ifdef AI_BUILD_BIG_ENDIAN
  58. le = le;
  59. #else
  60. le = !le;
  61. #endif
  62. if (le) {
  63. ByteSwapper<T,(sizeof(T)>1?true:false)> () (inout);
  64. }
  65. else ByteSwapper<T,false> () (inout);
  66. }
  67. };
  68. template <bool SwapEndianess, typename T>
  69. struct Getter<SwapEndianess,T,false> {
  70. void operator() (T* inout, bool le) {
  71. // static branch
  72. ByteSwapper<T,(SwapEndianess && sizeof(T)>1)> () (inout);
  73. }
  74. };
  75. } // end Intern
  76. // --------------------------------------------------------------------------------------------
  77. /** Wrapper class around IOStream to allow for consistent reading of binary data in both
  78. * little and big endian format. Don't attempt to instance the template directly. Use
  79. * StreamReaderLE to read from a little-endian stream and StreamReaderBE to read from a
  80. * BE stream. The class expects that the endianess of any input data is known at
  81. * compile-time, which should usually be true (#BaseImporter::ConvertToUTF8 implements
  82. * runtime endianess conversions for text files).
  83. *
  84. * XXX switch from unsigned int for size types to size_t? or ptrdiff_t?*/
  85. // --------------------------------------------------------------------------------------------
  86. template <bool SwapEndianess = false, bool RuntimeSwitch = false>
  87. class StreamReader
  88. {
  89. public:
  90. // FIXME: use these data types throughout the whole library,
  91. // then change them to 64 bit values :-)
  92. typedef int diff;
  93. typedef unsigned int pos;
  94. public:
  95. // ---------------------------------------------------------------------
  96. /** Construction from a given stream with a well-defined endianess.
  97. *
  98. * The StreamReader holds a permanent strong reference to the
  99. * stream, which is released upon destruction.
  100. * @param stream Input stream. The stream is not restarted if
  101. * its file pointer is not at 0. Instead, the stream reader
  102. * reads from the current position to the end of the stream.
  103. * @param le If @c RuntimeSwitch is true: specifies whether the
  104. * stream is in little endian byte order. Otherwise the
  105. * endianess information is contained in the @c SwapEndianess
  106. * template parameter and this parameter is meaningless. */
  107. StreamReader(boost::shared_ptr<IOStream> stream, bool le = false)
  108. : stream(stream)
  109. , le(le)
  110. {
  111. _Begin();
  112. }
  113. // ---------------------------------------------------------------------
  114. StreamReader(IOStream* stream, bool le = false)
  115. : stream(boost::shared_ptr<IOStream>(stream))
  116. , le(le)
  117. {
  118. _Begin();
  119. }
  120. // ---------------------------------------------------------------------
  121. ~StreamReader() {
  122. delete[] buffer;
  123. }
  124. public:
  125. // deprecated, use overloaded operator>> instead
  126. // ---------------------------------------------------------------------
  127. /** Read a float from the stream */
  128. float GetF4()
  129. {
  130. return Get<float>();
  131. }
  132. // ---------------------------------------------------------------------
  133. /** Read a double from the stream */
  134. double GetF8() {
  135. return Get<double>();
  136. }
  137. // ---------------------------------------------------------------------
  138. /** Read a signed 16 bit integer from the stream */
  139. int16_t GetI2() {
  140. return Get<int16_t>();
  141. }
  142. // ---------------------------------------------------------------------
  143. /** Read a signed 8 bit integer from the stream */
  144. int8_t GetI1() {
  145. return Get<int8_t>();
  146. }
  147. // ---------------------------------------------------------------------
  148. /** Read an signed 32 bit integer from the stream */
  149. int32_t GetI4() {
  150. return Get<int32_t>();
  151. }
  152. // ---------------------------------------------------------------------
  153. /** Read a signed 64 bit integer from the stream */
  154. int64_t GetI8() {
  155. return Get<int64_t>();
  156. }
  157. // ---------------------------------------------------------------------
  158. /** Read a unsigned 16 bit integer from the stream */
  159. uint16_t GetU2() {
  160. return Get<uint16_t>();
  161. }
  162. // ---------------------------------------------------------------------
  163. /** Read a unsigned 8 bit integer from the stream */
  164. uint8_t GetU1() {
  165. return Get<uint8_t>();
  166. }
  167. // ---------------------------------------------------------------------
  168. /** Read an unsigned 32 bit integer from the stream */
  169. uint32_t GetU4() {
  170. return Get<uint32_t>();
  171. }
  172. // ---------------------------------------------------------------------
  173. /** Read a unsigned 64 bit integer from the stream */
  174. uint64_t GetU8() {
  175. return Get<uint64_t>();
  176. }
  177. 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. }
  183. // ---------------------------------------------------------------------
  184. /** Get the remaining stream size (to the current read limit). The
  185. * return value is the remaining size of the stream if no custom
  186. * read limit has been set. */
  187. unsigned int GetRemainingSizeToLimit() const {
  188. return (unsigned int)(limit - current);
  189. }
  190. // ---------------------------------------------------------------------
  191. /** Increase the file pointer (relative seeking) */
  192. void IncPtr(int plus) {
  193. current += plus;
  194. if (current > limit) {
  195. throw DeadlyImportError("End of file or read limit was reached");
  196. }
  197. }
  198. // ---------------------------------------------------------------------
  199. /** Get the current file pointer */
  200. int8_t* GetPtr() const {
  201. return current;
  202. }
  203. // ---------------------------------------------------------------------
  204. /** Set current file pointer (Get it from #GetPtr). This is if you
  205. * prefer to do pointer arithmetics on your own or want to copy
  206. * large chunks of data at once.
  207. * @param p The new pointer, which is validated against the size
  208. * limit and buffer boundaries. */
  209. void SetPtr(int8_t* p) {
  210. current = p;
  211. if (current > limit || current < buffer) {
  212. throw DeadlyImportError("End of file or read limit was reached");
  213. }
  214. }
  215. // ---------------------------------------------------------------------
  216. /** Copy n bytes to an external buffer
  217. * @param out Destination for copying
  218. * @param bytes Number of bytes to copy */
  219. void CopyAndAdvance(void* out, size_t bytes) {
  220. int8_t* ur = GetPtr();
  221. SetPtr(ur+bytes); // fire exception if eof
  222. memcpy(out,ur,bytes);
  223. }
  224. // ---------------------------------------------------------------------
  225. /** Get the current offset from the beginning of the file */
  226. int GetCurrentPos() const {
  227. return (unsigned int)(current - buffer);
  228. }
  229. void SetCurrentPos(size_t pos) {
  230. SetPtr(buffer + pos);
  231. }
  232. // ---------------------------------------------------------------------
  233. /** Setup a temporary read limit
  234. *
  235. * @param limit Maximum number of bytes to be read from
  236. * the beginning of the file. Passing 0xffffffff
  237. * resets the limit to the original end of the stream. */
  238. void SetReadLimit(unsigned int _limit) {
  239. if (0xffffffff == _limit) {
  240. limit = end;
  241. return;
  242. }
  243. limit = buffer + _limit;
  244. if (limit > end) {
  245. throw DeadlyImportError("StreamReader: Invalid read limit");
  246. }
  247. }
  248. // ---------------------------------------------------------------------
  249. /** Get the current read limit in bytes. Reading over this limit
  250. * accidentially raises an exception. */
  251. int GetReadLimit() const {
  252. return (unsigned int)(limit - buffer);
  253. }
  254. // ---------------------------------------------------------------------
  255. /** Skip to the read limit in bytes. Reading over this limit
  256. * accidentially raises an exception. */
  257. void SkipToReadLimit() {
  258. current = limit;
  259. }
  260. // ---------------------------------------------------------------------
  261. /** overload operator>> and allow chaining of >> ops. */
  262. template <typename T>
  263. StreamReader& operator >> (T& f) {
  264. f = Get<T>();
  265. return *this;
  266. }
  267. private:
  268. // ---------------------------------------------------------------------
  269. /** Generic read method. ByteSwap::Swap(T*) *must* be defined */
  270. template <typename T>
  271. T Get() {
  272. if (current + sizeof(T) > limit) {
  273. throw DeadlyImportError("End of file or stream limit was reached");
  274. }
  275. T f = *((const T*)current);
  276. Intern :: Getter<SwapEndianess,T,RuntimeSwitch>() (&f,le);
  277. current += sizeof(T);
  278. return f;
  279. }
  280. // ---------------------------------------------------------------------
  281. void _Begin() {
  282. if (!stream) {
  283. throw DeadlyImportError("StreamReader: Unable to open file");
  284. }
  285. const size_t s = stream->FileSize() - stream->Tell();
  286. if (!s) {
  287. throw DeadlyImportError("StreamReader: File is empty or EOF is already reached");
  288. }
  289. current = buffer = new int8_t[s];
  290. stream->Read(current,s,1);
  291. end = limit = &buffer[s];
  292. }
  293. private:
  294. boost::shared_ptr<IOStream> stream;
  295. int8_t *buffer, *current, *end, *limit;
  296. bool le;
  297. };
  298. // --------------------------------------------------------------------------------------------
  299. // `static` StreamReaders. Their byte order is fixed and they might be a little bit faster.
  300. #ifdef AI_BUILD_BIG_ENDIAN
  301. typedef StreamReader<true> StreamReaderLE;
  302. typedef StreamReader<false> StreamReaderBE;
  303. #else
  304. typedef StreamReader<true> StreamReaderBE;
  305. typedef StreamReader<false> StreamReaderLE;
  306. #endif
  307. // `dynamic` StreamReader. The byte order of the input data is specified in the
  308. // c'tor. This involves runtime branching and might be a little bit slower.
  309. typedef StreamReader<true,true> StreamReaderAny;
  310. } // end namespace Assimp
  311. #endif // !! AI_STREAMREADER_H_INCLUDED