Obj-Stream-Handling: fix compiler-errors and warnings.
authorKim Kulling <kim.kulling@googlemail.com>
Sat, 29 Oct 2016 19:41:24 +0000 (21:41 +0200)
committerKim Kulling <kim.kulling@googlemail.com>
Sat, 29 Oct 2016 19:41:24 +0000 (21:41 +0200)
code/IOStreamBuffer.h
code/ObjFileParser.cpp
test/unit/utIOStreamBuffer.cpp

index e822073..9d87942 100644 (file)
@@ -42,6 +42,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 #include <assimp/types.h>
 #include <assimp/IOStream.hpp>
+#include "ParsingUtils.h"
 
 #include <iostream>
 
@@ -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<T> m_cache;
     size_t m_cachePos;
     size_t m_filePos;
@@ -101,6 +116,8 @@ IOStreamBuffer<T>::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<T>::~IOStreamBuffer() {
 template<class T>
 inline
 bool IOStreamBuffer<T>::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<T>::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<T>::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,12 +203,31 @@ bool IOStreamBuffer<T>::readNextBlock() {
     }
     m_filePos += m_cacheSize;
     m_cachePos = 0;
+    m_blockIdx++;
 
     return true;
 }
 
 template<class T>
 inline
+size_t IOStreamBuffer<T>::getNumBlocks() const {
+    return m_numBlocks;
+}
+
+template<class T>
+inline
+size_t IOStreamBuffer<T>::getCurrentBlockIndex() const {
+    return m_blockIdx;
+}
+
+template<class T>
+inline
+size_t IOStreamBuffer<T>::getFilePos() const {
+    return m_filePos;
+}
+
+template<class T>
+inline
 bool IOStreamBuffer<T>::getNextLine( std::vector<T> &buffer ) {
     buffer.resize( m_cacheSize );
     ::memset( &buffer[ 0 ], '\n', m_cacheSize );
index 2469219..f8fd08d 100644 (file)
@@ -110,8 +110,7 @@ void ObjFileParser::parseFile( IOStreamBuffer<char> &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<char> buffer;
@@ -121,14 +120,14 @@ void ObjFileParser::parseFile( IOStreamBuffer<char> &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)
index ec70f75..d139cfb 100644 (file)
@@ -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 );
+
+    IOStreamBuffer<char> myBuffer( 26 );
+    EXPECT_EQ( 26, myBuffer.cacheSize() );
+
+    TestDefaultIOStream myStream( fs, buffer );
 
-}
\ No newline at end of file
+    EXPECT_TRUE( myBuffer.open( &myStream ) );
+
+    EXPECT_EQ( 10, myBuffer.getNumBlocks() );
+    EXPECT_TRUE( myBuffer.close() );
+}
+
+TEST_F( IOStreamBufferTest, accessBlockIndexTest ) {
+
+}