1 /****************************************************************************** 2 * Internetting Cooperating Programmers 3 * ---------------------------------------------------------------------------- 4 * 5 * ____ PROJECT 6 * | _ \ __ _ _ __ ___ ___ _ __ 7 * | | | |/ _` | '_ \ / __/ _ \ '__| 8 * | |_| | (_| | | | | (_| __/ | 9 * |____/ \__,_|_| |_|\___\___|_| the IRC bot 10 * 11 * All files in this archive are subject to the GNU General Public License. 12 * 13 * $Source: /cvsroot/dancer/dancer/src/list.h,v $ 14 * $Revision: 1.1.1.1 $ 15 * $Date: 2000/11/13 02:42:45 $ 16 * $Author: holsta $ 17 * $State: Exp $ 18 * $Locker: $ 19 * 20 * --------------------------------------------------------------------------- 21 *****************************************************************************/ 22 23 #ifndef LIST_H 24 #define LIST_H 25 26 #ifndef NULL 27 #define NULL 0 28 #endif 29 30 typedef struct Header { 31 void *next; 32 void *prev; 33 } header; 34 35 typedef struct Liststruct { 36 struct Header h; 37 void *pointer; 38 } itemlist; 39 40 /* First returns the first entry in a list */ 41 #define First(list) ((NULL == (list)) ? NULL : ((header *)(list))->next) 42 /* Last returns the last entry in a list */ 43 #define Last(list) ((NULL == (list)) ? NULL : ((header *)(list))->prev) 44 45 /* Next returns the next entry relative to the given entry */ 46 #define Next(entry) (((header *)(entry))->next) 47 /* Prev returns the previous entry relative to the given entry */ 48 #define Prev(entry) (((header *)(entry))->prev) 49 50 /* Emptylist proves TRUE if there are no entries in the list */ 51 #define EmptyList(list) \ 52 ((NULL == (list)) || (NULL == ((header *)(list))->next)) 53 54 /* NewList creates a header for a list of the specified type */ 55 #define NewList(type) (type *)calloc(1, sizeof(type)) 56 57 /* NewEntry allocates a new entry of the specified type */ 58 #define NewEntry(type) (type *)calloc(1, sizeof(type)) 59 60 #define RemoveFirst(list) RemoveEntry((list), (list).h->next) 61 #define RemoveLast(list) RemoveEntry((list), (list).h->prev) 62 63 /* Move entry from source-list to the first entry in target-list */ 64 #define MoveFirst(source, entry, target) \ 65 do { \ 66 RemoveEntry((source), (entry)); \ 67 InsertFirst((target), (entry)); \ 68 } while (0) 69 70 /* Move entry from source-list to the last entry in target-list */ 71 #define MoveLast(source, entry, target) \ 72 do { \ 73 RemoveEntry((source), (entry)); \ 74 InsertLast((target), (entry)); \ 75 } while (0) 76 77 78 /* Insert entry as the first entry in a list */ 79 void InsertFirst(void *list, void *entry); 80 /* Insert entry as the last entry in a list */ 81 void InsertLast(void *list, void *entry); 82 /* Insert entry into a list after another entry */ 83 void InsertEntry(void *list, void *entry, void *after); 84 /* Remove entry from a list */ 85 void RemoveEntry(void *list, void *entry); 86 /* Remove entry from a list and free the resources */ 87 void DeleteEntry(void *list, void *entry, void (*cleanerFunc)(void *)); 88 89 /* Remove all entries except the header */ 90 void FlushList(void *list, void (*cleanerFunc)(void *)); 91 /* Remove an entire list including the header */ 92 #define DeleteList(list, cleanerFunc) \ 93 List_DeleteList((void **)&(list), (cleanerFunc)) 94 void List_DeleteList(void **list, void (*cleanerFunc)(void *)); 95 96 /* Aux func for itemlist */ 97 void FreeList(void *); 98 99 /* Sorts the list into alphabetical order; use the offsetof macro to 100 calculate the offset of the member (beware of NULL pointers) */ 101 void SortList(void *list, int offset); 102 103 #endif /* LIST_H */ 104