Tests how o adress this topic.
authorKim Kulling <kim.kulling@goolemail.com>
Sun, 28 May 2017 20:25:06 +0000 (22:25 +0200)
committerKim Kulling <kim.kulling@goolemail.com>
Sun, 28 May 2017 20:25:06 +0000 (22:25 +0200)
code/ObjFileParser.cpp
code/ObjFileParser.h
test/unit/utObjTools.cpp

index 0dfd862..08835de 100644 (file)
@@ -161,7 +161,7 @@ void ObjFileParser::parseFile( IOStreamBuffer<char> &streamBuffer ) {
             {
                 ++m_DataIt;
                 if (*m_DataIt == ' ' || *m_DataIt == '\t') {
-                    size_t numComponents = getNumComponentsInLine();
+                    size_t numComponents = getNumComponentsInDataDefinition();
                     if (numComponents == 3) {
                         // read in vertex definition
                         getVector3(m_pModel->m_Vertices);
@@ -274,21 +274,40 @@ void ObjFileParser::copyNextWord(char *pBuffer, size_t length) {
     pBuffer[index] = '\0';
 }
 
-size_t ObjFileParser::getNumComponentsInLine() {
+static bool isDataDefinitionEnd( const char *tmp ) {
+    if ( *tmp == '\\' ) {
+        tmp++;
+        if ( IsLineEnd( tmp ) ) {
+            return false;
+        }
+    } else {
+        return IsLineEnd( tmp );
+    }
+    return false;
+}
+
+size_t ObjFileParser::getNumComponentsInDataDefinition() {
     size_t numComponents( 0 );
     const char* tmp( &m_DataIt[0] );
-    while( !IsLineEnd( *tmp ) ) {        
+    while ( !isDataDefinitionEnd( tmp ) ) {
+    //while( !IsLineEnd( *tmp ) ) {        
         if ( !SkipSpaces( &tmp ) ) {
             break;
         }
+        const bool isNum( IsNumeric( *tmp ) );
         SkipToken( tmp );
-        ++numComponents;
+        if ( isNum ) {
+            ++numComponents;
+        }
+        if ( !SkipSpaces( &tmp ) ) {
+            break;
+        }
     }
     return numComponents;
 }
 
 void ObjFileParser::getVector( std::vector<aiVector3D> &point3d_array ) {
-    size_t numComponents = getNumComponentsInLine();
+    size_t numComponents = getNumComponentsInDataDefinition();
     ai_real x, y, z;
     if( 2 == numComponents ) {
         copyNextWord( m_buffer, Buffersize );
index 64dc18c..fa5b3ca 100644 (file)
@@ -91,6 +91,8 @@ protected:
     void copyNextWord(char *pBuffer, size_t length);
     /// Method to copy the new line.
 //    void copyNextLine(char *pBuffer, size_t length);
+    /// Get the number of components in a line.
+    size_t getNumComponentsInDataDefinition();
     /// Stores the vector
     void getVector( std::vector<aiVector3D> &point3d_array );
     /// Stores the following 3d vector.
@@ -129,8 +131,6 @@ protected:
     bool needsNewMesh( const std::string &rMaterialName );
     /// Error report in token
     void reportErrorTokenInFace();
-    /// Get the number of components in a line.
-    size_t getNumComponentsInLine();
 
 private:
     // Copy and assignment constructor should be private
index cacace4..f916d7e 100644 (file)
@@ -51,13 +51,23 @@ class utObjTools : public ::testing::Test {
 
 class TestObjFileParser : public ObjFileParser {
 public:
-    TestObjFileParser() : ObjFileParser(){}
-    ~TestObjFileParser() {}
+    TestObjFileParser() : ObjFileParser(){
+        // empty
+    }
+
+    ~TestObjFileParser() {
+        // empty
+    }
+    
     void testCopyNextWord( char *pBuffer, size_t length ) {
         copyNextWord( pBuffer, length );
     }
 
+    size_t testGetNumComponentsInDataDefinition() {
+        return getNumComponentsInDataDefinition();
+    }
 };
+
 TEST_F( utObjTools, skipDataLine_OneLine_Success ) {
     std::vector<char> buffer;
     std::string data( "v -0.5 -0.5 0.5\nend" );
@@ -90,4 +100,18 @@ TEST_F( utObjTools, skipDataLine_TwoLines_Success ) {
 
     test_parser.testCopyNextWord( data_buffer, Size );
     EXPECT_EQ( data_buffer[ 0 ], '-' );
-}
\ No newline at end of file
+}
+
+TEST_F( utObjTools, countComponents_TwoLines_Success ) {
+    TestObjFileParser test_parser;
+    std::string data( "-2.061493116917992e-15 -0.9009688496589661 \\n-0.4338837265968323" );
+    std::vector<char> buffer;
+    buffer.resize( data.size() );
+    ::memcpy( &buffer[ 0 ], &data[ 0 ], data.size() );
+    test_parser.setBuffer( buffer );
+    static const size_t Size = 4096UL;
+    char data_buffer[ Size ];
+
+    size_t numComps = test_parser.testGetNumComponentsInDataDefinition();
+    EXPECT_EQ( 3U, numComps );
+}