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;
};
// ----------------------------------------------------------------------------------
inline DefaultIOStream::DefaultIOStream () :
mFile (NULL),
@ -124,7 +123,6 @@ inline DefaultIOStream::DefaultIOStream () :
// empty
}
// ----------------------------------------------------------------------------------
inline DefaultIOStream::DefaultIOStream (FILE* pFile,
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 <iostream>
namespace Assimp {
#include <assimp/IOStream.hpp>
class IOStream;
#include <iostream>
namespace Assimp {
template<class T>
class IOStreamBuffer {
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_filesize( 0 )
, m_cacheSize( cache )
@ -59,10 +79,15 @@ public:
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 ) {
return false;
}
@ -72,11 +97,16 @@ public:
if ( m_filesize == 0 ) {
return false;
}
if ( m_filesize < m_cacheSize ) {
m_cacheSize = m_filesize;
}
return true;
}
bool close() {
template<class T>
inline
bool IOStreamBuffer<T>::close() {
if ( nullptr == m_stream ) {
return false;
}
@ -87,24 +117,34 @@ public:
return true;
}
size_t size() const {
template<class T>
inline
size_t IOStreamBuffer<T>::size() const {
return m_filesize;
}
bool readNextBlock() {
template<class T>
inline
bool IOStreamBuffer<T>::readNextBlock() {
m_stream->Seek( m_filePos, aiOrigin_SET );
size_t readLen = m_stream->Read( &m_cache[ 0 ], sizeof( T ), m_cacheSize );
if ( readLen == 0 ) {
return false;
}
if ( readLen < m_cacheSize ) {
m_cacheSize = readLen;
}
m_filePos += m_cacheSize;
m_cachePos = 0;
return true;
}
bool getNextLine( std::vector<T> &buffer ) {
template<class T>
inline
bool IOStreamBuffer<T>::getNextLine( std::vector<T> &buffer ) {
buffer.resize( m_cacheSize );
::memset( &buffer[ 0 ], ' ', m_cacheSize );
::memset( &buffer[ 0 ], '\n', m_cacheSize );
if ( m_cachePos == m_cacheSize || 0 == m_filePos ) {
if ( !readNextBlock() ) {
@ -122,18 +162,9 @@ public:
}
}
}
buffer[ i ]='\n';
m_cachePos++;
return true;
}
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;
};
} // !ns Assimp