diff --git a/code/ObjFileParser.cpp b/code/ObjFileParser.cpp index 0dfd8625b..08835debc 100644 --- a/code/ObjFileParser.cpp +++ b/code/ObjFileParser.cpp @@ -161,7 +161,7 @@ void ObjFileParser::parseFile( IOStreamBuffer &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 &point3d_array ) { - size_t numComponents = getNumComponentsInLine(); + size_t numComponents = getNumComponentsInDataDefinition(); ai_real x, y, z; if( 2 == numComponents ) { copyNextWord( m_buffer, Buffersize ); diff --git a/code/ObjFileParser.h b/code/ObjFileParser.h index 64dc18c8e..fa5b3ca31 100644 --- a/code/ObjFileParser.h +++ b/code/ObjFileParser.h @@ -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 &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 diff --git a/test/unit/utObjTools.cpp b/test/unit/utObjTools.cpp index cacace49d..f916d7ef4 100644 --- a/test/unit/utObjTools.cpp +++ b/test/unit/utObjTools.cpp @@ -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 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 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 ); +}