refactoring: remove duplicate code.

Signed-off-by: Kim Kulling <kim.kulling@googlemail.com>
pull/395/head
Kim Kulling 2014-09-23 23:34:05 +02:00
parent d228dbeab7
commit a5afbcf29c
4 changed files with 34 additions and 50 deletions

View File

@ -46,6 +46,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "ObjTools.h" #include "ObjTools.h"
#include "ObjFileData.h" #include "ObjFileData.h"
#include "fast_atof.h" #include "fast_atof.h"
#include "ParsingUtils.h"
namespace Assimp { namespace Assimp {
@ -228,7 +229,7 @@ void ObjFileMtlImporter::getColorRGBA( aiColor3D *pColor )
pColor->r = r; pColor->r = r;
// we have to check if color is default 0 with only one token // we have to check if color is default 0 with only one token
if( !isNewLine( *m_DataIt ) ) { if( !IsLineEnd( *m_DataIt ) ) {
m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, g ); m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, g );
m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, b ); m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, b );
} }
@ -257,7 +258,7 @@ void ObjFileMtlImporter::getFloatValue( float &value )
void ObjFileMtlImporter::createMaterial() void ObjFileMtlImporter::createMaterial()
{ {
std::string line( "" ); std::string line( "" );
while ( !isNewLine( *m_DataIt ) ) { while( !IsLineEnd( *m_DataIt ) ) {
line += *m_DataIt; line += *m_DataIt;
++m_DataIt; ++m_DataIt;
} }

View File

@ -186,12 +186,12 @@ void ObjFileParser::copyNextWord(char *pBuffer, size_t length)
{ {
size_t index = 0; size_t index = 0;
m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd); m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
while ( m_DataIt != m_DataItEnd && !isSeparator(*m_DataIt) ) while( m_DataIt != m_DataItEnd && !IsSpaceOrNewLine( *m_DataIt ) ) {
{
pBuffer[index] = *m_DataIt; pBuffer[index] = *m_DataIt;
index++; index++;
if (index == length-1) if( index == length - 1 ) {
break; break;
}
++m_DataIt; ++m_DataIt;
} }
@ -345,7 +345,7 @@ void ObjFileParser::getFace(aiPrimitiveType type)
} }
iPos++; iPos++;
} }
else if ( isSeparator(*pPtr) ) else if( IsSpaceOrNewLine( *pPtr ) )
{ {
iPos = 0; iPos = 0;
} }
@ -463,8 +463,9 @@ void ObjFileParser::getMaterialDesc()
return; return;
char *pStart = &(*m_DataIt); char *pStart = &(*m_DataIt);
while ( m_DataIt != m_DataItEnd && !isSeparator(*m_DataIt) ) while( m_DataIt != m_DataItEnd && !IsSpaceOrNewLine( *m_DataIt ) ) {
++m_DataIt; ++m_DataIt;
}
// Get name // Get name
std::string strName(pStart, &(*m_DataIt)); std::string strName(pStart, &(*m_DataIt));
@ -518,12 +519,14 @@ void ObjFileParser::getMaterialLib()
{ {
// Translate tuple // Translate tuple
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd); m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
if (m_DataIt == m_DataItEnd) if( m_DataIt == m_DataItEnd ) {
return; return;
}
char *pStart = &(*m_DataIt); char *pStart = &(*m_DataIt);
while (m_DataIt != m_DataItEnd && !isNewLine(*m_DataIt)) while( m_DataIt != m_DataItEnd && !IsLineEnd( *m_DataIt ) ) {
m_DataIt++; ++m_DataIt;
}
// Check for existence // Check for existence
const std::string strMatName(pStart, &(*m_DataIt)); const std::string strMatName(pStart, &(*m_DataIt));
@ -557,7 +560,7 @@ void ObjFileParser::getNewMaterial()
char *pStart = &(*m_DataIt); char *pStart = &(*m_DataIt);
std::string strMat( pStart, *m_DataIt ); std::string strMat( pStart, *m_DataIt );
while( m_DataIt != m_DataItEnd && isSeparator( *m_DataIt ) ) { while( m_DataIt != m_DataItEnd && IsSpaceOrNewLine( *m_DataIt ) ) {
++m_DataIt; ++m_DataIt;
} }
std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strMat ); std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strMat );
@ -662,7 +665,7 @@ void ObjFileParser::getObjectName()
return; return;
} }
char *pStart = &(*m_DataIt); char *pStart = &(*m_DataIt);
while( m_DataIt != m_DataItEnd && !isSeparator( *m_DataIt ) ) { while( m_DataIt != m_DataItEnd && !IsSpaceOrNewLine( *m_DataIt ) ) {
++m_DataIt; ++m_DataIt;
} }

View File

