summaryrefslogtreecommitdiff
path: root/app/xwd/list.c
blob: c5ea6c81e20508d4301d79ff4ba7d2e7fb341119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* $Xorg: list.c,v 1.5 2001/02/09 02:06:03 xorgcvs Exp $ */
/** ------------------------------------------------------------------------
	This file contains routines for manipulating generic lists.
	Lists are implemented with a "harness".  In other words, each
	node in the list consists of two pointers, one to the data item
	and one to the next node in the list.  The head of the list is
	the same struct as each node, but the "item" ptr is used to point
	to the current member of the list (used by the first_in_list and
	next_in_list functions).

Copyright 1994 Hewlett-Packard Co.
Copyright 1996, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.

  ----------------------------------------------------------------------- **/
/* $XFree86: xc/programs/xwd/list.c,v 3.6 2001/12/14 20:02:33 dawes Exp $ */

#include <stdio.h>
#include <stdlib.h>

#include "list.h"


/** ------------------------------------------------------------------------
	Sets the pointers of the specified list to NULL.
    --------------------------------------------------------------------- **/
void zero_list(list_ptr lp)
{
    lp->next = NULL;
    lp->ptr.item = NULL;
}


/** ------------------------------------------------------------------------
	Adds item to the list pointed to by lp.  Finds the end of the
	list, then mallocs a new list node onto the end of the list.
	The item pointer in the new node is set to "item" passed in,
	and the next pointer in the new node is set to NULL.
	Returns 1 if successful, 0 if the malloc failed.
    -------------------------------------------------------------------- **/
int add_to_list(list_ptr lp, void *item)
{
    while (lp->next) {
	lp = lp->next;
    }
    if ((lp->next = (list_ptr) malloc( sizeof( list_item))) == NULL) {

	return 0;
    }
    lp->next->ptr.item = item;
    lp->next->next = NULL;

    return 1;
}


/** ------------------------------------------------------------------------
	Creates a new list and sets its pointers to NULL.  
	Returns a pointer to the new list.
    -------------------------------------------------------------------- **/
list_ptr new_list ()
{
    list_ptr lp;

    if ((lp = (list_ptr) malloc( sizeof( list_item)))) {
	lp->next = NULL;
	lp->ptr.item = NULL;
    }

    return lp;
}


/** ------------------------------------------------------------------------
	Creates a new list head, pointing to the same list as the one
	passed in.  If start_at_curr is TRUE, the new list's first item
	is the "current" item (as set by calls to first/next_in_list()).
	If start_at_curr is FALSE, the first item in the new list is the
	same as the first item in the old list.  In either case, the
	curr pointer in the new list is the same as in the old list.
	Returns a pointer to the new list head.
    -------------------------------------------------------------------- **/
list_ptr dup_list_head(list_ptr lp, int start_at_curr)
{
    list_ptr new_list;

    if ((new_list = (list_ptr) malloc( sizeof( list_item))) == NULL) {

        return (list_ptr)NULL;
    }
    new_list->next = start_at_curr ? lp->ptr.curr : lp->next;
    new_list->ptr.curr = lp->ptr.curr;

    return new_list;
}


/** ------------------------------------------------------------------------
	Returns the number of items in the list.
    -------------------------------------------------------------------- **/
unsigned int list_length(list_ptr lp)
{
    unsigned int count = 0;

    while (lp->next) {
	count++;
	lp = lp->next;
    }

    return count;
}


/** ------------------------------------------------------------------------
	Scans thru list, looking for a node whose ptr.item is equal to
	the "item" passed in.  "Equal" here means the same address - no
	attempt is made to match equivalent values stored in different
	locations.  If a match is found, that node is deleted from the
	list.  Storage for the node is freed, but not for the item itself.
	Returns a pointer to the item, so the caller can free it if it
	so desires.  If a match is not found, returns NULL.
    -------------------------------------------------------------------- **/
void *delete_from_list(list_ptr lp, void *item)
{
    list_ptr new_next;

    while (lp->next) {
	if (lp->next->ptr.item == item) {
	    new_next = lp->next->next;
	    free (lp->next);
	    lp->next = new_next;

	    return item;
	}
	lp = lp->next;
    }

    return NULL;
}


/** ------------------------------------------------------------------------
	Deletes each node in the list *except the head*.  This allows
	the deletion of lists where the head is not malloced or created
	with new_list().  If free_items is true, each item pointed to 
	from the node is freed, in addition to the node itself.
    -------------------------------------------------------------------- **/
void delete_list(list_ptr lp, int free_items)
{
    list_ptr del_node;
    void *item;

    while (lp->next) {
	del_node = lp->next;
	item = del_node->ptr.item;
	lp->next = del_node->next;
	free (del_node);
	if (free_items) {
	    free( item);
	}
    }
}

void delete_list_destroying(list_ptr lp, void destructor(void *item))
{
    list_ptr del_node;
    void *item;

    while (lp->next) {
	del_node = lp->next;
	item = del_node->ptr.item;
	lp->next = del_node->next;
	free( del_node);
	if (destructor) {
	    destructor( item);
	}
    }
}


/** ------------------------------------------------------------------------
	Returns a ptr to the first *item* (not list node) in the list.
	Sets the list head node's curr ptr to the first node in the list.
	Returns NULL if the list is empty.
    -------------------------------------------------------------------- **/
void * first_in_list(list_ptr lp)
{
    if (! lp) {

	return NULL;
    }
    lp->ptr.curr = lp->next;

    return lp->ptr.curr ? lp->ptr.curr->ptr.item : NULL;
}

/** ------------------------------------------------------------------------
	Returns a ptr to the next *item* (not list node) in the list.
	Sets the list head node's curr ptr to the next node in the list.
	first_in_list must have been called prior.
	Returns NULL if no next item.
    -------------------------------------------------------------------- **/
void * next_in_list(list_ptr lp)
{
    if (! lp) {

	return NULL;
    }
    if (lp->ptr.curr) {
	lp->ptr.curr = lp->ptr.curr->next;
    }

    return lp->ptr.curr ? lp->ptr.curr->ptr.item : NULL;
}

int list_is_empty(list_ptr lp)
{
    return (lp == NULL || lp->next == NULL);
}