| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "list.h"cxx . . $ `<file>/Descent3/list.h`/ |
| 20 | // #include <malloc.h> |
| 21 | #include "mem.h"cxx . . $ `<file>/mem/mem.h`/ |
| 22 | |
| 23 | // Allocates space for a new list node, returning the pointer to it |
| 24 | listnodecxx . . $ listnode# *NewListNodecxx . . $ NewListNode(d74684795f1b3fc8).(void) { |
| 25 | listnodecxx . . $ listnode# *node; |
| 26 | |
| 27 | node = (listnodecxx . . $ listnode# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(listnode)); |
| 28 | if (node == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 29 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Not enough memory for a new listnode!\n"); |
| 30 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 31 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 32 | } |
| 33 | |
| 34 | node->datacxx . . $ listnode#data. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 35 | node->nextcxx . . $ listnode#next. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 36 | node->prevcxx . . $ listnode#prev. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 37 | |
| 38 | return node; |
| 39 | } |
| 40 | |
| 41 | // Adds an item to a list |
| 42 | // Returns 1 if everything is ok, else 0 |
| 43 | int AddListItemcxx . . $ AddListItem(69ddacfa89c563ce).(listnodecxx . . $ listnode# **listp, void *item) { |
| 44 | listnodecxx . . $ listnode# *newnode, *curr; |
| 45 | |
| 46 | newnode = NewListNodecxx . . $ NewListNode(d74684795f1b3fc8).(); |
| 47 | |
| 48 | if (newnode == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 49 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "There was a problem mallocing list node memory!\n"); |
| 50 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | if (*listp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) // If this list is empty, construct a new head |
| 55 | { |
| 56 | newnode->datacxx . . $ listnode#data. = item; |
| 57 | *listp = newnode; |
| 58 | return 1; |
| 59 | } else { |
| 60 | // Go through until the end of the list and add the new item there |
| 61 | for (curr = *listp; curr->nextcxx . . $ listnode#next. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; curr = curr->nextcxx . . $ listnode#next.) { |
| 62 | if (curr->datacxx . . $ listnode#data. == item) { |
| 63 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 64 | return 0; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | newnode->datacxx . . $ listnode#data. = item; |
| 69 | curr->nextcxx . . $ listnode#next. = newnode; |
| 70 | newnode->nextcxx . . $ listnode#next. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 71 | newnode->prevcxx . . $ listnode#prev. = curr; |
| 72 | } |
| 73 | |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | // Removes an item from a list |
| 78 | int RemoveListItemcxx . . $ RemoveListItem(69ddacfa89c563ce).(listnodecxx . . $ listnode# **listp, void *item) { |
| 79 | listnodecxx . . $ listnode# *curr, *node; |
| 80 | int inlist = 0; |
| 81 | |
| 82 | for (curr = *listp; curr != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; curr = curr->nextcxx . . $ listnode#next.) { |
| 83 | if (curr->datacxx . . $ listnode#data. == item) { |
| 84 | inlist = 1; |
| 85 | node = curr; |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (!inlist) { |
| 91 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "RemoveItem: Warning, item not found in list\n"); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | // Head of list is a special case |
| 96 | if (node == *listp) { |
| 97 | *listp = node->nextcxx . . $ listnode#next.; |
| 98 | if (*listp != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 99 | (*listp)->prevcxx . . $ listnode#prev. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 100 | mem_freecxx . . $ `mem/mem.h:80:9`!(node); |
| 101 | return 1; |
| 102 | } else { |
| 103 | if (node->prevcxx . . $ listnode#prev. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 104 | node->prevcxx . . $ listnode#prev.->nextcxx . . $ listnode#next. = node->nextcxx . . $ listnode#next.; |
| 105 | if (node->nextcxx . . $ listnode#next. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) |
| 106 | node->nextcxx . . $ listnode#next.->prevcxx . . $ listnode#prev. = node->prevcxx . . $ listnode#prev.; |
| 107 | mem_freecxx . . $ `mem/mem.h:80:9`!(node); |
| 108 | } |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | // Destroys all the nodes in a list |
| 113 | // The items must be freed in another routine |
| 114 | void DestroyListcxx . . $ DestroyList(7102129d4b5eb48d).(listnodecxx . . $ listnode# **listp) { |
| 115 | listnodecxx . . $ listnode# *node, *next; |
| 116 | |
| 117 | for (node = *listp; node != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; node = next) { |
| 118 | next = node->nextcxx . . $ listnode#next.; |
| 119 | mem_freecxx . . $ `mem/mem.h:80:9`!(node); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Returns the number of items in a list |
| 124 | int CountListItemscxx . . $ CountListItems(738f935373f69df8).(listnodecxx . . $ listnode# **listp) { |
| 125 | listnodecxx . . $ listnode# *node; |
| 126 | int count = 0; |
| 127 | |
| 128 | for (node = *listp; node != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; node = node->nextcxx . . $ listnode#next.) |
| 129 | count++; |
| 130 | |
| 131 | return count; |
| 132 | } |
| 133 | |
| 134 | // returns a pointer the given item index in a list |
| 135 | // Returns NULL if point not found |
| 136 | void *GetListItemcxx . . $ GetListItem(559f24056c1df4cf).(listnodecxx . . $ listnode# **listp, int index) { |
| 137 | listnodecxx . . $ listnode# *node; |
| 138 | |
| 139 | for (node = *listp; node != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; node = node->nextcxx . . $ listnode#next.) { |
| 140 | if (index-- == 0) |
| 141 | return node->datacxx . . $ listnode#data.; |
| 142 | } |
| 143 | |
| 144 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 145 | } |
| 146 | |
| 147 | // Returns how far from the head of the list a given item is |
| 148 | // Returns -1 if not found |
| 149 | int GetListItemIndexcxx . . $ GetListItemIndex(69ddacfa89c563ce).(listnodecxx . . $ listnode# **listp, void *item) { |
| 150 | listnodecxx . . $ listnode# *node; |
| 151 | int index = 0; |
| 152 | |
| 153 | for (node = *listp; node != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; node = node->nextcxx . . $ listnode#next.) { |
| 154 | if (node->datacxx . . $ listnode#data. == item) |
| 155 | return index; |
| 156 | index++; |
| 157 | } |
| 158 | |
| 159 | return -1; |
| 160 | } |