diff --git a/code/IOStreamBuffer.h b/code/IOStreamBuffer.h index 5b00ef5d6..e82207340 100644 --- a/code/IOStreamBuffer.h +++ b/code/IOStreamBuffer.h @@ -47,15 +47,43 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace Assimp { +// --------------------------------------------------------------------------- +/** + * Implementation of a cached stream buffer. + */ template class IOStreamBuffer { public: + /// @brief The class constructor. IOStreamBuffer( size_t cache = 4096 * 4096 ); + + /// @brief The class destructor. ~IOStreamBuffer(); + + /// @brief Will open the cached access for a given stream. + /// @param stream The stream to cache. + /// @return true if successful. bool open( IOStream *stream ); + + /// @brief Will close the cached access. + /// @return true if successful. bool close(); + + /// @brief Returns the filesize. + /// @return The filesize. size_t size() const; + + /// @brief Returns the cache size. + /// @return The cache size. + size_t cacheSize() const; + + /// @brief Will read the next block. + /// @return true if successful. bool readNextBlock(); + + /// @brief Will read the next line. + /// @param buffer The buffer for the next line. + /// @return true if successful. bool getNextLine( std::vector &buffer ); private: @@ -69,14 +97,14 @@ private: template inline -IOStreamBuffer::IOStreamBuffer( size_t cache = 4096 * 4096 ) - : m_stream( nullptr ) - , m_filesize( 0 ) - , m_cacheSize( cache ) - , m_cachePos( 0 ) - , m_filePos( 0 ) { +IOStreamBuffer::IOStreamBuffer( size_t cache ) +: m_stream( nullptr ) +, m_filesize( 0 ) +, m_cacheSize( cache ) +, m_cachePos( 0 ) +, m_filePos( 0 ) { m_cache.resize( cache ); - std::fill( m_cache.begin(), m_cache.end(), '\0' ); + std::fill( m_cache.begin(), m_cache.end(), '\n' ); } template @@ -88,6 +116,10 @@ IOStreamBuffer::~IOStreamBuffer() { template inline bool IOStreamBuffer::open( IOStream *stream ) { + if ( nullptr != m_stream ) { + return false; + } + if ( nullptr == stream ) { return false; } @@ -123,6 +155,12 @@ size_t IOStreamBuffer::size() const { return m_filesize; } +template +inline +size_t IOStreamBuffer::cacheSize() const { + return m_cacheSize; +} + template inline bool IOStreamBuffer::readNextBlock() { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7c14dff0e..c23ad806f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -70,6 +70,7 @@ SET( TEST_SRCS unit/utImporter.cpp unit/utImproveCacheLocality.cpp unit/utIOSystem.cpp + unit/utIOStreamBuffer.cpp unit/utIssues.cpp unit/utJoinVertices.cpp unit/utLimitBoneWeights.cpp diff --git a/test/unit/TestIOStream.h b/test/unit/TestIOStream.h new file mode 100644 index 000000000..1014a77d0 --- /dev/null +++ b/test/unit/TestIOStream.h @@ -0,0 +1,23 @@ +#pragma once + +#include "DefaultIOStream.h" + +using namespace ::Assimp; + +class TestDefaultIOStream : public DefaultIOStream { +public: + TestDefaultIOStream() + : DefaultIOStream() { + // empty + } + + TestDefaultIOStream( FILE* pFile, const std::string &strFilename ) + : DefaultIOStream( pFile, strFilename ) { + // empty + } + + virtual ~TestDefaultIOStream() { + // empty + } +}; + diff --git a/test/unit/utDefaultIOStream.cpp b/test/unit/utDefaultIOStream.cpp index 7d3c07f20..50a15437c 100644 --- a/test/unit/utDefaultIOStream.cpp +++ b/test/unit/utDefaultIOStream.cpp @@ -37,7 +37,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------*/ #include -#include "DefaultIOStream.h" +#include "TestIOStream.h" using namespace ::Assimp; @@ -45,23 +45,6 @@ class utDefaultIOStream : public ::testing::Test { // empty }; -class TestDefaultIOStream : public DefaultIOStream { -public: - TestDefaultIOStream() - : DefaultIOStream() { - // empty - } - - TestDefaultIOStream( FILE* pFile, const std::string &strFilename ) - : DefaultIOStream( pFile, strFilename ) { - // empty - } - - virtual ~TestDefaultIOStream() { - // empty - } -}; - TEST_F( utDefaultIOStream, FileSizeTest ) { char buffer[ L_tmpnam ]; tmpnam( buffer ); diff --git a/test/unit/utIOStreamBuffer.cpp b/test/unit/utIOStreamBuffer.cpp index 9d8d6ad96..ec70f759b 100644 --- a/test/unit/utIOStreamBuffer.cpp +++ b/test/unit/utIOStreamBuffer.cpp @@ -41,6 +41,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "UnitTestPCH.h" #include "IOStreamBuffer.h" +#include "TestIOStream.h" class IOStreamBufferTest : public ::testing::Test { // empty @@ -58,6 +59,33 @@ TEST_F( IOStreamBufferTest, creationTest ) { EXPECT_TRUE( ok ); } +TEST_F( IOStreamBufferTest, accessCacheSizeTest ) { + IOStreamBuffer myBuffer1; + EXPECT_NE( 0, myBuffer1.cacheSize() ); + + IOStreamBuffer myBuffer2( 100 ); + EXPECT_EQ( 100, myBuffer2.cacheSize() ); +} + +TEST_F( IOStreamBufferTest, open_close_Test ) { + IOStreamBuffer myBuffer; + + EXPECT_FALSE( myBuffer.open( nullptr ) ); + EXPECT_FALSE( myBuffer.close() ); + + char buffer[ L_tmpnam ]; + tmpnam( buffer ); + std::FILE *fs( std::fopen( buffer, "w+" ) ); + size_t written( std::fwrite( buffer, 1, sizeof( char ) * L_tmpnam, fs ) ); + std::fflush( fs ); + + TestDefaultIOStream myStream( fs, buffer ); + + EXPECT_TRUE( myBuffer.open( &myStream ) ); + EXPECT_FALSE( myBuffer.open( &myStream ) ); + EXPECT_TRUE( myBuffer.close() ); +} + TEST_F( IOStreamBufferTest, readlineTest ) { } \ No newline at end of file