comparison srogue/list.c @ 36:2128c7dc8a40

Import Super-Rogue 9.0 from the Roguelike Restoration Project (r1490)
author elwin
date Thu, 25 Nov 2010 12:21:41 +0000
parents
children d6b7c3fb37ea
comparison
equal deleted inserted replaced
35:05018c63a721 36:2128c7dc8a40
1 /*
2 * Functions for dealing with linked lists of goodies
3 *
4 * @(#)list.c 9.0 (rdk) 7/17/84
5 *
6 * Super-Rogue
7 * Copyright (C) 1984 Robert D. Kindelberger
8 * All rights reserved.
9 *
10 * Based on "Rogue: Exploring the Dungeons of Doom"
11 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
12 * All rights reserved.
13 *
14 * See the file LICENSE.TXT for full copyright and licensing information.
15 */
16
17 #include <stdlib.h>
18 #include "rogue.h"
19 #include "rogue.ext"
20
21 /*
22 * detach:
23 * Takes an item out of whatever linked list it might be in
24 */
25
26 _detach(list, item)
27 struct linked_list **list, *item;
28 {
29 if (*list == item)
30 *list = next(item);
31 if (prev(item) != NULL)
32 item->l_prev->l_next = next(item);
33 if (next(item) != NULL)
34 item->l_next->l_prev = prev(item);
35 item->l_next = NULL;
36 item->l_prev = NULL;
37 }
38
39 /*
40 * _attach: add an item to the head of a list
41 */
42 _attach(list, item)
43 struct linked_list **list, *item;
44 {
45 if (*list != NULL) {
46 item->l_next = *list;
47 (*list)->l_prev = item;
48 item->l_prev = NULL;
49 }
50 else {
51 item->l_next = NULL;
52 item->l_prev = NULL;
53 }
54 *list = item;
55 }
56
57 /*
58 * _free_list: Throw the whole blamed thing away
59 */
60 _free_list(ptr)
61 struct linked_list **ptr;
62 {
63 register struct linked_list *item;
64
65 while (*ptr != NULL) {
66 item = *ptr;
67 *ptr = next(item);
68 discard(item);
69 }
70 }
71
72 /*
73 * discard: free up an item
74 */
75 discard(item)
76 struct linked_list *item;
77 {
78 total -= 2;
79 FREE(item->l_data);
80 FREE(item);
81 }
82
83 /*
84 * new_item: get a new item with a specified size
85 */
86 struct linked_list *
87 new_item(size)
88 int size;
89 {
90 register struct linked_list *item;
91
92 item = (struct linked_list *) new(sizeof *item);
93 item->l_data = new(size);
94 item->l_next = item->l_prev = NULL;
95 return item;
96 }
97
98 char *
99 new(size)
100 int size;
101 {
102 register char *space = ALLOC(size);
103
104 if (space == NULL) {
105 sprintf(prbuf,"Rogue ran out of memory (%d).",sbrk(0));
106 fatal(prbuf);
107 }
108 total++;
109 return space;
110 }