1 /*-------------------------------------------------------------------*/ 2 /* List Functionality */ 3 /*-------------------------------------------------------------------*/ 4 /* #define SH_LIST_DEBUG */ 5 /*-------------------------------------------------------------------*/ 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include "shlist.h" 9 /*-------------------------------------------------------------------*/ 10 void shListInitList( SHLIST *listPtr ) 11 { 12 listPtr->data = (void *)0L; 13 listPtr->next = listPtr; 14 listPtr->prev = listPtr; 15 } 16 17 SHLIST *shListFindItem( SHLIST *head, void *val, shListEqual func ) 18 { 19 SHLIST *item; 20 21 for(item=head->next;( item != head );item=item->next) 22 if( func ) { 23 if( func( val, item->data ) ) { 24 return( item ); 25 } 26 } 27 else { 28 if( item->data == val ) { 29 return( item ); 30 } 31 } 32 return( NULL ); 33 } 34 35 SHLIST *shListGetLastItem( SHLIST *head ) 36 { 37 if( head->prev != head ) 38 return( head->prev ); 39 return( NULL ); 40 } 41 42 SHLIST *shListGetFirstItem( SHLIST *head ) 43 { 44 if( head->next != head ) 45 return( head->next ); 46 return( NULL ); 47 } 48 49 SHLIST *shListGetNItem( SHLIST *head, unsigned long num ) 50 { 51 SHLIST *item; 52 unsigned long i; 53 54 for(i=0,item=head->next;( (i < num) && (item != head) );i++,item=item->next); 55 if( item != head ) 56 return( item ); 57 return( NULL ); 58 } 59 60 SHLIST *shListGetNextItem( SHLIST *head, SHLIST *item ) 61 { 62 if( item == NULL ) 63 return( NULL ); 64 if( item->next != head ) 65 return( item->next ); 66 return( NULL ); 67 } 68 69 SHLIST *shListGetPrevItem( SHLIST *head, SHLIST *item ) 70 { 71 if( item == NULL ) 72 return( NULL ); 73 if( item->prev != head ) 74 return( item->prev ); 75 return( NULL ); 76 } 77 78 void shListDelItem( SHLIST *head, SHLIST *item, shListFree func ) 79 { 80 if( item == NULL ) 81 return; 82 #ifdef SH_LIST_DEBUG 83 fprintf(stderr, "Del %lx\n", (unsigned long)(item->data)); 84 #endif 85 (item->prev)->next = item->next; 86 (item->next)->prev = item->prev; 87 if( func && item->data ) { 88 func( (void *)(item->data) ); 89 } 90 free( item ); 91 head->data = (void *)((unsigned long)(head->data) - 1); 92 } 93 94 void shListInsFirstItem( SHLIST *head, void *val ) 95 { /* Insert to the beginning of the list */ 96 SHLIST *item; 97 98 item = (SHLIST *)malloc( sizeof(SHLIST) ); 99 if( item == NULL ) 100 return; 101 item->data = val; 102 item->next = head->next; 103 item->prev = head; 104 (head->next)->prev = item; 105 head->next = item; 106 #ifdef SH_LIST_DEBUG 107 fprintf(stderr, "Ins First %lx\n", (unsigned long)(item->data)); 108 #endif 109 head->data = (void *)((unsigned long)(head->data) + 1); 110 } 111 112 void shListInsLastItem( SHLIST *head, void *val ) 113 { /* Insert to the end of the list */ 114 SHLIST *item; 115 116 item = (SHLIST *)malloc( sizeof(SHLIST) ); 117 if( item == NULL ) 118 return; 119 item->data = val; 120 item->next = head; 121 item->prev = head->prev; 122 (head->prev)->next = item; 123 head->prev = item; 124 #ifdef SH_LIST_DEBUG 125 fprintf(stderr, "Ins Last %lx\n", (unsigned long)(item->data)); 126 #endif 127 head->data = (void *)((unsigned long)(head->data) + 1); 128 } 129 130 void shListInsBeforeItem( SHLIST *head, void *val, void *etal, 131 shListCmp func ) 132 { 133 SHLIST *item, *iptr; 134 135 if( func == NULL ) 136 shListInsFirstItem( head, val ); 137 else { 138 item = (SHLIST *)malloc( sizeof(SHLIST) ); 139 if( item == NULL ) 140 return; 141 item->data = val; 142 for(iptr=head->next;( iptr != head );iptr=iptr->next) 143 if( func( val, iptr->data, etal ) ) 144 break; 145 item->next = iptr; 146 item->prev = iptr->prev; 147 (iptr->prev)->next = item; 148 iptr->prev = item; 149 #ifdef SH_LIST_DEBUG 150 fprintf(stderr, "Ins Before %lx\n", (unsigned long)(item->data)); 151 #endif 152 head->data = (void *)((unsigned long)(head->data) + 1); 153 } 154 } 155 156 void shListDelAllItems( SHLIST *head, shListFree func ) 157 { 158 SHLIST *item; 159 160 for(item=head->next;( item != head );) { 161 shListDelItem( head, item, func ); 162 item = head->next; 163 } 164 head->data = (void *)0L; 165 } 166 167 void shListPrintAllItems( SHLIST *head, shListPrint func ) 168 { 169 #ifdef SH_LIST_DEBUG 170 SHLIST *item; 171 172 for(item=head->next;( item != head );item=item->next) 173 if( func ) { 174 func(item->data); 175 } 176 else { 177 fprintf(stderr, "Item: %lx\n",(unsigned long)(item->data)); 178 } 179 #endif 180 } 181 182 unsigned long shListGetCount( SHLIST *head ) 183 { 184 return( (unsigned long)(head->data) ); 185 } 186