@ -45,6 +45,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define OBJ_TOOLS_H_INC #define OBJ_TOOLS_H_INC
#include "fast_atof.h" #include "fast_atof.h"
#include "ParsingUtils.h"
namespace Assimp namespace Assimp
{ {
@ -68,28 +69,6 @@ inline bool isEndOfBuffer( char_t it, char_t end )
return ( it == end ); return ( it == end );
} }
/** @brief Returns true, if token is a space on any supported platform
* @param token Token to search in
* @return true, if token is a space
*/
inline bool isSeparator( char token )
{
return ( token == ' ' ||
token == '\n' ||
token == '\f' ||
token == '\r' ||
token == '\t' );
}
/** @brief Returns true, fi token id a new line marking token.
* @param token Token to search in
* @return true, if token is a newline token.
*/
inline bool isNewLine( char token )
{
return ( token == '\n' || token == '\f' || token == '\r' );
}
/** @brief Returns next word separated by a space /** @brief Returns next word separated by a space
* @param pBuffer Pointer to data buffer * @param pBuffer Pointer to data buffer
* @param pEnd Pointer to end of buffer * @param pEnd Pointer to end of buffer
@ -100,7 +79,7 @@ inline Char_T getNextWord( Char_T pBuffer, Char_T pEnd )
{ {
while ( !isEndOfBuffer( pBuffer, pEnd ) ) while ( !isEndOfBuffer( pBuffer, pEnd ) )
{ {
if ( !isSeparator( *pBuffer ) || isNewLine( *pBuffer ) ) if( !IsSpaceOrNewLine( *pBuffer ) || IsLineEnd( *pBuffer ) )
break; break;
pBuffer++; pBuffer++;
} }
@ -117,7 +96,7 @@ inline Char_T getNextToken( Char_T pBuffer, Char_T pEnd )
{ {
while ( !isEndOfBuffer( pBuffer, pEnd ) ) while ( !isEndOfBuffer( pBuffer, pEnd ) )
{ {
if ( isSeparator( *pBuffer ) ) if( IsSpaceOrNewLine( *pBuffer ) )
break; break;
pBuffer++; pBuffer++;
} }
@ -131,10 +110,10 @@ inline Char_T getNextToken( Char_T pBuffer, Char_T pEnd )
* @return Current-iterator with new position * @return Current-iterator with new position
*/ */
template<class char_t> template<class char_t>
inline char_t skipLine( char_t it, char_t end, unsigned int &uiLine ) inline char_t skipLine( char_t it, char_t end, unsigned int &uiLine ) {
{ while( !isEndOfBuffer( it, end ) && !IsLineEnd( *it ) ) {
while ( !isEndOfBuffer( it, end ) && !isNewLine( *it ) )
++it; ++it;
}
if ( it != end ) if ( it != end )
{ {
++it; ++it;
@ -157,15 +136,16 @@ template<class char_t>
inline char_t getName( char_t it, char_t end, std::string &name ) inline char_t getName( char_t it, char_t end, std::string &name )
{ {
name = ""; name = "";
if ( isEndOfBuffer( it, end ) ) if( isEndOfBuffer( it, end ) ) {
return end; return end;
}
char *pStart = &( *it ); char *pStart = &( *it );
while ( !isEndOfBuffer( it, end ) && !isNewLine( *it ) ) { while( !isEndOfBuffer( it, end ) && !IsLineEnd( *it ) ) {
++it; ++it;
} }
while(isEndOfBuffer( it, end ) || isNewLine( *it ) || isSeparator(*it)) { while( isEndOfBuffer( it, end ) || IsLineEnd( *it ) || IsSpaceOrNewLine( *it ) ) {
--it; --it;
} }
++it; ++it;
@ -196,7 +176,7 @@ inline char_t CopyNextWord( char_t it, char_t end, char *pBuffer, size_t length
{ {
size_t index = 0; size_t index = 0;
it = getNextWord<char_t>( it, end ); it = getNextWord<char_t>( it, end );
while ( !isSeparator( *it ) && !isEndOfBuffer( it, end ) ) while( !IsSpaceOrNewLine( *it ) && !isEndOfBuffer( it, end ) )
{ {
pBuffer[index] = *it ; pBuffer[index] = *it ;
index++; index++;

View File

@ -98,7 +98,7 @@ AI_FORCE_INLINE bool IsSpace( char_t in)
template <class char_t> template <class char_t>
AI_FORCE_INLINE bool IsLineEnd( char_t in) AI_FORCE_INLINE bool IsLineEnd( char_t in)
{ {
return (in == (char_t)'\r' || in == (char_t)'\n' || in == (char_t)'\0'); return (in==(char_t)'\r'||in==(char_t)'\n'||in==(char_t)'\0'||in==(char_t)'\f');
} }
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------