Changeset 13476

Show
Ignore:
Timestamp:
01/06/08 07:09:19 (4 months ago)
Author:
rouault
Message:

Various fixes in CPLList implementation (CPLListInsert and CPLListRemove) : ticket #2134

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gdal/port/cpl_list.cpp

    r10645 r13476  
    9999    nCount = CPLListCount( psList ); 
    100100 
    101     /* Allocate room for the new object */ 
    102     if ( nCount < nPosition ) 
    103     { 
    104         for ( i = nCount; i < nPosition - 1; i++ ) 
    105             CPLListAppend( psList, NULL ); 
    106         CPLListAppend( psList, pData ); 
     101    if ( nPosition == 0) 
     102    { 
     103        CPLList *psNew = (CPLList *)CPLMalloc( sizeof(CPLList) ); 
     104        psNew->pData = pData; 
     105        psNew->psNext = psList; 
     106        psList = psNew; 
     107    } 
     108    else if ( nCount < nPosition ) 
     109    { 
     110        /* Allocate room for the new object */ 
     111        CPLList* psLast = CPLListGetLast(psList); 
     112        for ( i = nCount; i <= nPosition - 1; i++ ) 
     113        { 
     114            psLast = CPLListAppend( psLast, NULL ); 
     115            if (psList == NULL) 
     116                psList = psLast; 
     117            else 
     118                psLast = psLast->psNext; 
     119        } 
     120        psLast = CPLListAppend( psLast, pData ); 
     121        if (psList == NULL) 
     122            psList = psLast; 
    107123    } 
    108124    else 
     
    163179    CPLList *psCurrent = psList; 
    164180 
    165     if ( psList == NULL
     181    if ( nPosition < 0
    166182        return NULL; 
    167183 
     
    206222 
    207223/** 
    208  * Remone the element from the specified position (zero based) in a list. Data 
     224 * Remove the element from the specified position (zero based) in a list. Data 
    209225 * object contained in removed element must be freed by the caller first. 
    210226 *  
     
    218234{ 
    219235    CPLList *psCurrent, *psRemoved; 
    220     int     i, nCount; 
    221      
    222     nCount = CPLListCount( psList ); 
    223  
    224     if ( nPosition < 0 || nCount < nPosition ) 
     236    int     i; 
     237 
     238    if ( psList == NULL) 
     239    { 
     240        return NULL; 
     241    } 
     242    else if ( nPosition < 0) 
     243    { 
    225244        return psList;      /* Nothing to do!*/ 
    226  
    227     psCurrent = psList; 
    228     for ( i = 0; i < nPosition - 1; i++ ) 
    229         psCurrent = psCurrent->psNext; 
    230     psRemoved = psCurrent->psNext; 
    231     psCurrent->psNext = psRemoved->psNext; 
    232     CPLFree( psRemoved ); 
     245    } 
     246    else if ( nPosition == 0 ) 
     247    { 
     248        psCurrent = psList->psNext; 
     249        CPLFree( psList ); 
     250        psList = psCurrent; 
     251    } 
     252    else 
     253    { 
     254        psCurrent = psList; 
     255        for ( i = 0; i < nPosition - 1; i++ ) 
     256        { 
     257            psCurrent = psCurrent->psNext; 
     258            /* psCurrent == NULL if nPosition >= CPLListCount(psList) */ 
     259            if (psCurrent == NULL) 
     260                return psList; 
     261        } 
     262        psRemoved = psCurrent->psNext; 
     263        /* psRemoved == NULL if nPosition >= CPLListCount(psList) */ 
     264        if (psRemoved == NULL) 
     265            return psList; 
     266        psCurrent->psNext = psRemoved->psNext; 
     267        CPLFree( psRemoved ); 
     268    } 
    233269 
    234270    return psList; 
     
    294330void *CPLListGetData( CPLList *psElement ) 
    295331{ 
    296     if ( psElement == NULL || psElement->pData == NULL
     332    if ( psElement == NULL
    297333        return NULL; 
    298334    else