IOStreamBuffer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #pragma once
  2. /*
  3. Open Asset Import Library (assimp)
  4. ----------------------------------------------------------------------
  5. Copyright (c) 2006-2016, assimp 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
  9. following 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 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. #include <assimp/types.h>
  35. #include <assimp/IOStream.hpp>
  36. #include <iostream>
  37. namespace Assimp {
  38. // ---------------------------------------------------------------------------
  39. /**
  40. * Implementation of a cached stream buffer.
  41. */
  42. template<class T>
  43. class IOStreamBuffer {
  44. public:
  45. /// @brief The class constructor.
  46. IOStreamBuffer( size_t cache = 4096 * 4096 );
  47. /// @brief The class destructor.
  48. ~IOStreamBuffer();
  49. /// @brief Will open the cached access for a given stream.
  50. /// @param stream The stream to cache.
  51. /// @return true if successful.
  52. bool open( IOStream *stream );
  53. /// @brief Will close the cached access.
  54. /// @return true if successful.
  55. bool close();
  56. /// @brief Returns the filesize.
  57. /// @return The filesize.
  58. size_t size() const;
  59. /// @brief Returns the cache size.
  60. /// @return The cache size.
  61. size_t cacheSize() const;
  62. /// @brief Will read the next block.
  63. /// @return true if successful.
  64. bool readNextBlock();
  65. /// @brief Will read the next line.
  66. /// @param buffer The buffer for the next line.
  67. /// @return true if successful.
  68. bool getNextLine( std::vector<T> &buffer );
  69. private:
  70. IOStream *m_stream;
  71. size_t m_filesize;
  72. size_t m_cacheSize;
  73. std::vector<T> m_cache;
  74. size_t m_cachePos;
  75. size_t m_filePos;
  76. };
  77. template<class T>
  78. inline
  79. IOStreamBuffer<T>::IOStreamBuffer( size_t cache )
  80. : m_stream( nullptr )
  81. , m_filesize( 0 )
  82. , m_cacheSize( cache )
  83. , m_cachePos( 0 )
  84. , m_filePos( 0 ) {
  85. m_cache.resize( cache );
  86. std::fill( m_cache.begin(), m_cache.end(), '\n' );
  87. }
  88. template<class T>
  89. inline
  90. IOStreamBuffer<T>::~IOStreamBuffer() {
  91. // empty
  92. }
  93. template<class T>
  94. inline
  95. bool IOStreamBuffer<T>::open( IOStream *stream ) {
  96. if ( nullptr != m_stream ) {
  97. return false;
  98. }
  99. if ( nullptr == stream ) {
  100. return false;
  101. }
  102. m_stream = stream;
  103. m_filesize = m_stream->FileSize();
  104. if ( m_filesize == 0 ) {
  105. return false;
  106. }
  107. if ( m_filesize < m_cacheSize ) {
  108. m_cacheSize = m_filesize;
  109. }
  110. return true;
  111. }
  112. template<class T>
  113. inline
  114. bool IOStreamBuffer<T>::close() {
  115. if ( nullptr == m_stream ) {
  116. return false;
  117. }
  118. m_stream = nullptr;
  119. m_filesize = 0;
  120. return true;
  121. }
  122. template<class T>
  123. inline
  124. size_t IOStreamBuffer<T>::size() const {
  125. return m_filesize;
  126. }
  127. template<class T>
  128. inline
  129. size_t IOStreamBuffer<T>::cacheSize() const {
  130. return m_cacheSize;
  131. }
  132. template<class T>
  133. inline
  134. bool IOStreamBuffer<T>::readNextBlock() {
  135. m_stream->Seek( m_filePos, aiOrigin_SET );
  136. size_t readLen = m_stream->Read( &m_cache[ 0 ], sizeof( T ), m_cacheSize );
  137. if ( readLen == 0 ) {
  138. return false;
  139. }
  140. if ( readLen < m_cacheSize ) {
  141. m_cacheSize = readLen;
  142. }
  143. m_filePos += m_cacheSize;
  144. m_cachePos = 0;
  145. return true;
  146. }
  147. template<class T>
  148. inline
  149. bool IOStreamBuffer<T>::getNextLine( std::vector<T> &buffer ) {
  150. buffer.resize( m_cacheSize );
  151. ::memset( &buffer[ 0 ], '\n', m_cacheSize );
  152. if ( m_cachePos == m_cacheSize || 0 == m_filePos ) {
  153. if ( !readNextBlock() ) {
  154. return false;
  155. }
  156. }
  157. size_t i = 0;
  158. while ( !IsLineEnd( m_cache[ m_cachePos ] ) ) {
  159. buffer[ i ] = m_cache[ m_cachePos ];
  160. m_cachePos++;
  161. i++;
  162. if ( m_cachePos >= m_cacheSize ) {
  163. if ( !readNextBlock() ) {
  164. return false;
  165. }
  166. }
  167. }
  168. m_cachePos++;
  169. return true;
  170. }
  171. } // !ns Assimp