| 1 | #include <config.h> |
|---|---|
| 2 | #include "lst.h" |
| 3 | |
| 4 | |
| 5 | int lstInsert( HLST hLst, void *pData ) |
| 6 | { |
| 7 | HLSTITEM hItem; |
| 8 | |
| 9 | if ( !hLst ) |
| 10 | return LST_ERROR; |
| 11 | |
| 12 | if ( !hLst->hCurrent ) |
| 13 | return lstAppend( hLst, pData ); |
| 14 | |
| 15 | /********************** |
| 16 | * CREATE AN ITEM |
| 17 | **********************/ |
| 18 | hItem = malloc( sizeof(LSTITEM) ); |
| 19 | if ( !hItem ) |
| 20 | return LST_ERROR; |
| 21 | |
| 22 | hItem->bDelete = false; |
| 23 | hItem->bHide = false; |
| 24 | hItem->hLst = hLst; |
| 25 | hItem->nRefs = 0; |
| 26 | hItem->pData = NULL; |
| 27 | hItem->pNext = NULL; |
| 28 | hItem->pPrev = NULL; |
| 29 | |
| 30 | if ( hLst->hLstBase ) |
| 31 | { |
| 32 | /********************** |
| 33 | * WE ARE A CURSOR LIST SO... |
| 34 | * 1. ADD TO BASE LIST |
| 35 | * 2. inc BASE LIST ITEM REF COUNT |
| 36 | * 3. ADD TO THIS LIST (ref to base list) |
| 37 | **********************/ |
| 38 | lstInsert( hLst->hLstBase, pData ); /* !!! INSERT POS IN BASE LIST IS UNPREDICTABLE !!! */ |
| 39 | /* BECAUSE hCurrent MAY HAVE CHANGED AND WE */ |
| 40 | /* ARE NOT TRYING TO PUT IT BACK */ |
| 41 | hItem->pData = hLst->hLstBase->hCurrent; |
| 42 | hLst->hLstBase->hCurrent->nRefs++; |
| 43 | _lstInsert( hLst, hItem ); |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | /********************** |
| 48 | * WE ARE THE ROOT SO... |
| 49 | * 1. ADD TO THIS LIST |
| 50 | **********************/ |
| 51 | hItem->pData = pData; |
| 52 | _lstInsert( hLst, hItem ); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | return LST_SUCCESS; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | |
| 61 | /************************* |
| 62 | * SIMPLY CONNECTS THE LINKS/POINTERS AND SETS CURRENT |
| 63 | *************************/ |
| 64 | int _lstInsert( HLST hLst, HLSTITEM hItem ) |
| 65 | { |
| 66 | if ( !hLst->hCurrent ) |
| 67 | return _lstAppend( hLst, hItem ); |
| 68 | |
| 69 | |
| 70 | hItem->pPrev = hLst->hCurrent->pPrev; |
| 71 | hItem->pNext = hLst->hCurrent; |
| 72 | |
| 73 | if ( hLst->hCurrent->pPrev ) |
| 74 | hLst->hCurrent->pPrev->pNext = hItem; |
| 75 | |
| 76 | hLst->hCurrent->pPrev = hItem; |
| 77 | |
| 78 | if ( hLst->hCurrent == hLst->hFirst ) |
| 79 | hLst->hFirst = hItem; |
| 80 | |
| 81 | hLst->hCurrent = hItem; |
| 82 | hLst->nItems++; |
| 83 | |
| 84 | return LST_SUCCESS; |
| 85 | } |
| 86 | |
| 87 |