diff --git a/contrib/openddlparser/code/OpenDDLCommon.cpp b/contrib/openddlparser/code/OpenDDLCommon.cpp new file mode 100644 index 000000000..00583a101 --- /dev/null +++ b/contrib/openddlparser/code/OpenDDLCommon.cpp @@ -0,0 +1,168 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2014-2015 Kim Kulling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +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 +#include + +BEGIN_ODDLPARSER_NS + +Text::Text( const char *buffer, size_t numChars ) +: m_capacity( 0 ) +, m_len( 0 ) +, m_buffer( ddl_nullptr ) { + set( buffer, numChars ); +} + +Text::~Text() { + clear(); +} + +void Text::clear() { + delete[] m_buffer; + m_buffer = ddl_nullptr; + m_capacity = 0; + m_len = 0; +} + +void Text::set( const char *buffer, size_t numChars ) { + clear(); + if( numChars > 0 ) { + m_len = numChars; + m_capacity = m_len + 1; + m_buffer = new char[ m_capacity ]; + strncpy( m_buffer, buffer, numChars ); + m_buffer[ numChars ] = '\0'; + } +} + +bool Text::operator == ( const std::string &name ) const { + if( m_len != name.size() ) { + return false; + } + const int res( strncmp( m_buffer, name.c_str(), name.size() ) ); + + return ( 0 == res ); +} + +bool Text::operator == ( const Text &rhs ) const { + if( m_len != rhs.m_len ) { + return false; + } + + const int res( strncmp( m_buffer, rhs.m_buffer, m_len ) ); + + return ( 0 == res ); +} + +Identifier::Identifier( const char buffer[], size_t len ) +: m_text( buffer, len ) { + // empty +} + +Identifier::Identifier( const char buffer[] ) +: m_text( buffer, strlen( buffer ) ) { + // empty +} + +Identifier::~Identifier() { + // empty +} + +bool Identifier::operator == ( const Identifier &rhs ) const { + return m_text == rhs.m_text; +} + +Name::Name( NameType type, Identifier *id ) +: m_type( type ) +, m_id( id ) { + // empty +} + +Name::~Name() { + m_id = ddl_nullptr; +} + +Reference::Reference() +: m_numRefs( 0 ) +, m_referencedName( ddl_nullptr ) { + // empty +} + +Reference::Reference( size_t numrefs, Name **names ) +: m_numRefs( numrefs ) +, m_referencedName( ddl_nullptr ) { + m_referencedName = new Name *[ numrefs ]; + for( size_t i = 0; i < numrefs; i++ ) { + Name *name = new Name( names[ i ]->m_type, names[ i ]->m_id ); + m_referencedName[ i ] = name; + } +} + +Reference::~Reference() { + for( size_t i = 0; i < m_numRefs; i++ ) { + delete m_referencedName[ i ]; + } + m_numRefs = 0; + m_referencedName = ddl_nullptr; +} + +Property::Property( Identifier *id ) +: m_key( id ) +, m_value( ddl_nullptr ) +, m_ref( ddl_nullptr ) +, m_next( ddl_nullptr ) { + // empty +} + +Property::~Property() { + m_key = ddl_nullptr; + m_value = ddl_nullptr; + m_ref = ddl_nullptr;; + m_next = ddl_nullptr;; +} + +DataArrayList::DataArrayList() +: m_numItems( 0 ) +, m_dataList( ddl_nullptr ) +, m_next( ddl_nullptr ) { + // empty +} + +DataArrayList::~DataArrayList() { + // empty +} + +Context::Context() +: m_root( ddl_nullptr ) { + // empty +} + +Context::~Context() { + m_root = ddl_nullptr; +} + +void Context::clear() { + delete m_root; + m_root = ddl_nullptr; +} + +END_ODDLPARSER_NS diff --git a/contrib/openddlparser/code/OpenDDLExport.cpp b/contrib/openddlparser/code/OpenDDLExport.cpp new file mode 100644 index 000000000..5a010ea59 --- /dev/null +++ b/contrib/openddlparser/code/OpenDDLExport.cpp @@ -0,0 +1,116 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2014-2015 Kim Kulling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +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 +#include + +BEGIN_ODDLPARSER_NS + +struct DDLNodeIterator { + const DDLNode::DllNodeList &m_childs; + size_t m_idx; + DDLNodeIterator( const DDLNode::DllNodeList &childs ) + : m_childs( childs ) + , m_idx( 0 ) { + + } + + bool getNext( DDLNode **node ) { + if( m_childs.size() > (m_idx+1) ) { + m_idx++; + *node = m_childs[ m_idx ]; + return true; + } + + return false; + } +}; + +OpenDDLExport::OpenDDLExport() +:m_file( nullptr ) { + +} + +OpenDDLExport::~OpenDDLExport() { + if( nullptr != m_file ) { + ::fclose( m_file ); + m_file = nullptr; + } +} + +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 ) { + return true; + } + + return handleNode( root ); +} + +bool OpenDDLExport::handleNode( DDLNode *node ) { + if( ddl_nullptr == node ) { + return true; + } + + const DDLNode::DllNodeList &childs = node->getChildNodeList(); + if( childs.empty() ) { + return true; + } + DDLNode *current( ddl_nullptr ); + DDLNodeIterator it( childs ); + bool success( true ); + while( it.getNext( ¤t ) ) { + if( ddl_nullptr != current ) { + success |= writeNode( current ); + if( !handleNode( current ) ) { + success != false; + } + } + } + + return success; +} + +bool OpenDDLExport::writeNode( DDLNode *node ) { + bool success( true ); + if( node->hasProperties() ) { + success |= writeProperties( node ); + } + + return true; +} + +bool OpenDDLExport::writeProperties( DDLNode *node ) { + Property *prop( node->getProperties() ); + + return true; +} + +END_ODDLPARSER_NS diff --git a/contrib/openddlparser/include/openddlparser/OpenDDLExport.h b/contrib/openddlparser/include/openddlparser/OpenDDLExport.h new file mode 100644 index 000000000..269296527 --- /dev/null +++ b/contrib/openddlparser/include/openddlparser/OpenDDLExport.h @@ -0,0 +1,48 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2014-2015 Kim Kulling + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +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. +-----------------------------------------------------------------------------------------------*/ +#pragma once + +#include + +BEGIN_ODDLPARSER_NS + +/// +/// @ingroup OpenDDLParser +/// @brief This class represents the OpenDDLExporter. +/// +class DLL_ODDLPARSER_EXPORT OpenDDLExport { +public: + OpenDDLExport(); + ~OpenDDLExport(); + bool exportContext( Context *ctx, const std::string &filename ); + bool handleNode( DDLNode *node ); + +protected: + bool writeNode( DDLNode *node ); + bool writeProperties( DDLNode *node ); + +private: + FILE *m_file; +}; + +END_ODDLPARSER_NS