- merge master into branch
- fix metrix header parsing.

Signed-off-by: Kim Kulling <kim.kulling@googlemail.com>
pull/502/head
Kim Kulling 2014-10-28 21:45:09 +01:00
parent f84589fec4
commit 6843c42da1
2 changed files with 98 additions and 37 deletions

View File

@ -55,8 +55,7 @@ static const std::string Metric = "Metric";
static const std::string GeometryNode = "GeometryNode"; static const std::string GeometryNode = "GeometryNode";
static const std::string GeometryObject = "GeometryObject"; static const std::string GeometryObject = "GeometryObject";
static const std::string Material = "Material"; static const std::string Material = "Material";
static const size_t NumObjects = 4; static const size_t NumObjects = 4;
static const std::string RootNodes[ NumObjects ] = { static const std::string RootNodes[ NumObjects ] = {
Metric, Metric,
GeometryNode, GeometryNode,
@ -64,9 +63,31 @@ static const std::string RootNodes[ NumObjects ] = {
Material Material
}; };
static bool containsNode( const char *bufferPtr, size_t size, const std::string *nodes, size_t numNodes, static const size_t NumSeparator = 4;
const std::string *tokenFound ) { static const std::string Separator[ NumSeparator ] = {
tokenFound = NULL; "(", ")", "{", "}"
};
static const size_t NumToken = 8;
static const std::string Token[ NumToken ] = {
RootNodes[ 0 ],
RootNodes[ 1 ],
RootNodes[ 2 ],
RootNodes[ 3 ],
Separator[ 0 ],
Separator[ 1 ],
Separator[ 2 ],
Separator[ 3 ]
};
static bool isSeparator( char in ) {
return ( in == '(' || in == ')' || in == '{' || in == '}' );
}
static bool containsNode( const char *bufferPtr, size_t size, const std::string *nodes,
size_t numNodes, std::string &tokenFound ) {
tokenFound = "";
if( 0 == numNodes ) { if( 0 == numNodes ) {
return false; return false;
} }
@ -74,7 +95,7 @@ static bool containsNode( const char *bufferPtr, size_t size, const std::string
bool found( false ); bool found( false );
for( size_t i = 0; i < numNodes; ++i ) { for( size_t i = 0; i < numNodes; ++i ) {
if( TokenMatch( bufferPtr, nodes[ i ].c_str(), nodes[ i ].size() ) ) { if( TokenMatch( bufferPtr, nodes[ i ].c_str(), nodes[ i ].size() ) ) {
tokenFound = &nodes[ i ]; tokenFound = nodes[ i ];
found = true; found = true;
break; break;
} }
@ -83,12 +104,27 @@ static bool containsNode( const char *bufferPtr, size_t size, const std::string
return found; return found;
} }
static OpenGEXParser::TokenType getTokenTypeByName( const char *in ) {
ai_assert( NULL != in );
OpenGEXParser::TokenType type( OpenGEXParser::None );
for( size_t i = 0; i < NumToken; ++i ) {
if( TokenMatch( in, Token[ i ].c_str(), Token[ i ].size() ) ) {
type = static_cast<OpenGEXParser::TokenType>( i+1 );
break;
}
}
return type;
}
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
OpenGEXParser::OpenGEXParser( const std::vector<char> &buffer ) OpenGEXParser::OpenGEXParser( const std::vector<char> &buffer )
: m_buffer( buffer ) : m_buffer( buffer )
, m_index( 0 ) , m_index( 0 )
, m_buffersize( buffer.size() ) { , m_buffersize( buffer.size() )
, m_nodeTypeStack() {
// empty
} }
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
@ -107,10 +143,10 @@ void OpenGEXParser::parse() {
std::string OpenGEXParser::getNextToken() { std::string OpenGEXParser::getNextToken() {
std::string token; std::string token;
while( m_index < m_buffersize && IsSpace( m_buffer[ m_index ] ) ) { while( m_index < m_buffersize && IsSpace( m_buffer[ m_index ] ) ) {
++m_index; m_index++;
} }
while( m_index < m_buffersize && !IsSpace( m_buffer[ m_index ] ) ) { while( m_index < m_buffersize && !IsSpace( m_buffer[ m_index ] ) && !isSeparator( m_buffer[ m_index ] ) ) {
token += m_buffer[ m_index ]; token += m_buffer[ m_index ];
m_index++; m_index++;
} }
@ -120,6 +156,13 @@ std::string OpenGEXParser::getNextToken() {
token = getNextToken(); token = getNextToken();
} }
if( token.empty() ) {
if( isSeparator( m_buffer[ m_index ] ) ) {
token += m_buffer[ m_index ];
m_index++;
}
}
return token; return token;
} }
@ -140,7 +183,8 @@ bool OpenGEXParser::skipComments() {
bool OpenGEXParser::parseNextNode() { bool OpenGEXParser::parseNextNode() {
std::string token( getNextToken() ); std::string token( getNextToken() );
std::string rootNodeName; std::string rootNodeName;
if( containsNode( token.c_str(), token.size(), RootNodes, NumObjects, &rootNodeName ) ) { if( containsNode( token.c_str(), token.size(), RootNodes, NumObjects, rootNodeName ) ) {
m_nodeTypeStack.push_back( getTokenTypeByName( rootNodeName.c_str() ) );
if( !getNodeHeader( rootNodeName ) ) { if( !getNodeHeader( rootNodeName ) ) {
return false; return false;
} }
@ -148,6 +192,8 @@ bool OpenGEXParser::parseNextNode() {
if( !getNodeData() ) { if( !getNodeData() ) {
return false; return false;
} }
m_nodeTypeStack.pop_back();
} }
return true; return true;
@ -155,12 +201,15 @@ bool OpenGEXParser::parseNextNode() {
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
bool OpenGEXParser::getNodeHeader( const std::string &name ) { bool OpenGEXParser::getNodeHeader( const std::string &name ) {
if( name == Metric ) { bool success( false );
std::string token( getNextToken() ); if( m_nodeTypeStack.back() == MetricNode ) {
std::string attribName, value;
if( getMetricAttributeKey( attribName, value ) ) {
success = true;
}
} }
return false; return success;
} }
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
@ -226,33 +275,31 @@ bool OpenGEXParser::getFloatData( size_t num, float *data ) {
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
bool OpenGEXParser::getNodeData() { bool OpenGEXParser::getNodeData() {
if( !getBracketOpen() ) {
return false;
}
if( !onMetricNode() ) {
return false;
}
if( !getBracketClose() ) {
return false;
}
return true; return true;
} }
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
bool OpenGEXParser::getMetricAttribute( std::string &attribName ) { bool OpenGEXParser::getMetricAttributeKey( std::string &attribName, std::string &value ) {
return false; bool ok( false );
attribName = "";
std::string token( getNextToken() );
if( token[ 0 ] == '(' ) {
// get attribute
attribName = getNextToken();
std::string equal = getNextToken();
value = getNextToken();
token = getNextToken();
if( token[ 0 ] == ')' ) {
ok = true;
}
}
return ok;
} }
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
bool OpenGEXParser::onMetricNode() { bool OpenGEXParser::onMetricNode( const std::string &attribName ) {
std::string attribName;
if( !getMetricAttribute( attribName ) ) {
return false;
}
if( "distance" == attribName ) { if( "distance" == attribName ) {
float distance( 0.0f ); float distance( 0.0f );
getFloatData( 1, &distance ); getFloatData( 1, &distance );

View File

@ -47,6 +47,19 @@ namespace Assimp {
namespace OpenGEX { namespace OpenGEX {
class OpenGEXParser { class OpenGEXParser {
public:
enum TokenType {
None = 0,
MetricNode,
GeometryNode,
GeometryObject,
Material,
BracketIn,
BracketOut,
CurlyBracketIn,
CurlyBracketOut,
};
public: public:
OpenGEXParser( const std::vector<char> &buffer ); OpenGEXParser( const std::vector<char> &buffer );
~OpenGEXParser(); ~OpenGEXParser();
@ -62,8 +75,8 @@ protected:
bool getStringData( std::string &data ); bool getStringData( std::string &data );
bool getFloatData( size_t num, float *data ); bool getFloatData( size_t num, float *data );
bool getNodeData(); bool getNodeData();
bool getMetricAttribute( std::string &attribName ); bool getMetricAttributeKey( std::string &attribName, std::string &value );
bool onMetricNode(); bool onMetricNode( const std::string &attribName );
private: private:
OpenGEXParser( const OpenGEXParser & ); OpenGEXParser( const OpenGEXParser & );
@ -71,6 +84,7 @@ private:
private: private:
const std::vector<char> &m_buffer; const std::vector<char> &m_buffer;
std::vector<TokenType> m_nodeTypeStack;
size_t m_index; size_t m_index;
size_t m_buffersize; size_t m_buffersize;
}; };