Kim Kulling 2017-06-19 21:07:18 +02:00
parent d3140a68ab
commit 51b915c97b
9 changed files with 176 additions and 106 deletions

View File

@ -68,8 +68,8 @@ DDLNode::DDLNode( const std::string &type, const std::string &name, size_t idx,
}
DDLNode::~DDLNode() {
releaseDataType<Property>( m_properties );
releaseDataType<Value>( m_value );
delete m_properties;
delete m_value;
releaseReferencedNames( m_references );
delete m_dtArrayList;
@ -77,6 +77,9 @@ DDLNode::~DDLNode() {
if( s_allocatedNodes[ m_idx ] == this ) {
s_allocatedNodes[ m_idx ] = ddl_nullptr;
}
for ( size_t i = 0; i<m_children.size(); i++ ){
delete m_children[ i ];
}
}
void DDLNode::attachParent( DDLNode *parent ) {
@ -91,9 +94,8 @@ void DDLNode::attachParent( DDLNode *parent ) {
}
void DDLNode::detachParent() {
if( m_parent ) {
std::vector<DDLNode*>::iterator it;
it = std::find( m_parent->m_children.begin(), m_parent->m_children.end(), this );
if( ddl_nullptr != m_parent ) {
DDLNodeIt it = std::find( m_parent->m_children.begin(), m_parent->m_children.end(), this );
if( m_parent->m_children.end() != it ) {
m_parent->m_children.erase( it );
}
@ -126,6 +128,8 @@ const std::string &DDLNode::getName() const {
}
void DDLNode::setProperties( Property *prop ) {
if(m_properties!=ddl_nullptr)
delete m_properties;
m_properties = prop;
}
@ -197,7 +201,7 @@ DDLNode *DDLNode::create( const std::string &type, const std::string &name, DDLN
void DDLNode::releaseNodes() {
if( s_allocatedNodes.size() > 0 ) {
for( DllNodeList::iterator it = s_allocatedNodes.begin(); it != s_allocatedNodes.end(); it++ ) {
for( DDLNodeIt it = s_allocatedNodes.begin(); it != s_allocatedNodes.end(); it++ ) {
if( *it ) {
delete *it;
}

View File

@ -22,6 +22,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-----------------------------------------------------------------------------------------------*/
#include <openddlparser/OpenDDLCommon.h>
#include <openddlparser/DDLNode.h>
#include <openddlparser/Value.h>
BEGIN_ODDLPARSER_NS
@ -84,7 +85,14 @@ Name::~Name() {
m_id = ddl_nullptr;
}
Reference::Reference()
Name::Name( const Name &name ){
m_type=name.m_type;
m_id=new Text(name.m_id->m_buffer,name.m_id->m_len);
}
Reference::Reference()
: m_numRefs( 0 )
, m_referencedName( ddl_nullptr ) {
// empty
@ -96,8 +104,16 @@ Reference::Reference( size_t numrefs, Name **names )
if ( numrefs > 0 ) {
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;
m_referencedName[ i ] = names[i];
}
}
}
Reference::Reference(const Reference &ref) {
m_numRefs=ref.m_numRefs;
if(m_numRefs!=0){
m_referencedName = new Name*[m_numRefs];
for ( size_t i = 0; i < m_numRefs; i++ ) {
m_referencedName[i] = new Name(*ref.m_referencedName[i]);
}
}
}
@ -107,6 +123,7 @@ Reference::~Reference() {
delete m_referencedName[ i ];
}
m_numRefs = 0;
delete [] m_referencedName;
m_referencedName = ddl_nullptr;
}
@ -135,21 +152,30 @@ Property::Property( Text *id )
}
Property::~Property() {
m_key = ddl_nullptr;
m_value = ddl_nullptr;
m_ref = ddl_nullptr;;
m_next = ddl_nullptr;;
delete m_key;
if(m_value!=ddl_nullptr)
delete m_value;
if(m_ref!=ddl_nullptr)
delete(m_ref);
if(m_next!=ddl_nullptr)
delete m_next;
}
DataArrayList::DataArrayList()
: m_numItems( 0 )
, m_dataList( ddl_nullptr )
, m_next( ddl_nullptr ) {
, m_next( ddl_nullptr )
, m_refs(ddl_nullptr)
, m_numRefs(0){
// empty
}
DataArrayList::~DataArrayList() {
// empty
delete m_dataList;
if(m_next!=ddl_nullptr)
delete m_next;
if(m_refs!=ddl_nullptr)
delete m_refs;
}
size_t DataArrayList::size() {

View File

@ -280,7 +280,7 @@ bool OpenDDLExport::writeValueType( Value::ValueType type, size_t numItems, std:
statement += "[";
char buffer[ 256 ];
::memset( buffer, '\0', 256 * sizeof( char ) );
sprintf( buffer, "%d", int(numItems) );
sprintf( buffer, "%d", static_cast<int>( numItems ) );
statement += buffer;
statement += "]";
}

View File

@ -36,7 +36,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
BEGIN_ODDLPARSER_NS
static const char *Version = "0.3.0";
static const char *Version = "0.4.0";
namespace Grammar {
static const char *OpenBracketToken = "{";
@ -49,7 +49,7 @@ namespace Grammar {
static const char *BoolFalse = "false";
static const char *CommaSeparator = ",";
static const char* PrimitiveTypeToken[ Value::ddl_types_max ] = {
static const char *PrimitiveTypeToken[ Value::ddl_types_max ] = {
"bool",
"int8",
"int16",
@ -74,8 +74,8 @@ const char *getTypeToken( Value::ValueType type ) {
static void logInvalidTokenError( char *in, const std::string &exp, OpenDDLParser::logCallback callback ) {
std::stringstream stream;
stream << "Invalid token \"" << *in << "\"" << " expected \"" << exp << "\"" << std::endl;
std::string full(in);
std::string part(full.substr(0,50));
std::string full( in );
std::string part( full.substr( 0, 50 ) );
stream << part;
callback( ddl_error_msg, stream.str() );
}
@ -85,6 +85,7 @@ static bool isIntegerType( Value::ValueType integerType ) {
integerType != Value::ddl_int32 && integerType != Value::ddl_int64 ) {
return false;
}
return true;
}
@ -193,10 +194,11 @@ size_t OpenDDLParser::getBufferSize() const {
void OpenDDLParser::clear() {
m_buffer.resize( 0 );
if( ddl_nullptr != m_context ) {
m_context->m_root = ddl_nullptr;
delete m_context;
m_context=ddl_nullptr;
}
DDLNode::releaseNodes();
// DDLNode::releaseNodes();
}
bool OpenDDLParser::parse() {
@ -212,11 +214,11 @@ bool OpenDDLParser::parse() {
// do the main parsing
char *current( &m_buffer[ 0 ] );
char *end( &m_buffer[ m_buffer.size() - 1 ] + 1 );
char *end( &m_buffer[m_buffer.size() - 1 ] + 1 );
size_t pos( current - &m_buffer[ 0 ] );
while( pos < m_buffer.size() ) {
current = parseNextNode( current, end );
if(current==ddl_nullptr) {
if ( current == ddl_nullptr ) {
return false;
}
pos = current - &m_buffer[ 0 ];
@ -271,14 +273,17 @@ char *OpenDDLParser::parseHeader( char *in, char *end ) {
} else {
std::cerr << "nullptr returned by creating DDLNode." << std::endl;
}
delete id;
Name *name(ddl_nullptr);
in = OpenDDLParser::parseName(in, end, &name);
if( ddl_nullptr != name && ddl_nullptr != node ) {
const std::string nodeName( name->m_id->m_buffer );
node->setName( nodeName );
delete name;
}
Property *first(ddl_nullptr);
in = lookForNextToken(in, end);
if (*in == Grammar::OpenPropertyToken[0]) {
@ -303,7 +308,7 @@ char *OpenDDLParser::parseHeader( char *in, char *end ) {
prev = prop;
}
}
in++;
++in;
}
// set the properties
@ -330,9 +335,9 @@ char *OpenDDLParser::parseStructure( char *in, char *end ) {
return ddl_nullptr;
}
} while ( *in != '}' );
in++;
++in;
} else {
in++;
++in;
logInvalidTokenError( in, std::string( Grammar::OpenBracketToken ), m_logCallback );
error = true;
return ddl_nullptr;
@ -373,7 +378,7 @@ static void setNodeDataArrayList( DDLNode *currentNode, DataArrayList *dtArrayLi
char *OpenDDLParser::parseStructureBody( char *in, char *end, bool &error ) {
if( !isNumeric( *in ) && !isCharacter( *in ) ) {
in++;
++in;
}
in = lookForNextToken( in, end );
@ -431,7 +436,6 @@ DDLNode *OpenDDLParser::popNode() {
DDLNode *topNode( top() );
m_stack.pop_back();
return topNode;
}
@ -467,7 +471,14 @@ void OpenDDLParser::normalizeBuffer( std::vector<char> &buffer) {
for( size_t readIdx = 0; readIdx<len; ++readIdx ) {
char *c( &buffer[readIdx] );
// check for a comment
if( !isComment<char>( c, end ) && !isNewLine( *c ) ) {
if (isCommentOpenTag(c, end)) {
++readIdx;
while (!isCommentCloseTag(&buffer[readIdx], end)) {
++readIdx;
}
++readIdx;
++readIdx;
} else if( !isComment<char>( c, end ) && !isNewLine( *c ) ) {
newBuffer.push_back( buffer[ readIdx ] );
} else {
if( isComment<char>( c, end ) ) {
@ -535,8 +546,7 @@ char *OpenDDLParser::parseIdentifier( char *in, char *end, Text **id ) {
}
const size_t len( idLen );
Text *newId = new Text( start, len );
*id = newId;
*id = new Text( start, len );
return in;
}
@ -567,14 +577,14 @@ char *OpenDDLParser::parsePrimitiveDataType( char *in, char *end, Value::ValueTy
bool ok( true );
if( *in == Grammar::OpenArrayToken[ 0 ] ) {
ok = false;
in++;
++in;
char *start( in );
while ( in != end ) {
in++;
++in;
if( *in == Grammar::CloseArrayToken[ 0 ] ) {
len = ::atoi( start );
ok = true;
in++;
++in;
break;
}
}
@ -623,10 +633,10 @@ char *OpenDDLParser::parseBooleanLiteral( char *in, char *end, Value **boolean )
char *start( in );
size_t len( 0 );
while( !isSeparator( *in ) && in != end ) {
in++;
len++;
++in;
++len;
}
len++;
++len;
int res = ::strncmp( Grammar::BoolTrue, start, strlen( Grammar::BoolTrue ) );
if( 0 != res ) {
res = ::strncmp( Grammar::BoolFalse, start, strlen( Grammar::BoolFalse ) );
@ -657,7 +667,7 @@ char *OpenDDLParser::parseIntegerLiteral( char *in, char *end, Value **integer,
in = lookForNextToken( in, end );
char *start( in );
while( !isSeparator( *in ) && in != end ) {
in++;
++in;
}
if( isNumeric( *start ) ) {
@ -711,7 +721,7 @@ char *OpenDDLParser::parseFloatingLiteral( char *in, char *end, Value **floating
in = lookForNextToken( in, end );
char *start( in );
while( !isSeparator( *in ) && in != end ) {
in++;
++in;
}
// parse the float value
@ -732,8 +742,7 @@ char *OpenDDLParser::parseFloatingLiteral( char *in, char *end, Value **floating
}
if( ok ) {
if(floatType == Value::ddl_double)
{
if ( floatType == Value::ddl_double ) {
const double value( atof( start ) );
*floating = ValueAllocator::allocPrimData( Value::ddl_double );
( *floating )->setDouble( value );
@ -757,17 +766,17 @@ char *OpenDDLParser::parseStringLiteral( char *in, char *end, Value **stringData
size_t len( 0 );
char *start( in );
if( *start == '\"' ) {
start++;
in++;
++start;
++in;
while( *in != '\"' && in != end ) {
in++;
len++;
++in;
++len;
}
*stringData = ValueAllocator::allocPrimData( Value::ddl_string, len );
::strncpy( ( char* ) ( *stringData )->m_data, start, len );
( *stringData )->m_data[len] = '\0';
in++;
++in;
}
return in;
@ -791,12 +800,12 @@ char *OpenDDLParser::parseHexaLiteral( char *in, char *end, Value **data ) {
return in;
}
in++;
++in;
if( *in != 'x' && *in != 'X' ) {
return in;
}
in++;
++in;
bool ok( true );
char *start( in );
int pos( 0 );
@ -805,8 +814,8 @@ char *OpenDDLParser::parseHexaLiteral( char *in, char *end, Value **data ) {
ok = false;
break;
}
pos++;
in++;
++pos;
++in;
}
if( !ok ) {
@ -816,9 +825,9 @@ char *OpenDDLParser::parseHexaLiteral( char *in, char *end, Value **data ) {
int value( 0 );
while( pos > 0 ) {
int v = hex2Decimal( *start );
pos--;
--pos;
value = ( value << 4 ) | v;
start++;
++start;
}
*data = ValueAllocator::allocPrimData( Value::ddl_unsigned_int64 );
@ -841,7 +850,7 @@ char *OpenDDLParser::parseProperty( char *in, char *end, Property **prop ) {
if( ddl_nullptr != id ) {
in = lookForNextToken( in, end );
if( *in == '=' ) {
in++;
++in;
in = getNextToken( in, end );
Value *primData( ddl_nullptr );
if( isInteger( in, end ) ) {
@ -862,6 +871,8 @@ char *OpenDDLParser::parseProperty( char *in, char *end, Property **prop ) {
( *prop )->m_ref = ref;
}
}
} else {
delete id;
}
}
@ -878,7 +889,7 @@ char *OpenDDLParser::parseDataList( char *in, char *end, Value::ValueType type,
in = lookForNextToken( in, end );
if( *in == '{' ) {
in++;
++in;
Value *current( ddl_nullptr ), *prev( ddl_nullptr );
while( '}' != *in ) {
current = ddl_nullptr;
@ -934,7 +945,7 @@ char *OpenDDLParser::parseDataList( char *in, char *end, Value::ValueType type,
prev->setNext( current );
prev = current;
}
numValues++;
++numValues;
}
in = getNextSeparator( in, end );
@ -942,7 +953,7 @@ char *OpenDDLParser::parseDataList( char *in, char *end, Value::ValueType type,
break;
}
}
in++;
++in;
}
return in;
@ -972,7 +983,7 @@ char *OpenDDLParser::parseDataArrayList( char *in, char *end,Value::ValueType ty
in = lookForNextToken( in, end );
if( *in == Grammar::OpenBracketToken[ 0 ] ) {
in++;
++in;
Value *currentValue( ddl_nullptr );
Reference *refs( ddl_nullptr );
DataArrayList *prev( ddl_nullptr ), *currentDataList( ddl_nullptr );
@ -995,7 +1006,7 @@ char *OpenDDLParser::parseDataArrayList( char *in, char *end,Value::ValueType ty
}
} while( Grammar::CommaSeparator[ 0 ] == *in && in != end );
in = lookForNextToken( in, end );
in++;
++in;
}
return in;

View File

@ -110,7 +110,17 @@ Value::Value( ValueType type )
}
Value::~Value() {
// empty
if(m_data!=ddl_nullptr) {
if (m_type == ddl_ref ) {
Reference *tmp = (Reference *) m_data;
if (tmp != ddl_nullptr)
delete tmp;
}else
delete[] m_data;
}
if(m_next!=ddl_nullptr)
delete m_next;
}
void Value::setBool( bool value ) {
@ -273,13 +283,7 @@ void Value::setRef( Reference *ref ) {
delete [] m_data;
}
m_data = new unsigned char[ sizeof( Reference ) ];
Reference *myRef = ( Reference * ) m_data;
myRef->m_numRefs = ref->m_numRefs;
myRef->m_referencedName = new Name *[ myRef->m_numRefs ];
for ( size_t i = 0; i < myRef->m_numRefs; i++ ) {
myRef->m_referencedName[ i ] = new Name( ref->m_referencedName[ i ]->m_type, ref->m_referencedName[ i ]->m_id );
}
m_data = (unsigned char*) new Reference(*ref);
}
}
}
@ -366,7 +370,6 @@ Value *ValueAllocator::allocPrimData( Value::ValueType type, size_t len ) {
}
Value *data = new Value( type );
data->m_type = type;
switch( type ) {
case Value::ddl_bool:
data->m_size = sizeof( bool );
@ -405,10 +408,10 @@ Value *ValueAllocator::allocPrimData( Value::ValueType type, size_t len ) {
data->m_size = sizeof( double );
break;
case Value::ddl_string:
data->m_size = sizeof( char );
data->m_size = sizeof( char )*(len+1);
break;
case Value::ddl_ref:
data->m_size = sizeof( char );
data->m_size = 0;
break;
case Value::ddl_none:
case Value::ddl_types_max:
@ -417,12 +420,8 @@ Value *ValueAllocator::allocPrimData( Value::ValueType type, size_t len ) {
}
if( data->m_size ) {
size_t len1( len );
if( Value::ddl_string == type ) {
len1++;
}
data->m_size *= len1;
data->m_data = new unsigned char[ data->m_size ];
::memset(data->m_data,0,data->m_size);
}
return data;

View File

@ -53,6 +53,9 @@ public:
/// @brief The child-node-list type.
typedef std::vector<DDLNode*> DllNodeList;
/// @brief The child-node-list iterator.
typedef std::vector<DDLNode*>::iterator DDLNodeIt;
public:
/// @brief The class destructor.
~DDLNode();

View File

@ -148,12 +148,12 @@ struct DLL_ODDLPARSER_EXPORT Name {
/// @param type [in] The name type.
/// @param id [in] The id.
Name( NameType type, Text *id );
Name( const Name &name );
/// @brief The destructor.
~Name();
private:
Name( const Name & ) ddl_no_copy;
Name &operator = ( const Name& ) ddl_no_copy;
};
@ -164,7 +164,7 @@ struct DLL_ODDLPARSER_EXPORT Reference {
/// @brief The default constructor.
Reference();
Reference( const Reference &ref );
/// @brief The constructor with an array of ref names.
/// @param numrefs [in] The number of ref names.
/// @param names [in] The ref names.
@ -178,7 +178,6 @@ struct DLL_ODDLPARSER_EXPORT Reference {
size_t sizeInBytes();
private:
Reference( const Reference & ) ddl_no_copy;
Reference &operator = ( const Reference & ) ddl_no_copy;
};

View File

@ -47,7 +47,7 @@ struct Property;
template<class T>
inline
T *lookForNextToken( T *in, T *end ) {
while( ( isSpace( *in ) || isNewLine( *in ) || ',' == *in ) && ( in != end ) ) {
while( ( in != end ) && ( isSpace( *in ) || isNewLine( *in ) || ',' == *in ) ) {
in++;
}
return in;

View File

@ -84,7 +84,7 @@ static const unsigned char chartype_table[ 256 ] = {
template<class T>
inline
bool isNumeric( const T in ) {
return ( chartype_table[ static_cast<int>( in ) ] == 1 );
return ( chartype_table[ static_cast<size_t>( in ) ] == 1 );
}
template<class T>
@ -98,7 +98,7 @@ inline
bool isInteger( T *in, T *end ) {
if( in != end ) {
if( *in == '-' ) {
in++;
++in;
}
}
@ -108,7 +108,7 @@ bool isInteger( T *in, T *end ) {
if( !result ) {
break;
}
in++;
++in;
}
return result;
@ -119,7 +119,7 @@ inline
bool isFloat( T *in, T *end ) {
if( in != end ) {
if( *in == '-' ) {
in++;
++in;
}
}
@ -134,12 +134,12 @@ bool isFloat( T *in, T *end ) {
if( !result ) {
return false;
}
in++;
++in;
}
// check for 1<.>0f
if( *in == '.' ) {
in++;
++in;
} else {
return false;
}
@ -150,7 +150,7 @@ bool isFloat( T *in, T *end ) {
if( !result ) {
return false;
}
in++;
++in;
}
return result;
@ -208,7 +208,7 @@ template<class T>
inline
static T *getNextSeparator( T *in, T *end ) {
while( !isSeparator( *in ) || in == end ) {
in++;
++in;
}
return in;
}
@ -250,5 +250,33 @@ bool isComment( T *in, T *end ) {
return false;
}
template<class T>
inline
bool isCommentOpenTag(T *in, T *end ) {
if (*in == '/') {
if (in + 1 != end) {
if (*(in + 1) == '*') {
return true;
}
}
}
return false;
}
template<class T>
inline
bool isCommentCloseTag(T *in, T *end) {
if (*in == '*') {
if (in + 1 != end) {
if (*(in + 1) == '/') {
return true;
}
}
}
return false;
}
END_ODDLPARSER_NS