IOStreamBuffer.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #pragma once
  2. /*
  3. Open Asset Import Library (assimp)
  4. ----------------------------------------------------------------------
  5. Copyright (c) 2006-2017, 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 <vector>
  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 file-size.
  58. /// @return The file-size.
  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 getNextDataLine( std::vector<T> &buffer, T continuationToken );
  79. /// @brief Will read the next line ascii or binary end line char.
  80. /// @param buffer The buffer for the next line.
  81. /// @return true if successful.
  82. bool getNextLine(std::vector<T> &buffer);
  83. /// @brief Will read the next block.
  84. /// @param buffer The buffer for the next block.
  85. /// @return true if successful.
  86. bool getNextBlock( std::vector<T> &buffer );
  87. private:
  88. IOStream *m_stream;
  89. size_t m_filesize;
  90. size_t m_cacheSize;
  91. size_t m_numBlocks;
  92. size_t m_blockIdx;
  93. std::vector<T> m_cache;
  94. size_t m_cachePos;
  95. size_t m_filePos;
  96. };
  97. template<class T>
  98. inline
  99. IOStreamBuffer<T>::IOStreamBuffer( size_t cache )
  100. : m_stream( nullptr )
  101. , m_filesize( 0 )
  102. , m_cacheSize( cache )
  103. , m_numBlocks( 0 )
  104. , m_blockIdx( 0 )
  105. , m_cachePos( 0 )
  106. , m_filePos( 0 ) {
  107. m_cache.resize( cache );
  108. std::fill( m_cache.begin(), m_cache.end(), '\n' );
  109. }
  110. template<class T>
  111. inline
  112. IOStreamBuffer<T>::~IOStreamBuffer() {
  113. // empty
  114. }
  115. template<class T>
  116. inline
  117. bool IOStreamBuffer<T>::open( IOStream *stream ) {
  118. // file still opened!
  119. if ( nullptr != m_stream ) {
  120. return false;
  121. }
  122. // Invalid stream pointer
  123. if ( nullptr == stream ) {
  124. return false;
  125. }
  126. m_stream = stream;
  127. m_filesize = m_stream->FileSize();
  128. if ( m_filesize == 0 ) {
  129. return false;
  130. }
  131. if ( m_filesize < m_cacheSize ) {
  132. m_cacheSize = m_filesize;
  133. }
  134. m_numBlocks = m_filesize / m_cacheSize;
  135. if ( ( m_filesize % m_cacheSize ) > 0 ) {
  136. m_numBlocks++;
  137. }
  138. return true;
  139. }
  140. template<class T>
  141. inline
  142. bool IOStreamBuffer<T>::close() {
  143. if ( nullptr == m_stream ) {
  144. return false;
  145. }
  146. // init counters and state vars
  147. m_stream = nullptr;
  148. m_filesize = 0;
  149. m_numBlocks = 0;
  150. m_blockIdx = 0;
  151. m_cachePos = 0;
  152. m_filePos = 0;
  153. return true;
  154. }
  155. template<class T>
  156. inline
  157. size_t IOStreamBuffer<T>::size() const {
  158. return m_filesize;
  159. }
  160. template<class T>
  161. inline
  162. size_t IOStreamBuffer<T>::cacheSize() const {
  163. return m_cacheSize;
  164. }
  165. template<class T>
  166. inline
  167. bool IOStreamBuffer<T>::readNextBlock() {
  168. m_stream->Seek( m_filePos, aiOrigin_SET );
  169. size_t readLen = m_stream->Read( &m_cache[ 0 ], sizeof( T ), m_cacheSize );
  170. if ( readLen == 0 ) {
  171. return false;
  172. }
  173. if ( readLen < m_cacheSize ) {
  174. m_cacheSize = readLen;
  175. }
  176. m_filePos += m_cacheSize;
  177. m_cachePos = 0;
  178. m_blockIdx++;
  179. return true;
  180. }
  181. template<class T>
  182. inline
  183. size_t IOStreamBuffer<T>::getNumBlocks() const {
  184. return m_numBlocks;
  185. }
  186. template<class T>
  187. inline
  188. size_t IOStreamBuffer<T>::getCurrentBlockIndex() const {
  189. return m_blockIdx;
  190. }
  191. template<class T>
  192. inline
  193. size_t IOStreamBuffer<T>::getFilePos() const {
  194. return m_filePos;
  195. }
  196. template<class T>
  197. inline
  198. bool IOStreamBuffer<T>::getNextDataLine( std::vector<T> &buffer, T continuationToken ) {
  199. buffer.resize( m_cacheSize );
  200. if ( m_cachePos == m_cacheSize || 0 == m_filePos ) {
  201. if ( !readNextBlock() ) {
  202. return false;
  203. }
  204. }
  205. bool continuationFound( false ), endOfDataLine( false );
  206. size_t i = 0;
  207. while ( !endOfDataLine ) {
  208. if ( continuationToken == m_cache[ m_cachePos ] ) {
  209. continuationFound = true;
  210. ++m_cachePos;
  211. }
  212. if ( IsLineEnd( m_cache[ m_cachePos ] ) ) {
  213. if ( !continuationFound ) {
  214. // the end of the data line
  215. break;
  216. } else {
  217. // skip line end
  218. while ( m_cache[m_cachePos] != '\n') {
  219. ++m_cachePos;
  220. }
  221. ++m_cachePos;
  222. continuationFound = false;
  223. }
  224. }
  225. buffer[ i ] = m_cache[ m_cachePos ];
  226. m_cachePos++;
  227. i++;
  228. if ( m_cachePos >= m_cacheSize ) {
  229. if ( !readNextBlock() ) {
  230. return false;
  231. }
  232. }
  233. }
  234. buffer[ i ] = '\n';
  235. m_cachePos++;
  236. return true;
  237. }
  238. static
  239. inline
  240. bool isEndOfCache( size_t pos, size_t cacheSize ) {
  241. return ( pos == cacheSize );
  242. }
  243. template<class T>
  244. inline
  245. bool IOStreamBuffer<T>::getNextLine(std::vector<T> &buffer) {
  246. buffer.resize(m_cacheSize);
  247. if ( isEndOfCache( m_cachePos, m_cacheSize ) || 0 == m_filePos) {
  248. if (!readNextBlock()) {
  249. return false;
  250. }
  251. }
  252. if (IsLineEnd(m_cache[m_cachePos])) {
  253. // skip line end
  254. while (m_cache[m_cachePos] != '\n') {
  255. ++m_cachePos;
  256. }
  257. ++m_cachePos;
  258. if ( isEndOfCache( m_cachePos, m_cacheSize ) ) {
  259. if ( !readNextBlock() ) {
  260. return false;
  261. }
  262. }
  263. }
  264. size_t i = 0;
  265. while (!IsLineEnd(m_cache[ m_cachePos ])) {
  266. buffer[i] = m_cache[ m_cachePos ];
  267. m_cachePos++;
  268. i++;
  269. if (m_cachePos >= m_cacheSize) {
  270. if (!readNextBlock()) {
  271. return false;
  272. }
  273. }
  274. }
  275. buffer[i] = '\n';
  276. m_cachePos++;
  277. return true;
  278. }
  279. template<class T>
  280. inline
  281. bool IOStreamBuffer<T>::getNextBlock( std::vector<T> &buffer) {
  282. //just return the last blockvalue if getNextLine was used before
  283. if ( m_cachePos != 0) {
  284. buffer = std::vector<T>(m_cache.begin() + m_cachePos, m_cache.end());
  285. m_cachePos = 0;
  286. }
  287. else {
  288. if ( !readNextBlock() )
  289. return false;
  290. buffer = std::vector<T>(m_cache.begin(), m_cache.end());
  291. }
  292. return true;
  293. }
  294. } // !ns Assimp