OpenDDLParser: latest greatest.

pull/682/head
Kim Kulling 2015-10-31 09:43:34 +01:00
parent 6f75ea5702
commit eadc6116c0
9 changed files with 466 additions and 76 deletions

View File

@ -325,7 +325,7 @@ void Q3BSPFileImporter::CreateNodes( const Q3BSP::Q3BSPModel *pModel, aiScene* p
matIdx++;
}
pScene->mNumMeshes = MeshArray.size();
pScene->mNumMeshes = static_cast<unsigned int>( MeshArray.size() );
if ( pScene->mNumMeshes > 0 )
{
pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes ];

View File

@ -117,7 +117,6 @@ const std::string &DDLNode::getType() const {
return m_type;
}
void DDLNode::setName( const std::string &name ) {
m_name = name;
}
@ -151,6 +150,7 @@ Property *DDLNode::findPropertyByName( const std::string &name ) {
if( ddl_nullptr == m_properties ) {
return ddl_nullptr;
}
Property *current( m_properties );
while( ddl_nullptr != current ) {
int res = strncmp( current->m_key->m_text.m_buffer, name.c_str(), name.size() );

View File

@ -22,16 +22,61 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-----------------------------------------------------------------------------------------------*/
#include <openddlparser/OpenDDLExport.h>
#include <openddlparser/DDLNode.h>
#include <openddlparser/Value.h>
#include <openddlparser/OpenDDLParser.h>
#include <sstream>
BEGIN_ODDLPARSER_NS
IOStreamBase::IOStreamBase()
: m_file( ddl_nullptr ) {
// empty
}
IOStreamBase::~IOStreamBase() {
// empty
}
bool IOStreamBase::open( const std::string &name ) {
m_file = ::fopen( name.c_str(), "a" );
if (m_file == ddl_nullptr) {
return false;
}
return true;
}
bool IOStreamBase::close() {
if (ddl_nullptr == m_file) {
return false;
}
::fclose( m_file );
m_file = ddl_nullptr;
return true;
}
void IOStreamBase::write( const std::string &statement ) {
if (ddl_nullptr == m_file) {
return;
}
::fwrite( statement.c_str(), sizeof( char ), statement.size(), m_file );
}
struct DDLNodeIterator {
const DDLNode::DllNodeList &m_childs;
size_t m_idx;
DDLNodeIterator( const DDLNode::DllNodeList &childs )
: m_childs( childs )
, m_idx( 0 ) {
// empty
}
~DDLNodeIterator() {
// empty
}
bool getNext( DDLNode **node ) {
@ -45,33 +90,43 @@ struct DDLNodeIterator {
}
};
OpenDDLExport::OpenDDLExport()
:m_file( nullptr ) {
static void writeLineEnd( std::string &statement ) {
statement += "\n";
}
OpenDDLExport::OpenDDLExport( IOStreamBase *stream )
: m_stream( stream ) {
if (ddl_nullptr == m_stream) {
m_stream = new IOStreamBase();
}
}
OpenDDLExport::~OpenDDLExport() {
if( nullptr != m_file ) {
::fclose( m_file );
m_file = nullptr;
if (ddl_nullptr != m_stream) {
m_stream->close();
}
delete m_stream;
}
bool OpenDDLExport::exportContext( Context *ctx, const std::string &filename ) {
if( filename.empty() ) {
return false;
}
if( ddl_nullptr == ctx ) {
return false;
}
DDLNode *root( ctx->m_root );
if( nullptr == root ) {
if ( ddl_nullptr == root ) {
return true;
}
return handleNode( root );
if (!filename.empty()) {
if (!m_stream->open( filename )) {
return false;
}
}
const bool retValue( handleNode( root ) );
return retValue;
}
bool OpenDDLExport::handleNode( DDLNode *node ) {
@ -85,12 +140,13 @@ bool OpenDDLExport::handleNode( DDLNode *node ) {
}
DDLNode *current( ddl_nullptr );
DDLNodeIterator it( childs );
std::string statement;
bool success( true );
while( it.getNext( &current ) ) {
if( ddl_nullptr != current ) {
success |= writeNode( current );
success |= writeNode( current, statement );
if( !handleNode( current ) ) {
success != false;
success = false;
}
}
}
@ -98,17 +154,257 @@ bool OpenDDLExport::handleNode( DDLNode *node ) {
return success;
}
bool OpenDDLExport::writeNode( DDLNode *node ) {
bool success( true );
if( node->hasProperties() ) {
success |= writeProperties( node );
bool OpenDDLExport::writeToStream( const std::string &statement ) {
if (ddl_nullptr == m_stream ) {
return false;
}
if ( !statement.empty()) {
m_stream->write( statement );
}
return true;
}
bool OpenDDLExport::writeProperties( DDLNode *node ) {
bool OpenDDLExport::writeNode( DDLNode *node, std::string &statement ) {
bool success( true );
writeNodeHeader( node, statement );
if (node->hasProperties()) {
success |= writeProperties( node, statement );
}
writeLineEnd( statement );
statement = "}";
DataArrayList *al( node->getDataArrayList() );
if ( ddl_nullptr != al ) {
writeValueType( al->m_dataList->m_type, al->m_numItems, statement );
writeValueArray( al, statement );
}
Value *v( node->getValue() );
if (ddl_nullptr != v ) {
writeValueType( v->m_type, 1, statement );
statement = "{";
writeLineEnd( statement );
writeValue( v, statement );
statement = "}";
writeLineEnd( statement );
}
statement = "}";
writeLineEnd( statement );
writeToStream( statement );
return true;
}
bool OpenDDLExport::writeNodeHeader( DDLNode *node, std::string &statement ) {
if (ddl_nullptr == node) {
return false;
}
statement += node->getType();
const std::string &name( node->getName() );
if ( !name.empty() ) {
statement += " ";
statement += "$";
statement += name;
}
return true;
}
bool OpenDDLExport::writeProperties( DDLNode *node, std::string &statement ) {
if ( ddl_nullptr == node ) {
return false;
}
Property *prop( node->getProperties() );
// if no properties are there, return
if ( ddl_nullptr == prop ) {
return true;
}
if ( ddl_nullptr != prop ) {
// for instance (attrib = "position", bla=2)
statement += "(";
bool first( true );
while ( ddl_nullptr != prop ) {
if (!first) {
statement += ", ";
} else {
first = false;
}
statement += std::string( prop->m_key->m_text.m_buffer );
statement += " = ";
writeValue( prop->m_value, statement );
prop = prop->m_next;
}
statement += ")";
}
return true;
}
bool OpenDDLExport::writeValueType( Value::ValueType type, size_t numItems, std::string &statement ) {
if ( Value::ddl_types_max == type) {
return false;
}
const std::string typeStr( getTypeToken( type ) );
statement += typeStr;
// if we have an array to write
if ( numItems > 1 ) {
statement += "[";
char buffer[ 256 ];
::memset( buffer, '\0', 256 * sizeof( char ) );
sprintf( buffer, "%d", numItems );
statement += buffer;
statement += "]";
}
return true;
}
bool OpenDDLExport::writeValue( Value *val, std::string &statement ) {
if (ddl_nullptr == val) {
return false;
}
switch ( val->m_type ) {
case Value::ddl_bool:
if ( true == val->getBool() ) {
statement += "true";
} else {
statement += "false";
}
break;
case Value::ddl_int8:
{
std::stringstream stream;
const int i = static_cast<int>( val->getInt8() );
stream << i;
statement += stream.str();
}
break;
case Value::ddl_int16:
{
std::stringstream stream;
char buffer[ 256 ];
::memset( buffer, '\0', 256 * sizeof( char ) );
sprintf( buffer, "%d", val->getInt16() );
statement += buffer;
}
break;
case Value::ddl_int32:
{
std::stringstream stream;
char buffer[ 256 ];
::memset( buffer, '\0', 256 * sizeof( char ) );
const int i = static_cast< int >( val->getInt32() );
sprintf( buffer, "%d", i );
statement += buffer;
}
break;
case Value::ddl_int64:
{
std::stringstream stream;
const int i = static_cast< int >( val->getInt64() );
stream << i;
statement += stream.str();
}
break;
case Value::ddl_unsigned_int8:
{
std::stringstream stream;
const int i = static_cast< unsigned int >( val->getUnsignedInt8() );
stream << i;
statement += stream.str();
}
break;
case Value::ddl_unsigned_int16:
{
std::stringstream stream;
const int i = static_cast< unsigned int >( val->getUnsignedInt16() );
stream << i;
statement += stream.str();
}
break;
case Value::ddl_unsigned_int32:
{
std::stringstream stream;
const int i = static_cast< unsigned int >( val->getUnsignedInt32() );
stream << i;
statement += stream.str();
}
break;
case Value::ddl_unsigned_int64:
{
std::stringstream stream;
const int i = static_cast< unsigned int >( val->getUnsignedInt64() );
stream << i;
statement += stream.str();
}
break;
case Value::ddl_half:
break;
case Value::ddl_float:
{
std::stringstream stream;
stream << val->getFloat();
statement += stream.str();
}
break;
case Value::ddl_double:
break;
case Value::ddl_string:
{
std::stringstream stream;
stream << val->getString();
statement += "\"";
statement += stream.str();
statement += "\"";
}
break;
case Value::ddl_ref:
break;
case Value::ddl_none:
case Value::ddl_types_max:
default:
break;
}
return true;
}
bool OpenDDLExport::writeValueArray( DataArrayList *al, std::string &statement ) {
if (ddl_nullptr == al) {
return false;
}
if (0 == al->m_numItems) {
return true;
}
DataArrayList *nextDataArrayList = al ;
Value *nextValue( nextDataArrayList->m_dataList );
while (ddl_nullptr != nextDataArrayList) {
if (ddl_nullptr != nextDataArrayList) {
statement += "{ ";
nextValue = nextDataArrayList->m_dataList;
size_t idx( 0 );
while (ddl_nullptr != nextValue) {
if (idx > 0) {
statement += ", ";
}
writeValue( nextValue, statement );
nextValue = nextValue->m_next;
idx++;
}
statement += " }";
}
nextDataArrayList = nextDataArrayList->m_next;
}
return true;
}

View File

@ -21,6 +21,7 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-----------------------------------------------------------------------------------------------*/
#include <openddlparser/OpenDDLParser.h>
#include <openddlparser/OpenDDLExport.h>
#include <cassert>
#include <iostream>
@ -67,6 +68,10 @@ namespace Grammar {
};
} // Namespace Grammar
const char *getTypeToken( Value::ValueType type ) {
return Grammar::PrimitiveTypeToken[ type ];
}
static void logInvalidTokenError( char *in, const std::string &exp, OpenDDLParser::logCallback callback ) {
std::stringstream stream;
stream << "Invalid token " << *in << ", " << exp << " expected." << std::endl;
@ -211,13 +216,12 @@ bool OpenDDLParser::exportContext( Context *ctx, const std::string &filename ) {
return false;
}
if( filename.empty() ) {
return false;
}
OpenDDLExport myExporter;
return myExporter.exportContext( ctx, filename );
return false;
}
char *OpenDDLParser::parseNextNode( char *in, char *end ) {
in = parseHeader( in, end );
in = parseStructure( in, end );
@ -227,6 +231,7 @@ char *OpenDDLParser::parseNextNode( char *in, char *end ) {
static void dumpId( Identifier *id ) {
if( ddl_nullptr != id ) {
if ( ddl_nullptr != id->m_text.m_buffer ) { }
std::cout << id->m_text.m_buffer << std::endl;
}
}
@ -366,7 +371,7 @@ char *OpenDDLParser::parseStructureBody( char *in, char *end, bool &error ) {
Value *values( ddl_nullptr );
if( 1 == arrayLen ) {
size_t numRefs( 0 ), numValues( 0 );
in = parseDataList( in, end, &values, numValues, &refs, numRefs );
in = parseDataList( in, end, type, &values, numValues, &refs, numRefs );
setNodeValues( top(), values );
setNodeReferences( top(), refs );
} else if( arrayLen > 1 ) {
@ -548,7 +553,7 @@ char *OpenDDLParser::parsePrimitiveDataType( char *in, char *end, Value::ValueTy
while ( in != end ) {
in++;
if( *in == Grammar::CloseArrayToken[ 0 ] ) {
len = atoi( start );
len = ::atoi( start );
ok = true;
in++;
break;
@ -814,7 +819,7 @@ char *OpenDDLParser::parseProperty( char *in, char *end, Property **prop ) {
return in;
}
char *OpenDDLParser::parseDataList( char *in, char *end, Value **data, size_t &numValues, Reference **refs, size_t &numRefs ) {
char *OpenDDLParser::parseDataList( char *in, char *end, Value::ValueType type, Value **data, size_t &numValues, Reference **refs, size_t &numRefs ) {
*data = ddl_nullptr;
numValues = numRefs = 0;
if( ddl_nullptr == in || in == end ) {
@ -828,23 +833,37 @@ char *OpenDDLParser::parseDataList( char *in, char *end, Value **data, size_t &n
while( '}' != *in ) {
current = ddl_nullptr;
in = lookForNextToken( in, end );
if( isInteger( in, end ) ) {
if (Value::ddl_none == type) {
if (isInteger( in, end )) {
in = parseIntegerLiteral( in, end, &current );
} else if( isFloat( in, end ) ) {
}
else if (isFloat( in, end )) {
in = parseFloatingLiteral( in, end, &current );
} else if( isStringLiteral( *in ) ) {
}
else if (isStringLiteral( *in )) {
in = parseStringLiteral( in, end, &current );
} else if( isHexLiteral( in, end ) ) {
}
else if (isHexLiteral( in, end )) {
in = parseHexaLiteral( in, end, &current );
} else { // reference data
}
else { // reference data
std::vector<Name*> names;
in = parseReference( in, end, names );
if( !names.empty() ) {
if (!names.empty()) {
Reference *ref = new Reference( names.size(), &names[ 0 ] );
*refs = ref;
numRefs = names.size();
}
}
} else {
if (Value::ddl_int32 == type) {
in = parseIntegerLiteral( in, end, &current );
} else if (Value::ddl_float == type) {
in = parseFloatingLiteral( in, end, &current );
} else if (Value::ddl_string == type) {
in = parseStringLiteral( in, end, &current );
}
}
if( ddl_nullptr != current ) {
if( ddl_nullptr == *data ) {
@ -891,7 +910,8 @@ char *OpenDDLParser::parseDataArrayList( char *in, char *end, DataArrayList **da
do {
size_t numRefs( 0 ), numValues( 0 );
currentValue = ddl_nullptr;
in = parseDataList( in, end, &currentValue, numValues, &refs, numRefs );
Value::ValueType type( Value::ddl_none );
in = parseDataList( in, end, type, &currentValue, numValues, &refs, numRefs );
if( ddl_nullptr != currentValue ) {
if( ddl_nullptr == prev ) {
*dataList = createDataArrayList( currentValue, numValues );

View File

@ -95,7 +95,7 @@ bool Value::Iterator::operator == ( const Iterator &rhs ) const {
}
Value *Value::Iterator::operator->( ) const {
if( nullptr == m_current ) {
if(ddl_nullptr == m_current ) {
return ddl_nullptr;
}
return m_current;
@ -120,7 +120,6 @@ void Value::setBool( bool value ) {
bool Value::getBool() {
assert( ddl_bool == m_type );
return ( *m_data == 1 );
}
@ -141,7 +140,9 @@ void Value::setInt16( int16 value ) {
int16 Value::getInt16() {
assert( ddl_int16 == m_type );
return ( int16 ) ( *m_data );
int16 i;
::memcpy( &i, m_data, m_size );
return i;
}
void Value::setInt32( int32 value ) {
@ -151,16 +152,21 @@ void Value::setInt32( int32 value ) {
int32 Value::getInt32() {
assert( ddl_int32 == m_type );
return ( int32 ) ( *m_data );
int32 i;
::memcpy( &i, m_data, m_size );
return i;
}
void Value::setInt64( int64 value ) {
assert( ddl_int32 == m_type );
assert( ddl_int64 == m_type );
::memcpy( m_data, &value, m_size );
}
int64 Value::getInt64() {
return ( int64 ) ( *m_data );
assert( ddl_int64 == m_type );
int64 i;
::memcpy( &i, m_data, m_size );
return i;
}
void Value::setUnsignedInt8( uint8 value ) {
@ -170,7 +176,9 @@ void Value::setUnsignedInt8( uint8 value ) {
uint8 Value::getUnsignedInt8() const {
assert( ddl_unsigned_int8 == m_type );
return ( uint8 ) ( *m_data );
uint8 i;
::memcpy( &i, m_data, m_size );
return i;
}
void Value::setUnsignedInt16( uint16 value ) {
@ -180,7 +188,9 @@ void Value::setUnsignedInt16( uint16 value ) {
uint16 Value::getUnsignedInt16() const {
assert( ddl_unsigned_int16 == m_type );
return ( uint8 ) ( *m_data );
uint16 i;
::memcpy( &i, m_data, m_size );
return i;
}
void Value::setUnsignedInt32( uint32 value ) {
@ -190,7 +200,9 @@ void Value::setUnsignedInt32( uint32 value ) {
uint32 Value::getUnsignedInt32() const {
assert( ddl_unsigned_int32 == m_type );
return ( uint8 ) ( *m_data );
uint32 i;
::memcpy( &i, m_data, m_size );
return i;
}
void Value::setUnsignedInt64( uint64 value ) {
@ -200,7 +212,9 @@ void Value::setUnsignedInt64( uint64 value ) {
uint64 Value::getUnsignedInt64() const {
assert( ddl_unsigned_int64 == m_type );
return ( uint64 ) ( *m_data );
uint64 i;
::memcpy( &i, m_data, m_size );
return i;
}
void Value::setFloat( float value ) {
@ -226,6 +240,7 @@ void Value::setDouble( double value ) {
}
double Value::getDouble() const {
assert( ddl_double == m_type );
double v;
::memcpy( &v, m_data, m_size );
return v;
@ -237,6 +252,7 @@ void Value::setString( const std::string &str ) {
m_data[ str.size() ] = '\0';
}
const char *Value::getString() const {
assert( ddl_string == m_type );
return (const char*) m_data;
}
@ -312,22 +328,25 @@ Value *ValueAllocator::allocPrimData( Value::ValueType type, size_t len ) {
data->m_size = sizeof( bool );
break;
case Value::ddl_int8:
data->m_size = sizeof( char );
data->m_size = sizeof( int8 );
break;
case Value::ddl_int16:
data->m_size = sizeof( short );
data->m_size = sizeof( int16 );
break;
case Value::ddl_int32:
data->m_size = sizeof( int );
data->m_size = sizeof( int32 );
break;
case Value::ddl_int64:
data->m_size = sizeof( int64 );
break;
case Value::ddl_unsigned_int8:
data->m_size = sizeof( unsigned char );
data->m_size = sizeof( uint8 );
break;
case Value::ddl_unsigned_int16:
data->m_size = sizeof( uint16 );
break;
case Value::ddl_unsigned_int32:
data->m_size = sizeof( unsigned int );
data->m_size = sizeof( uint32 );
break;
case Value::ddl_unsigned_int64:
data->m_size = sizeof( uint64 );

View File

@ -26,6 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#ifndef _WIN32
# include <inttypes.h>

View File

@ -23,26 +23,66 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#pragma once
#include <openddlparser/OpenDDLCommon.h>
#include <openddlparser/Value.h>
BEGIN_ODDLPARSER_NS
///
/// @ingroup OpenDDLParser
/// @brief This class represents the OpenDDLExporter.
///
class DLL_ODDLPARSER_EXPORT OpenDDLExport {
//-------------------------------------------------------------------------------------------------
/// @ingroup IOStreamBase
/// @brief This class represents the stream to write out.
//-------------------------------------------------------------------------------------------------
class DLL_ODDLPARSER_EXPORT IOStreamBase {
public:
OpenDDLExport();
~OpenDDLExport();
bool exportContext( Context *ctx, const std::string &filename );
bool handleNode( DDLNode *node );
protected:
bool writeNode( DDLNode *node );
bool writeProperties( DDLNode *node );
IOStreamBase();
virtual ~IOStreamBase();
virtual bool open( const std::string &anme );
virtual bool close();
virtual void write( const std::string &statement );
private:
FILE *m_file;
};
//-------------------------------------------------------------------------------------------------
///
/// @ingroup OpenDDLParser
/// @brief This class represents the OpenDDLExporter.
///
//-------------------------------------------------------------------------------------------------
class DLL_ODDLPARSER_EXPORT OpenDDLExport {
public:
/// @brief The class constructor
OpenDDLExport( IOStreamBase *stream = ddl_nullptr );
/// @brief The class destructor.
~OpenDDLExport();
/// @brief Export the data of a parser context.
/// @param ctx [in] Pointer to the context.
/// @param filename [in] The filename for the export.
/// @return True in case of success, false in case of an error.
bool exportContext( Context *ctx, const std::string &filename );
/// @brief Handles a node export.
/// @param node [in] The node to handle with.
/// @return True in case of success, false in case of an error.
bool handleNode( DDLNode *node );
/// @brief Writes the statement to the stream.
/// @param statement [in] The content to write.
/// @return True in case of success, false in case of an error.
bool writeToStream( const std::string &statement );
protected:
bool writeNode( DDLNode *node, std::string &statement );
bool writeNodeHeader( DDLNode *node, std::string &statement );
bool writeProperties( DDLNode *node, std::string &statement );
bool writeValueType( Value::ValueType type, size_t numItems, std::string &statement );
bool writeValue( Value *val, std::string &statement );
bool writeValueArray( DataArrayList *al, std::string &statement );
private:
IOStreamBase *m_stream;
};
END_ODDLPARSER_NS

View File

@ -80,6 +80,8 @@ enum LogSeverity {
ddl_error_msg ///< Parser errors
};
DLL_ODDLPARSER_EXPORT const char *getTypeToken( Value::ValueType type );
//-------------------------------------------------------------------------------------------------
/// @class OpenDDLParser
/// @ingroup OpenDDLParser
@ -168,7 +170,7 @@ public: // parser helpers
static char *parseStringLiteral( char *in, char *end, Value **stringData );
static char *parseHexaLiteral( char *in, char *end, Value **data );
static char *parseProperty( char *in, char *end, Property **prop );
static char *parseDataList( char *in, char *end, Value **data, size_t &numValues, Reference **refs, size_t &numRefs );
static char *parseDataList( char *in, char *end, Value::ValueType type, Value **data, size_t &numValues, Reference **refs, size_t &numRefs );
static char *parseDataArrayList( char *in, char *end, DataArrayList **dataList );
static const char *getVersion();

View File

@ -151,11 +151,23 @@ public:
size_t m_size;
unsigned char *m_data;
Value *m_next;
private:
Value &operator =( const Value & );
Value( const Value & );
};
///------------------------------------------------------------------------------------------------
/// @brief This class implements the value allocator.
///------------------------------------------------------------------------------------------------
struct DLL_ODDLPARSER_EXPORT ValueAllocator {
static Value *allocPrimData( Value::ValueType type, size_t len = 1 );
static void releasePrimData( Value **data );
private:
ValueAllocator();
ValueAllocator( const ValueAllocator & );
ValueAllocator &operator = ( const ValueAllocator & );
};
END_ODDLPARSER_NS