From 80498f4f79d6e004d511bbb7a0fe55c518065e71 Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 11 Oct 2013 12:00:06 -0700 Subject: [PATCH] cmData.h/c: cmDataPrepend/Insert/AppendChild() now automatically unlinks the new child prior to relinking it to it's new parent. Fixed bug in cmDataMakePair() where the key and value were never actually linked in. --- cmData.c | 13 +++++++++++++ cmData.h | 20 ++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/cmData.c b/cmData.c index 45c0dee..4bf8b5b 100644 --- a/cmData.c +++ b/cmData.c @@ -1284,8 +1284,12 @@ cmData_t* cmDataPrependChild(cmData_t* parent, cmData_t* p ) { assert( cmDataIsStruct(p) ); + + cmDataUnlink(p); + p->u.child = parent->u.child; parent->u.child = p; + p->parent = parent; return p; } @@ -1294,6 +1298,8 @@ cmData_t* cmDataAppendChild( cmData_t* parent, cmData_t* p ) assert( cmDataIsStruct(parent) ); assert( parent->tid != kRecordDtId || (parent->tid == kRecordDtId && p->tid==kPairDtId)); + cmDataUnlink(p); + cmData_t* cp = parent->u.child; if( cp == NULL ) parent->u.child = p; @@ -1307,6 +1313,7 @@ cmData_t* cmDataAppendChild( cmData_t* parent, cmData_t* p ) } } + p->parent = parent; p->sibling = NULL; return p; } @@ -1316,6 +1323,8 @@ cmData_t* cmDataInsertChild( cmData_t* parent, unsigned index, cmData_t* p ) if( !cmDataIsStruct(parent) ) return NULL; + cmDataUnlink(p); + unsigned n = 0; cmData_t* cp = parent->u.child; cmData_t* pp = NULL; @@ -1339,6 +1348,8 @@ cmData_t* cmDataInsertChild( cmData_t* parent, unsigned index, cmData_t* p ) ++n; } + p->parent = parent; + return p; } @@ -1422,6 +1433,8 @@ cmData_t* cmDataMakePair( cmData_t* parent, cmData_t* p, cmData_t* key, cmData_t p->parent = parent; p->flags = 0; p->u.child = NULL; + cmDataAppendChild(p,key); + cmDataAppendChild(p,value); return p; } diff --git a/cmData.h b/cmData.h index f3d6d0b..01f9344 100644 --- a/cmData.h +++ b/cmData.h @@ -125,7 +125,7 @@ extern "C" { bool cmDataIsValue( const cmData_t* p ); bool cmDataIsPtr( const cmData_t* p ); - bool cmDataIsStruct( const cmData_t* p ); + bool cmDataIsStruct( const cmData_t* p ); // is a pair,list or record /* TODO: @@ -367,17 +367,20 @@ extern "C" { // Prepend 'p' to 'parents' child list. // The source node 'p' is not duplicated it is simply linked in. + // 'p' is automatically unlinked prior to being prepended. // Returns 'p'. cmData_t* cmDataPrependChild(cmData_t* parent, cmData_t* p ); // Append 'p' to the end of 'parent' child list. // The source node 'p' is not duplicated it is simply linked in. - // Returns 'p'. + // 'p' is automatically unlinked prior to being appended. + // Returns 'p'. cmData_t* cmDataAppendChild( cmData_t* parent, cmData_t* p ); // Insert 'p' at index. Index must be in the range: // 0 to cmDataChildCount(parent). // The source node 'p' is not duplicated it is simply linked in. + // 'p' is automatically unlinked prior to being inserted. // Returns 'p'. cmData_t* cmDataInsertChild( cmData_t* parent, unsigned index, cmData_t* p ); @@ -408,12 +411,18 @@ extern "C" { // The data space for the 'label' string is dynamically allocated. cmData_t* cmDataPairSetKeyLabel( cmData_t* p, const cmChar_t* label ); + // Create a pair value by assigning a key and value to 'p'. + // 'p' is unlinked and freed prior to the key value assignment. + // 'key' and 'value' are simply linked in they are not duplicated or reallocated. cmData_t* cmDataMakePair( cmData_t* parent, cmData_t* p, cmData_t* key, cmData_t* value ); - // Dynamically allocate a pair node + // Dynamically allocate a pair node. Both the key and value nodes are reallocated. cmData_t* cmDataAllocPair( cmData_t* parent, const cmData_t* key, const cmData_t* value ); + + // Dynamically allocate the id but link (w/o realloc) the value. cmData_t* cmDataAllocPairId( cmData_t* parent, unsigned keyId, cmData_t* value ); - // The data space for the 'label' string is dynamically allocated. + + // Dynamically allocate the label but link (w/o realloc) the value. cmData_t* cmDataAllocPairLabel( cmData_t* parent, const cmChar_t* label, cmData_t* value ); //---------------------------------------------------------------------------- @@ -476,6 +485,9 @@ extern "C" { cmData_t* cmRecdMake( cmData_t* parent, cmData_t* p ); cmData_t* cmRecdAlloc( cmData_t* parent ); + // Append a pair node by linking the pair node 'pair' to the record node 'p'. + // 'pair' is simply linked to 'p' via cmDataAppendChild() no + // reallocation or duplicattion takes place. cmData_t* cmRecdAppendPair( cmData_t* p, cmData_t* pair );