[1856] | 1 |
|
---|
| 2 | /***********************************************************************
|
---|
| 3 |
|
---|
| 4 | $Id: $
|
---|
| 5 |
|
---|
| 6 | Linked list utilities
|
---|
| 7 |
|
---|
| 8 | Copyright (c) 2015 Steven H. Levine
|
---|
| 9 |
|
---|
| 10 | 07 Aug 15 SHL Baseline
|
---|
| 11 |
|
---|
| 12 | ***********************************************************************/
|
---|
| 13 |
|
---|
| 14 | #define INCL_DOS
|
---|
| 15 |
|
---|
| 16 | #include "fm3dll.h"
|
---|
| 17 | #include "errutil.h" // Dos_Error...
|
---|
| 18 | #include "listutil.h"
|
---|
| 19 |
|
---|
| 20 | // Data definitions
|
---|
| 21 | #pragma data_seg(DATA1)
|
---|
| 22 |
|
---|
| 23 | static PSZ pszSrcFile = __FILE__;
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Append item to doubly linked list
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | VOID List2Append(PLIST2HEADER header, PLIST2 item)
|
---|
| 30 | {
|
---|
| 31 | item->next = NULL;
|
---|
| 32 | item->prev = header->last;
|
---|
| 33 | if (item->prev)
|
---|
| 34 | item->prev->next = item;
|
---|
| 35 | header->last = item;
|
---|
| 36 | if (!header->first)
|
---|
| 37 | header->first = item; // List empty
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * Get first item from doubly linked list
|
---|
| 42 | * @return item or NULL
|
---|
| 43 | */
|
---|
| 44 |
|
---|
| 45 | PLIST2 List2GetFirst(PLIST2HEADER header)
|
---|
| 46 | {
|
---|
| 47 | return header->first;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Delete first item from doubly linked list
|
---|
| 52 | * @return deleted item or NULL
|
---|
| 53 | */
|
---|
| 54 |
|
---|
| 55 | PLIST2 List2DeleteFirst(PLIST2HEADER header)
|
---|
| 56 | {
|
---|
| 57 | PLIST2 item = header->first;
|
---|
| 58 |
|
---|
| 59 | // If list not empty
|
---|
| 60 | if (item) {
|
---|
| 61 | header->first = item->next;
|
---|
| 62 | if (header->first)
|
---|
| 63 | header->first->prev = NULL;
|
---|
| 64 | if (header->last == item)
|
---|
| 65 | header->last = NULL;
|
---|
| 66 | item->next = NULL; // Catch illegal accesses
|
---|
| 67 | item->prev = NULL; // Catch illegal accesses
|
---|
| 68 | }
|
---|
| 69 | return item;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Delete specified item from doubly linked list
|
---|
| 74 | * @param header is list header
|
---|
| 75 | * @param item point to item to be deleted
|
---|
| 76 | * @return deleted item or NULL
|
---|
| 77 | * @note item must be a member of list
|
---|
| 78 | */
|
---|
| 79 |
|
---|
| 80 | VOID List2Delete(PLIST2HEADER header, PLIST2 item)
|
---|
| 81 | {
|
---|
| 82 | if (item->next) {
|
---|
| 83 | if (item->next->prev != item)
|
---|
| 84 | Runtime_Error(pszSrcFile, __LINE__, "item %u item->next->prev %p", item, item->next->prev);
|
---|
| 85 | item->next->prev = item->prev;
|
---|
| 86 | }
|
---|
| 87 | else
|
---|
| 88 | header->last = item->prev;
|
---|
| 89 |
|
---|
| 90 | if (item->prev) {
|
---|
| 91 | if (item->prev->next != item)
|
---|
| 92 | Runtime_Error(pszSrcFile, __LINE__, "item %u item->prev->next%p", item, item->prev->next);
|
---|
| 93 | item->prev->next = item->next;
|
---|
| 94 | }
|
---|
| 95 | else
|
---|
| 96 | header->first = item->next;
|
---|
| 97 |
|
---|
| 98 | item->prev = NULL; // Catch illegal accesses
|
---|
| 99 | item->next = NULL; // Catch illegal accesses
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Search list for matching item
|
---|
| 104 | * @returns list pointer or NULL
|
---|
| 105 | */
|
---|
| 106 |
|
---|
| 107 | PLIST2 List2Search(PLIST2HEADER header, PLIST2MATCH matchFunc, PVOID data) {
|
---|
| 108 | PLIST2 item;
|
---|
| 109 | for (item = header->first; item; item = item->next) {
|
---|
| 110 | if (matchFunc(item, data))
|
---|
| 111 | break; // Matched
|
---|
| 112 | } // for
|
---|
| 113 | return item;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | // #pragma alloc_text(LISTUTIL, FIXME)
|
---|