BUgfixes regarding filepos setupBUgfixes regarding filepos setup.

pull/1043/head
Kim Kulling 2016-10-22 21:03:11 +02:00
parent 33e370a8b7
commit d3a3bd9c12
2 changed files with 111 additions and 82 deletions

View File

@ -114,7 +114,6 @@ private:
mutable size_t mCachedSize; mutable size_t mCachedSize;
}; };
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
inline DefaultIOStream::DefaultIOStream () : inline DefaultIOStream::DefaultIOStream () :
mFile (NULL), mFile (NULL),
@ -124,7 +123,6 @@ inline DefaultIOStream::DefaultIOStream () :
// empty // empty
} }
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
inline DefaultIOStream::DefaultIOStream (FILE* pFile, inline DefaultIOStream::DefaultIOStream (FILE* pFile,
const std::string &strFilename) : const std::string &strFilename) :

View File

@ -41,15 +41,35 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <assimp/types.h> #include <assimp/types.h>
#include <iostream> #include <assimp/IOStream.hpp>
namespace Assimp {
class IOStream; #include <iostream>
namespace Assimp {
template<class T> template<class T>
class IOStreamBuffer { class IOStreamBuffer {
public: public:
IOStreamBuffer( size_t cache = 4096 ) IOStreamBuffer( size_t cache = 4096 * 4096 );
~IOStreamBuffer();
bool open( IOStream *stream );
bool close();
size_t size() const;
bool readNextBlock();
bool getNextLine( std::vector<T> &buffer );
private:
IOStream *m_stream;
size_t m_filesize;
size_t m_cacheSize;
std::vector<T> m_cache;
size_t m_cachePos;
size_t m_filePos;
};
template<class T>
inline
IOStreamBuffer<T>::IOStreamBuffer( size_t cache = 4096 * 4096 )
: m_stream( nullptr ) : m_stream( nullptr )
, m_filesize( 0 ) , m_filesize( 0 )
, m_cacheSize( cache ) , m_cacheSize( cache )
@ -57,12 +77,17 @@ public:
, m_filePos( 0 ) { , m_filePos( 0 ) {
m_cache.resize( cache ); m_cache.resize( cache );
std::fill( m_cache.begin(), m_cache.end(), '\0' ); std::fill( m_cache.begin(), m_cache.end(), '\0' );
} }
~IOStreamBuffer() { template<class T>
} inline
IOStreamBuffer<T>::~IOStreamBuffer() {
// empty
}
bool open( IOStream *stream ) { template<class T>
inline
bool IOStreamBuffer<T>::open( IOStream *stream ) {
if ( nullptr == stream ) { if ( nullptr == stream ) {
return false; return false;
} }
@ -72,39 +97,54 @@ public:
if ( m_filesize == 0 ) { if ( m_filesize == 0 ) {
return false; return false;
} }
if ( m_filesize < m_cacheSize ) {
return true; m_cacheSize = m_filesize;
} }
bool close() { return true;
}
template<class T>
inline
bool IOStreamBuffer<T>::close() {
if ( nullptr == m_stream ) { if ( nullptr == m_stream ) {
return false; return false;
} }
m_stream = nullptr; m_stream = nullptr;
m_filesize =0; m_filesize = 0;
return true; return true;
} }
size_t size() const { template<class T>
inline
size_t IOStreamBuffer<T>::size() const {
return m_filesize; return m_filesize;
} }
bool readNextBlock() { template<class T>
inline
bool IOStreamBuffer<T>::readNextBlock() {
m_stream->Seek( m_filePos, aiOrigin_SET ); m_stream->Seek( m_filePos, aiOrigin_SET );
size_t readLen = m_stream->Read( &m_cache[ 0 ], sizeof( T ), m_cacheSize ); size_t readLen = m_stream->Read( &m_cache[ 0 ], sizeof( T ), m_cacheSize );
if ( readLen == 0 ) { if ( readLen == 0 ) {
return false; return false;
} }
if ( readLen < m_cacheSize ) {
m_cacheSize = readLen;
}
m_filePos += m_cacheSize; m_filePos += m_cacheSize;
m_cachePos = 0; m_cachePos = 0;
return true;
}
bool getNextLine( std::vector<T> &buffer ) { return true;
}
template<class T>
inline
bool IOStreamBuffer<T>::getNextLine( std::vector<T> &buffer ) {
buffer.resize( m_cacheSize ); buffer.resize( m_cacheSize );
::memset( &buffer[ 0 ], ' ', m_cacheSize ); ::memset( &buffer[ 0 ], '\n', m_cacheSize );
if ( m_cachePos == m_cacheSize || 0 == m_filePos ) { if ( m_cachePos == m_cacheSize || 0 == m_filePos ) {
if ( !readNextBlock() ) { if ( !readNextBlock() ) {
@ -122,18 +162,9 @@ public:
} }
} }
} }
buffer[ i ]='\n';
m_cachePos++; m_cachePos++;
return true;
}
private: return true;
IOStream *m_stream; }
size_t m_filesize;
size_t m_cacheSize;
std::vector<T> m_cache;
size_t m_cachePos;
size_t m_filePos;
};
} // !ns Assimp } // !ns Assimp