Debian Codegraph @

contrib/xwintoppm/list.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* $XConsortium: list.c /main/4 1996/10/14 15:03:56 swick $ */
2 /* $XFree86: xc/programs/xwd/list.c,v 3.1 1996/12/23 07:14:46 dawes Exp $ */
3 /** ------------------------------------------------------------------------
4 This file contains routines for manipulating generic lists.
5 Lists are implemented with a "harness". In other words, each
6 node in the list consists of two pointers, one to the data item
7 and one to the next node in the list. The head of the list is
8 the same struct as each node, but the "item" ptr is used to point
9 to the current member of the list (used by the first_in_list and
10 next_in_list functions).
11
12 Copyright (c) 1994 Hewlett-Packard Co.
13 Copyright (c) 1996 X Consortium
14
15 Permission is hereby granted, free of charge, to any person obtaining
16 a copy of this software and associated documentation files (the
17 "Software"), to deal in the Software without restriction, including
18 without limitation the rights to use, copy, modify, merge, publish,
19 distribute, sublicense, and sell copies of the Software, and to
20 permit persons to whom the Software is furnished to do so, subject to
21 the following conditions:
22
23 The above copyright notice and this permission notice shall be included
24 in all copies or substantial portions of the Software.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
30 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32 OTHER DEALINGS IN THE SOFTWARE.
33
34 Except as contained in this notice, the name of the X Consortium shall
35 not be used in advertising or otherwise to promote the sale, use or
36 other dealings in this Software without prior written authorization
37 from the X Consortium.
38
39 ----------------------------------------------------------------------- **/
40
41 #include <stdio.h>
42
43 #ifndef X_NOT_STDC_ENV
44 #include <stdlib.h>
45 #else
46 char *malloc();
47 #endif
48
49 #include "list.h"
50
51
52 /** ------------------------------------------------------------------------
53 Sets the pointers of the specified list to NULL.
54 --------------------------------------------------------------------- **/
55 #if NeedFunctionPrototypes
56 void zero_list(list_ptr lp)
57 #else
58 void zero_list(lp)
59 list_ptr lp;
60 #endif
61 {
62 lp->next = NULL;
63 lp->ptr.item = NULL;
64 }
65
66
67 /** ------------------------------------------------------------------------
68 Adds item to the list pointed to by lp. Finds the end of the
69 list, then mallocs a new list node onto the end of the list.
70 The item pointer in the new node is set to "item" passed in,
71 and the next pointer in the new node is set to NULL.
72 Returns 1 if successful, 0 if the malloc failed.
73 -------------------------------------------------------------------- **/
74 #if NeedFunctionPrototypes
75 int add_to_list(list_ptr lp, void *item)
76 #else
77 int add_to_list(lp, item)
78 list_ptr lp;
79 void *item;
80 #endif
81 {
82 while (lp->next) {
83 lp = lp->next;
84 }
85 if ((lp->next = (list_ptr) malloc( sizeof( list_item))) == NULL) {
86
87 return 0;
88 }
89 lp->next->ptr.item = item;
90 lp->next->next = NULL;
91
92 return 1;
93 }
94
95
96 /** ------------------------------------------------------------------------
97 Creates a new list and sets its pointers to NULL.
98 Returns a pointer to the new list.
99 -------------------------------------------------------------------- **/
100 list_ptr new_list ()
101 {
102 list_ptr lp;
103
104 if (lp = (list_ptr) malloc( sizeof( list_item))) {
105 lp->next = NULL;
106 lp->ptr.item = NULL;
107 }
108
109 return lp;
110 }
111
112
113 /** ------------------------------------------------------------------------
114 Creates a new list head, pointing to the same list as the one
115 passed in. If start_at_curr is TRUE, the new list's first item
116 is the "current" item (as set by calls to first/next_in_list()).
117 If start_at_curr is FALSE, the first item in the new list is the
118 same as the first item in the old list. In either case, the
119 curr pointer in the new list is the same as in the old list.
120 Returns a pointer to the new list head.
121 -------------------------------------------------------------------- **/
122 #if NeedFunctionPrototypes
123 list_ptr dup_list_head(list_ptr lp, int start_at_curr)
124 #else
125 list_ptr dup_list_head(lp, start_at_curr)
126 list_ptr lp;
127 int start_at_curr;
128 #endif
129 {
130 list_ptr new_list;
131
132 if ((new_list = (list_ptr) malloc( sizeof( list_item))) == NULL) {
133
134 return (list_ptr)NULL;
135 }
136 new_list->next = start_at_curr ? lp->ptr.curr : lp->next;
137 new_list->ptr.curr = lp->ptr.curr;
138
139 return new_list;
140 }
141
142
143 /** ------------------------------------------------------------------------
144 Returns the number of items in the list.
145 -------------------------------------------------------------------- **/
146 #if NeedFunctionPrototypes
147 unsigned int list_length(list_ptr lp)
148 #else
149 unsigned int list_length(lp)
150 list_ptr lp;
151 #endif
152 {
153 unsigned int count = 0;
154
155 while (lp->next) {
156 count++;
157 lp = lp->next;
158 }
159
160 return count;
161 }
162
163
164 /** ------------------------------------------------------------------------
165 Scans thru list, looking for a node whose ptr.item is equal to
166 the "item" passed in. "Equal" here means the same address - no
167 attempt is made to match equivalent values stored in different
168 locations. If a match is found, that node is deleted from the
169 list. Storage for the node is freed, but not for the item itself.
170 Returns a pointer to the item, so the caller can free it if it
171 so desires. If a match is not found, returns NULL.
172 -------------------------------------------------------------------- **/
173 #if NeedFunctionPrototypes
174 void *delete_from_list(list_ptr lp, void *item)
175 #else
176 void *delete_from_list(lp, item)
177 list_ptr lp;
178 void *item;
179 #endif
180 {
181 list_ptr new_next;
182
183 while (lp->next) {
184 if (lp->next->ptr.item == item) {
185 new_next = lp->next->next;
186 free (lp->next);
187 lp->next = new_next;
188
189 return item;
190 }
191 lp = lp->next;
192 }
193
194 return NULL;
195 }
196
197
198 /** ------------------------------------------------------------------------
199 Deletes each node in the list *except the head*. This allows
200 the deletion of lists where the head is not malloced or created
201 with new_list(). If free_items is true, each item pointed to
202 from the node is freed, in addition to the node itself.
203 -------------------------------------------------------------------- **/
204 #if NeedFunctionPrototypes
205 void delete_list(list_ptr lp, int free_items)
206 #else
207 void delete_list(lp, free_items)
208 list_ptr lp;
209 int free_items;
210 #endif
211 {
212 list_ptr del_node;
213 void *item;
214
215 while (lp->next) {
216 del_node = lp->next;
217 item = del_node->ptr.item;
218 lp->next = del_node->next;
219 free (del_node);
220 if (free_items) {
221 free( item);
222 }
223 }
224 }
225
226 #if NeedFunctionPrototypes
227 void delete_list_destroying(list_ptr lp, void destructor(void *item))
228 #else
229 void delete_list_destroying(lp, destructor)
230 list_ptr lp;
231 void (*destructor)();
232 #endif
233 {
234 list_ptr del_node;
235 void *item;
236
237 while (lp->next) {
238 del_node = lp->next;
239 item = del_node->ptr.item;
240 lp->next = del_node->next;
241 free( del_node);
242 if (destructor) {
243 destructor( item);
244 }
245 }
246 }
247
248
249 /** ------------------------------------------------------------------------
250 Returns a ptr to the first *item* (not list node) in the list.
251 Sets the list head node's curr ptr to the first node in the list.
252 Returns NULL if the list is empty.
253 -------------------------------------------------------------------- **/
254 #if NeedFunctionPrototypes
255 void * first_in_list(list_ptr lp)
256 #else
257 void * first_in_list(lp)
258 list_ptr lp;
259 #endif
260 {
261 if (! lp) {
262
263 return NULL;
264 }
265 lp->ptr.curr = lp->next;
266
267 return lp->ptr.curr ? lp->ptr.curr->ptr.item : NULL;
268 }
269
270 /** ------------------------------------------------------------------------
271 Returns a ptr to the next *item* (not list node) in the list.
272 Sets the list head node's curr ptr to the next node in the list.
273 first_in_list must have been called prior.
274 Returns NULL if no next item.
275 -------------------------------------------------------------------- **/
276 #if NeedFunctionPrototypes
277 void * next_in_list(list_ptr lp)
278 #else
279 void * next_in_list(lp)
280 list_ptr lp;
281 #endif
282 {
283 if (! lp) {
284
285 return NULL;
286 }
287 if (lp->ptr.curr) {
288 lp->ptr.curr = lp->ptr.curr->next;
289 }
290
291 return lp->ptr.curr ? lp->ptr.curr->ptr.item : NULL;
292 }
293
294 #if NeedFunctionPrototypes
295 int list_is_empty(list_ptr lp)
296 #else
297 int list_is_empty(lp)
298 list_ptr lp;
299 #endif
300 {
301 return (lp == NULL || lp->next == NULL);
302 }
303