http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / userapps / opensource / atm2684 / atm / ilmid / asn1 / asn_list.c
1 /*
2  * asn_list.c  - borrowed from Murray Goldberg
3  * 
4  * the following routines implement the list data structure 
5  *
6  * Copyright (C) 1992 the University of British Columbia
7  * 
8  * This library is free software; you can redistribute it and/or
9  * modify it provided that this copyright/license information is retained
10  * in original form.
11  *
12  * If you modify this file, you must clearly indicate your changes.
13  *
14  * This source code is distributed in the hope that it will be
15  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  */
18
19 #include "asn_config.h"
20 #include "asn_list.h"
21
22
23
24 /*
25  * this routine removes the current node from the list. After removal the
26  * current pointer will point to the next node in line, or NULL if the
27  * removed item was at the tail of the list.
28  */
29 void
30 AsnListRemove PARAMS( (list),
31 AsnList* list )
32 {
33     AsnListNode* node;
34
35     if( list->curr )
36         {
37         if( list->curr->next )
38             list->curr->next->prev = list->curr->prev;
39         else
40             list->last = list->curr->prev;
41
42         if( list->curr->prev )
43             list->curr->prev->next = list->curr->next;
44         else
45             list->first = list->curr->next;
46
47         node       = list->curr;
48
49         list->curr = list->curr->next;
50         list->count--;
51
52         free( node );
53         }
54 }
55
56 /*
57  * this creates a new node after the current node and returns the
58  * address of the memory allocated for data. The current pointer is changed
59  * to point to the newly added node in the list. If the current pointer is
60  * initially off the list then this operation fails.
61  */
62 void*
63 AsnListAdd PARAMS((list),
64 AsnList* list )
65 {
66     AsnListNode* newNode;
67     void*      dataAddr;
68
69     if( list->curr )
70         {
71         newNode  = (AsnListNode *) Asn1Alloc( sizeof(AsnListNode) + list->dataSize );
72         dataAddr = (void *) &(newNode->data);
73
74         newNode->next = list->curr->next;
75         newNode->prev = list->curr;
76         if( list->curr->next )
77             list->curr->next->prev = newNode;
78         else
79             list->last = newNode;
80         list->curr->next = newNode;
81
82         list->curr = newNode;
83         list->count++;
84         }
85
86     else
87         dataAddr = NULL;
88
89     return( dataAddr );
90 }
91
92 /*
93  * this creates a new node before the current node and returns the
94  * address of the memory allocated for data. The current pointer is changed 
95  * to point to the newly added node in the list. If the current pointer is
96  * initially off the list then this operation fails.
97  */
98 void*
99 AsnListInsert PARAMS( (list),
100 AsnList* list )
101 {
102     AsnListNode* newNode;
103     void*      dataAddr;
104
105     if( list->curr )
106         {
107         newNode  = (AsnListNode *) Asn1Alloc( sizeof(AsnListNode) + list->dataSize );
108         dataAddr = (void *) &(newNode->data);
109
110         newNode->next = list->curr;
111         newNode->prev = list->curr->prev;
112         if( list->curr->prev )
113             list->curr->prev->next = newNode;
114         else
115             list->first  = newNode;
116         list->curr->prev = newNode;
117
118         list->curr = newNode;
119         list->count++;
120         }
121
122     else
123         dataAddr = NULL;
124
125     return( dataAddr );
126 }
127
128
129 void
130 AsnListInit PARAMS((list, dataSize),
131 AsnList* list _AND_
132 int dataSize)
133 {
134     list->first = list->last = list->curr = NULL;
135     list->count = 0;
136     list->dataSize = dataSize;
137
138 }  /* AsnListInit */
139
140
141 AsnList*
142 AsnListNew PARAMS( (dataSize),
143 int dataSize )
144 {
145     AsnList* list;
146
147     list = (AsnList *) Asn1Alloc( sizeof(AsnList) );
148     list->first = list->last = list->curr = NULL;
149     list->count = 0;
150     list->dataSize = dataSize;
151
152     return( list );
153 }
154
155 /*
156  * backs up the current pointer by one and returns the data address of the new
157  * current node. If the current pointer is off the list, the new current node
158  * will be the last node of the list (unless the list is empty).
159  */
160 void*
161 AsnListPrev PARAMS( (list),
162 AsnList* list )
163 {
164     void* retVal;
165
166     if( list->curr == NULL )
167         list->curr = list->last;
168     else
169         list->curr = list->curr->prev;
170
171     if( list->curr == NULL )
172         retVal = NULL;
173     else
174         retVal = (void *) &(list->curr->data);
175
176     return( retVal );
177 }
178
179 /*
180  * advances the current pointer by one and returns the data address of the new
181  * current node. If the current pointer is off the list, the new current node
182  * will be the first node of the list (unless the list is empty).
183  */
184 void*
185 AsnListNext PARAMS( (list),
186 AsnList* list )
187 {
188     void* retVal;
189
190     if( list->curr == NULL )
191         list->curr = list->first;
192     else
193         list->curr = list->curr->next;
194
195     if( list->curr == NULL )
196         retVal = NULL;
197     else
198         retVal = (void *) &(list->curr->data);
199
200     return( retVal );
201 }
202
203 /*
204  * returns the data address of the last node (if there is one) and sets the
205  * current pointer to this node.
206  */
207 void*
208 AsnListLast PARAMS((list),
209 AsnList* list )
210 {
211     void* retVal;
212
213     list->curr = list->last;
214
215     if( list->curr == NULL )
216         retVal = NULL;
217     else
218         retVal = (void *) &(list->curr->data);
219
220     return( retVal );
221 }
222
223 /*
224  * returns the data address of the first node (if there is one) and sets the
225  * current pointer to this node.
226  */
227 void*
228 AsnListFirst PARAMS( (list),
229 AsnList* list )
230 {
231     void* retVal;
232
233     list->curr = list->first;
234
235     if( list->curr == NULL )
236         retVal = NULL;
237     else
238         retVal = (void *) &(list->curr->data);
239
240     return( retVal );
241 }
242
243 /*
244  * this creates a new node at the beginning of the list and returns the
245  * address of the memory allocated for data. The current pointer is changed 
246  * to point to the newly added node in the list.
247  */
248 void*
249 AsnListPrepend PARAMS( (list),
250 AsnList* list )
251 {
252     AsnListNode* newNode;
253     void*      dataAddr;
254
255     newNode  = (AsnListNode *) Asn1Alloc( sizeof(AsnListNode) + list->dataSize );
256     dataAddr = (void *) &(newNode->data);
257
258     newNode->prev = NULL;
259
260     if( list->first == NULL )
261         {
262         newNode->next = NULL;
263         list->first   = list->last = newNode;
264         }
265     else
266         {
267         newNode->next     = list->first;
268         list->first->prev = newNode;
269         list->first       = newNode;
270         }
271
272     list->curr = newNode;
273     list->count++;
274
275     return( dataAddr );
276 }
277
278 /*
279  * this creates a new node at the end of the list and returns the
280  * address of the memory allocated for data. The current pointer is changed
281  * to point to the newly added node in the list.
282  */
283 void*
284 AsnListAppend PARAMS( (list),
285 AsnList* list )
286 {
287     AsnListNode* newNode;
288     void*      dataAddr;
289
290     newNode  = (AsnListNode *) Asn1Alloc( sizeof(AsnListNode) + list->dataSize );
291     dataAddr = (void *) &(newNode->data);
292
293     newNode->next = NULL;
294
295     if( list->last == NULL )
296         {
297         newNode->prev = NULL;
298         list->first   = list->last = newNode;
299         }
300     else
301         {
302         newNode->prev     = list->last;
303         list->last->next  = newNode;
304         list->last        = newNode;
305         }
306
307     list->curr = newNode;
308     list->count++;
309
310     return( dataAddr );
311 }
312
313 void*
314 AsnListCurr PARAMS( (list),
315 AsnList* list )
316 {
317     void* retVal;
318
319     if( list->curr )
320         retVal = (void *) &(list->curr->data);
321     else
322         retVal = NULL;
323
324     return( retVal );
325 }
326
327 int
328 AsnListCount PARAMS( (list),
329 AsnList* list )
330 {
331     return( list->count );
332 }
333
334
335
336
337