StreamReader.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * @param stream Input stream
  56. */
  57. inline StreamReader(IOStream* stream)
  58. {
  59. ai_assert(NULL != stream);
  60. this->stream = stream;
  61. size_t s = stream->FileSize();
  62. if (!s)throw new ImportErrorException("File is empty");
  63. current = buffer = new int8_t[s];
  64. end = &buffer[s];
  65. }
  66. inline ~StreamReader() {delete[] buffer;}
  67. /** Read a float from the stream
  68. */
  69. inline float GetF4()
  70. {
  71. return Get<float>();
  72. }
  73. /** Read a double from the stream
  74. */
  75. inline double GetF8()
  76. {
  77. return Get<double>();
  78. }
  79. /** Read a short from the stream
  80. */
  81. inline int16_t GetI2()
  82. {
  83. return Get<int16_t>();
  84. }
  85. /** Read a char from the stream
  86. */
  87. inline int8_t GetI1()
  88. {
  89. if (current >= end)
  90. throw new ImportErrorException("End of file was reached");
  91. return *current++;
  92. }
  93. /** Read an int from the stream
  94. */
  95. inline int32_t GetI4()
  96. {
  97. return Get<int32_t>();
  98. }
  99. /** Read a long from the stream
  100. */
  101. inline int64_t GetI8()
  102. {
  103. return Get<int64_t>();
  104. }
  105. /** Get the remaining stream size
  106. */
  107. inline unsigned int GetRemainingSize()
  108. {
  109. return (unsigned int)(end - current);
  110. }
  111. // overload operator>> for those who prefer this way ...
  112. inline void operator >> (float& f)
  113. {f = GetF4();}
  114. inline void operator >> (double& f)
  115. {f = GetF8();}
  116. inline void operator >> (int16_t& f)
  117. {f = GetI2();}
  118. inline void operator >> (int32_t& f)
  119. {f = GetI4();}
  120. inline void operator >> (int64_t& f)
  121. {f = GetI8();}
  122. inline void operator >> (int8_t& f)
  123. {f = GetI1();}
  124. private:
  125. /** Generic read method. ByteSwap::Swap(T*) must exist.
  126. */
  127. template <typename T>
  128. inline T Get()
  129. {
  130. if (current + sizeof(T) > end)
  131. throw new ImportErrorException("End of file was reached");
  132. T f = *((const T*)current);
  133. if (SwapEndianess)
  134. {
  135. ByteSwap::Swap(&f);
  136. }
  137. current += sizeof(T);
  138. return f;
  139. }
  140. IOStream* stream;
  141. int8_t *buffer, *current, *end;
  142. };
  143. #ifdef AI_BUILD_BIG_ENDIAN
  144. typedef StreamReader<true> StreamReaderLE;
  145. typedef StreamReader<false> StreamReaderBE;
  146. #else
  147. typedef StreamReader<true> StreamReaderBE;
  148. typedef StreamReader<false> StreamReaderLE;
  149. #endif
  150. };
  151. #endif // !! AI_STREAMREADER_H_INCLUDED