comparison arogue5/list.c @ 63:0ed67132cf10

Import Advanced Rogue 5.8 from the Roguelike Restoration Project (r1490)
author elwin
date Thu, 09 Aug 2012 22:58:48 +0000
parents
children 56e748983fa8
comparison
equal deleted inserted replaced
62:0ef99244acb8 63:0ed67132cf10
1 /*
2 * Functions for dealing with linked lists of goodies
3 *
4 * Advanced Rogue
5 * Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka and AT&T
6 * All rights reserved.
7 *
8 * Based on "Rogue: Exploring the Dungeons of Doom"
9 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
10 * All rights reserved.
11 *
12 * See the file LICENSE.TXT for full copyright and licensing information.
13 */
14
15 #include <stdlib.h>
16 #include "curses.h"
17 #include "rogue.h"
18
19 /*
20 * detach:
21 * Takes an item out of whatever linked list it might be in
22 */
23
24 _detach(list, item)
25 register struct linked_list **list, *item;
26 {
27 if (*list == item)
28 *list = next(item);
29 if (prev(item) != NULL) item->l_prev->l_next = next(item);
30 if (next(item) != NULL) item->l_next->l_prev = prev(item);
31 item->l_next = NULL;
32 item->l_prev = NULL;
33 }
34
35 /*
36 * _attach:
37 * add an item to the head of a list
38 */
39
40 _attach(list, item)
41 register struct linked_list **list, *item;
42 {
43 if (*list != NULL)
44 {
45 item->l_next = *list;
46 (*list)->l_prev = item;
47 item->l_prev = NULL;
48 }
49 else
50 {
51 item->l_next = NULL;
52 item->l_prev = NULL;
53 }
54
55 *list = item;
56 }
57
58 /*
59 * o_free_list:
60 * Throw the whole object list away
61 */
62
63 _o_free_list(ptr)
64 register struct linked_list **ptr;
65 {
66 register struct linked_list *item;
67
68 while (*ptr != NULL)
69 {
70 item = *ptr;
71 *ptr = next(item);
72 o_discard(item);
73 }
74 }
75
76 /*
77 * o_discard:
78 * free up an item and its object(and maybe contents)
79 */
80
81 o_discard(item)
82 register struct linked_list *item;
83 {
84 register struct object *obj;
85 obj = OBJPTR(item);
86 if (obj->contents != NULL)
87 o_free_list(obj->contents);
88 total -= 2;
89 FREE(item->l_data);
90 FREE(item);
91 }
92
93 /*
94 * t_free_list:
95 * Throw the whole thing list away
96 */
97
98 _t_free_list(ptr)
99 register struct linked_list **ptr;
100 {
101 register struct linked_list *item;
102
103 while (*ptr != NULL)
104 {
105 item = *ptr;
106 *ptr = next(item);
107 t_discard(item);
108 }
109 }
110
111 /*
112 * t_discard:
113 * free up an item and its thing
114 */
115
116 t_discard(item)
117 struct linked_list *item;
118 {
119 total -= 2;
120 FREE(item->l_data);
121 FREE(item);
122 }
123
124 /*
125 * destroy_item:
126 * get rid of an item structure -- don't worry about contents
127 */
128
129 destroy_item(item)
130 register struct linked_list *item;
131 {
132 total--;
133 FREE(item);
134 }
135
136 /*
137 * new_item
138 * get a new item with a specified size
139 */
140
141 struct linked_list *
142 new_item(size)
143 int size;
144 {
145 register struct linked_list *item;
146
147 if ((item = (struct linked_list *) new(sizeof *item)) == NULL)
148 msg("Ran out of memory for header after %d items", total);
149 if ((item->l_data = new(size)) == NULL)
150 msg("Ran out of memory for data after %d items", total);
151 item->l_next = item->l_prev = NULL;
152 return item;
153 }
154
155 /*
156 * creat_item:
157 * Create just an item structure -- don't make any contents
158 */
159
160 struct linked_list *
161 creat_item()
162 {
163 register struct linked_list *item;
164
165 if ((item = (struct linked_list *) new(sizeof *item)) == NULL)
166 msg("Ran out of memory for header after %d items", total);
167 item->l_next = item->l_prev = NULL;
168 return item;
169 }
170
171 char *
172 new(size)
173 int size;
174 {
175 register char *space = ALLOC(size);
176 static char errbuf[LINELEN];
177
178 if (space == NULL) {
179 sprintf(errbuf,"Rogue ran out of memory (used = %d, wanted = %d).",
180 md_memused(), size);
181 fatal(errbuf);
182 }
183 total++;
184 return space;
185 }