From b9261f01a3b23338781346332fee613896b78fd3 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sat, 29 Oct 2016 21:41:24 +0200 Subject: [PATCH] Obj-Stream-Handling: fix compiler-errors and warnings. --- code/IOStreamBuffer.h | 52 ++++++++++++++++++++++++++++++++-- code/ObjFileParser.cpp | 17 ++++++----- test/unit/utIOStreamBuffer.cpp | 20 ++++++++++++- 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/code/IOStreamBuffer.h b/code/IOStreamBuffer.h index e82207340..9d87942df 100644 --- a/code/IOStreamBuffer.h +++ b/code/IOStreamBuffer.h @@ -42,6 +42,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include "ParsingUtils.h" #include @@ -81,6 +82,18 @@ public: /// @return true if successful. bool readNextBlock(); + /// @brief Returns the number of blocks to read. + /// @return The number of blocks. + size_t getNumBlocks() const; + + /// @brief Returns the current block index. + /// @return The current block index. + size_t getCurrentBlockIndex() const; + + /// @brief Returns the current file pos. + /// @return The current file pos. + size_t getFilePos() const; + /// @brief Will read the next line. /// @param buffer The buffer for the next line. /// @return true if successful. @@ -90,6 +103,8 @@ private: IOStream *m_stream; size_t m_filesize; size_t m_cacheSize; + size_t m_numBlocks; + size_t m_blockIdx; std::vector m_cache; size_t m_cachePos; size_t m_filePos; @@ -101,6 +116,8 @@ IOStreamBuffer::IOStreamBuffer( size_t cache ) : m_stream( nullptr ) , m_filesize( 0 ) , m_cacheSize( cache ) +, m_numBlocks( 0 ) +, m_blockIdx( 0 ) , m_cachePos( 0 ) , m_filePos( 0 ) { m_cache.resize( cache ); @@ -116,10 +133,12 @@ IOStreamBuffer::~IOStreamBuffer() { template inline bool IOStreamBuffer::open( IOStream *stream ) { + // file still opened! if ( nullptr != m_stream ) { return false; } + // Invalid stream pointer if ( nullptr == stream ) { return false; } @@ -133,6 +152,11 @@ bool IOStreamBuffer::open( IOStream *stream ) { m_cacheSize = m_filesize; } + m_numBlocks = m_filesize / m_cacheSize; + if ( ( m_filesize % m_cacheSize ) > 0 ) { + m_numBlocks++; + } + return true; } @@ -143,8 +167,13 @@ bool IOStreamBuffer::close() { return false; } - m_stream = nullptr; - m_filesize = 0; + // init counters and state vars + m_stream = nullptr; + m_filesize = 0; + m_numBlocks = 0; + m_blockIdx = 0; + m_cachePos = 0; + m_filePos = 0; return true; } @@ -174,10 +203,29 @@ bool IOStreamBuffer::readNextBlock() { } m_filePos += m_cacheSize; m_cachePos = 0; + m_blockIdx++; return true; } +template +inline +size_t IOStreamBuffer::getNumBlocks() const { + return m_numBlocks; +} + +template +inline +size_t IOStreamBuffer::getCurrentBlockIndex() const { + return m_blockIdx; +} + +template +inline +size_t IOStreamBuffer::getFilePos() const { + return m_filePos; +} + template inline bool IOStreamBuffer::getNextLine( std::vector &buffer ) { diff --git a/code/ObjFileParser.cpp b/code/ObjFileParser.cpp index 2469219f8..f8fd08d4a 100644 --- a/code/ObjFileParser.cpp +++ b/code/ObjFileParser.cpp @@ -110,8 +110,7 @@ void ObjFileParser::parseFile( IOStreamBuffer &streamBuffer ) { const unsigned int progressTotal = 3 * bytesToProcess; const unsigned int progressOffset = bytesToProcess; unsigned int processed = 0; - - //DataArrayIt lastDataIt = m_DataIt; + size_t lastFilePos( 0 ); bool endOfFile( false ); std::vector buffer; @@ -121,14 +120,14 @@ void ObjFileParser::parseFile( IOStreamBuffer &streamBuffer ) { m_DataIt = buffer.begin(); m_DataItEnd = buffer.end(); - // Handle progress reporting - /*processed += std::distance(lastDataIt, m_DataIt); - lastDataIt = m_DataIt; - if (processed > (progressCounter * updateProgressEveryBytes)) - { + // Handle progress reporting + const size_t filePos( streamBuffer.getFilePos() ); + if ( lastFilePos < filePos ) { + processed += filePos; + lastFilePos = filePos; progressCounter++; - m_progress->UpdateFileRead(progressOffset + processed*2, progressTotal); - }*/ + m_progress->UpdateFileRead( progressOffset + processed * 2, progressTotal ); + } // parse line switch (*m_DataIt) diff --git a/test/unit/utIOStreamBuffer.cpp b/test/unit/utIOStreamBuffer.cpp index ec70f759b..d139cfbec 100644 --- a/test/unit/utIOStreamBuffer.cpp +++ b/test/unit/utIOStreamBuffer.cpp @@ -87,5 +87,23 @@ TEST_F( IOStreamBufferTest, open_close_Test ) { } TEST_F( IOStreamBufferTest, readlineTest ) { + 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 ); -} \ No newline at end of file + IOStreamBuffer myBuffer( 26 ); + EXPECT_EQ( 26, myBuffer.cacheSize() ); + + TestDefaultIOStream myStream( fs, buffer ); + + EXPECT_TRUE( myBuffer.open( &myStream ) ); + + EXPECT_EQ( 10, myBuffer.getNumBlocks() ); + EXPECT_TRUE( myBuffer.close() ); +} + +TEST_F( IOStreamBufferTest, accessBlockIndexTest ) { + +}