IOStreamBuffer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "ParsingUtils.h"
  37. #include <iostream>
  38. namespace Assimp {
  39. // ---------------------------------------------------------------------------
  40. /**
  41. * Implementation of a cached stream buffer.
  42. */
  43. template<class T>
  44. class IOStreamBuffer {
  45. public:
  46. /// @brief The class constructor.
  47. IOStreamBuffer( size_t cache = 4096 * 4096 );
  48. /// @brief The class destructor.
  49. ~IOStreamBuffer();
  50. /// @brief Will open the cached access for a given stream.
  51. /// @param stream The stream to cache.
  52. /// @return true if successful.
  53. bool open( IOStream *stream );
  54. /// @brief Will close the cached access.
  55. /// @return true if successful.
  56. bool close();
  57. /// @brief Returns the filesize.
  58. /// @return The filesize.
  59. size_t size() const;
  60. /// @brief Returns the cache size.
  61. /// @return The cache size.
  62. size_t cacheSize() const;
  63. /// @brief Will read the next block.
  64. /// @return true if successful.
  65. bool readNextBlock();
  66. /// @brief Returns the number of blocks to read.
  67. /// @return The number of blocks.
  68. size_t getNumBlocks() const;
  69. /// @brief Returns the current block index.
  70. /// @return The current block index.
  71. size_t getCurrentBlockIndex() const;
  72. /// @brief Returns the current file pos.
  73. /// @return The current file pos.
  74. size_t getFilePos() const;
  75. /// @brief Will read the next line.
  76. /// @param buffer The buffer for the next line.
  77. /// @return true if successful.
  78. bool getNextLine( std::vector<T> &buffer );
  79. private:
  80. IOStream *m_stream;
  81. size_t m_filesize;
  82. size_t m_cacheSize;
  83. size_t m_numBlocks;
  84. size_t m_blockIdx;
  85. std::vector<T> m_cache;
  86. size_t m_cachePos;
  87. size_t m_filePos;
  88. };
  89. template<class T>
  90. inline
  91. IOStreamBuffer<T>::IOStreamBuffer( size_t cache )
  92. : m_stream( nullptr )
  93. , m_filesize( 0 )
  94. , m_cacheSize( cache )
  95. , m_numBlocks( 0 )
  96. , m_blockIdx( 0 )
  97. , m_cachePos( 0 )
  98. , m_filePos( 0 ) {
  99. m_cache.resize( cache );
  100. std::fill( m_cache.begin(), m_cache.end(), '\n' );
  101. }
  102. template<class T>
  103. inline
  104. IOStreamBuffer<T>::~IOStreamBuffer() {
  105. // empty
  106. }
  107. template<class T>
  108. inline
  109. bool IOStreamBuffer<T>::open( IOStream *stream ) {
  110. // file still opened!
  111. if ( nullptr != m_stream ) {
  112. return false;
  113. }
  114. // Invalid stream pointer
  115. if ( nullptr == stream ) {
  116. return false;
  117. }
  118. m_stream = stream;
  119. m_filesize = m_stream->FileSize();
  120. if ( m_filesize == 0 ) {
  121. return false;
  122. }
  123. if ( m_filesize < m_cacheSize ) {
  124. m_cacheSize = m_filesize;
  125. }
  126. m_numBlocks = m_filesize / m_cacheSize;
  127. if ( ( m_filesize % m_cacheSize ) > 0 ) {
  128. m_numBlocks++;
  129. }
  130. return true;
  131. }
  132. template<class T>
  133. inline
  134. bool IOStreamBuffer<T>::close() {
  135. if ( nullptr == m_stream ) {
  136. return false;
  137. }
  138. // init counters and state vars
  139. m_stream = nullptr;
  140. m_filesize = 0;
  141. m_numBlocks = 0;
  142. m_blockIdx = 0;
  143. m_cachePos = 0;
  144. m_filePos = 0;
  145. return true;
  146. }
  147. template<class T>
  148. inline
  149. size_t IOStreamBuffer<T>::size() const {
  150. return m_filesize;
  151. }
  152. template<class T>
  153. inline
  154. size_t IOStreamBuffer<T>::cacheSize() const {
  155. return m_cacheSize;
  156. }
  157. template<class T>
  158. inline
  159. bool IOStreamBuffer<T>::readNextBlock() {
  160. m_stream->Seek( m_filePos, aiOrigin_SET );
  161. size_t readLen = m_stream->Read( &m_cache[ 0 ], sizeof( T ), m_cacheSize );
  162. if ( readLen == 0 ) {
  163. return false;
  164. }
  165. if ( readLen < m_cacheSize ) {
  166. m_cacheSize = readLen;
  167. }
  168. m_filePos += m_cacheSize;
  169. m_cachePos = 0;
  170. m_blockIdx++;
  171. return true;
  172. }
  173. template<class T>
  174. inline
  175. size_t IOStreamBuffer<T>::getNumBlocks() const {
  176. return m_numBlocks;
  177. }
  178. template<class T>
  179. inline
  180. size_t IOStreamBuffer<T>::getCurrentBlockIndex() const {
  181. return m_blockIdx;
  182. }
  183. template<class T>
  184. inline
  185. size_t IOStreamBuffer<T>::getFilePos() const {
  186. return m_filePos;
  187. }
  188. template<class T>
  189. inline
  190. bool IOStreamBuffer<T>::getNextLine( std::vector<T> &buffer ) {
  191. buffer.resize( m_cacheSize );
  192. if ( m_cachePos == m_cacheSize || 0 == m_filePos ) {
  193. if ( !readNextBlock() ) {
  194. return false;
  195. }
  196. }
  197. size_t i = 0;
  198. while ( !IsLineEnd( m_cache[ m_cachePos ] ) ) {
  199. buffer[ i ] = m_cache[ m_cachePos ];
  200. m_cachePos++;
  201. i++;
  202. if ( m_cachePos >= m_cacheSize ) {
  203. if ( !readNextBlock() ) {
  204. return false;
  205. }
  206. }
  207. }
  208. buffer[ i ] = '\n';
  209. m_cachePos++;
  210. return true;
  211. }
  212. } // !ns Assimp