2 * asn_list.c - borrowed from Murray Goldberg
4 * the following routines implement the list data structure
6 * Copyright (C) 1992 the University of British Columbia
8 * This library is free software; you can redistribute it and/or
9 * modify it provided that this copyright/license information is retained
12 * If you modify this file, you must clearly indicate your changes.
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.
19 #include "asn_config.h"
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.
30 AsnListRemove PARAMS( (list),
37 if( list->curr->next )
38 list->curr->next->prev = list->curr->prev;
40 list->last = list->curr->prev;
42 if( list->curr->prev )
43 list->curr->prev->next = list->curr->next;
45 list->first = list->curr->next;
49 list->curr = list->curr->next;
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.
63 AsnListAdd PARAMS((list),
71 newNode = (AsnListNode *) Asn1Alloc( sizeof(AsnListNode) + list->dataSize );
72 dataAddr = (void *) &(newNode->data);
74 newNode->next = list->curr->next;
75 newNode->prev = list->curr;
76 if( list->curr->next )
77 list->curr->next->prev = newNode;
80 list->curr->next = newNode;
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.
99 AsnListInsert PARAMS( (list),
102 AsnListNode* newNode;
107 newNode = (AsnListNode *) Asn1Alloc( sizeof(AsnListNode) + list->dataSize );
108 dataAddr = (void *) &(newNode->data);
110 newNode->next = list->curr;
111 newNode->prev = list->curr->prev;
112 if( list->curr->prev )
113 list->curr->prev->next = newNode;
115 list->first = newNode;
116 list->curr->prev = newNode;
118 list->curr = newNode;
130 AsnListInit PARAMS((list, dataSize),
134 list->first = list->last = list->curr = NULL;
136 list->dataSize = dataSize;
142 AsnListNew PARAMS( (dataSize),
147 list = (AsnList *) Asn1Alloc( sizeof(AsnList) );
148 list->first = list->last = list->curr = NULL;
150 list->dataSize = dataSize;
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).
161 AsnListPrev PARAMS( (list),
166 if( list->curr == NULL )
167 list->curr = list->last;
169 list->curr = list->curr->prev;
171 if( list->curr == NULL )
174 retVal = (void *) &(list->curr->data);
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).
185 AsnListNext PARAMS( (list),
190 if( list->curr == NULL )
191 list->curr = list->first;
193 list->curr = list->curr->next;
195 if( list->curr == NULL )
198 retVal = (void *) &(list->curr->data);
204 * returns the data address of the last node (if there is one) and sets the
205 * current pointer to this node.
208 AsnListLast PARAMS((list),
213 list->curr = list->last;
215 if( list->curr == NULL )
218 retVal = (void *) &(list->curr->data);
224 * returns the data address of the first node (if there is one) and sets the
225 * current pointer to this node.
228 AsnListFirst PARAMS( (list),
233 list->curr = list->first;
235 if( list->curr == NULL )
238 retVal = (void *) &(list->curr->data);
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.
249 AsnListPrepend PARAMS( (list),
252 AsnListNode* newNode;
255 newNode = (AsnListNode *) Asn1Alloc( sizeof(AsnListNode) + list->dataSize );
256 dataAddr = (void *) &(newNode->data);
258 newNode->prev = NULL;
260 if( list->first == NULL )
262 newNode->next = NULL;
263 list->first = list->last = newNode;
267 newNode->next = list->first;
268 list->first->prev = newNode;
269 list->first = newNode;
272 list->curr = newNode;
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.
284 AsnListAppend PARAMS( (list),
287 AsnListNode* newNode;
290 newNode = (AsnListNode *) Asn1Alloc( sizeof(AsnListNode) + list->dataSize );
291 dataAddr = (void *) &(newNode->data);
293 newNode->next = NULL;
295 if( list->last == NULL )
297 newNode->prev = NULL;
298 list->first = list->last = newNode;
302 newNode->prev = list->last;
303 list->last->next = newNode;
304 list->last = newNode;
307 list->curr = newNode;
314 AsnListCurr PARAMS( (list),
320 retVal = (void *) &(list->curr->data);
328 AsnListCount PARAMS( (list),
331 return( list->count );