/* Output from p2c, the Pascal-to-C translator */ /* From input file "lists.pas" */ #include #define LISTS_G #include "lists.h" Static inode dummy_node = { 0x7FFFFFFF, NULL, NULL }; Void init_list(list *ilist) { ilist->head = &dummy_node; ilist->tail = &dummy_node; ilist->next = &dummy_node; ilist->count = 0; } Void duplicate(ilist, olist) list *ilist, *olist; { inode *current; Anyptr tempptr; if (ilist->size == 0) { printf("\n%%DUPLICATION attempted on non-fixed size list.\n\n"); _Escape(0); } current = ilist->head; while (current != &dummy_node) { /*not at end of list... */ tempptr = Malloc((long)ilist->size); memmove(tempptr, current->e, (long)ilist->size); add_list(olist, tempptr, current->idx); current = current->next; } } Void fixed_list(ilist, size) list *ilist; long size; { ilist->head = &dummy_node; ilist->tail = &dummy_node; ilist->next = &dummy_node; ilist->count = 0; ilist->size = size; } Void unravel(ilist) list *ilist; { inode *current, *last; current = ilist->head; while (current != &dummy_node) { /*not at end of list... */ last = current; current = current->next; Free(last); } /* else already empty list */ /* always set list up for possible reuse */ init_list(ilist); } /* Local variables for add: */ struct LOC_add { list *ilist; Anyptr element; long idx; inode *current, *last; } ; Local Void insert_(struct LOC_add *LINK) { inode *temp; LINK->ilist->count++; temp = (inode *)Malloc(sizeof(inode)); /* link in new node to next node */ temp->next = LINK->current; /* set element pointer */ temp->e = LINK->element; /* set index */ temp->idx = LINK->idx; /* fix previous node to point to new node, unless current is present head of list */ if (LINK->current != LINK->last) LINK->last->next = temp; /* inserted at head of list, replace head */ if (LINK->current == LINK->ilist->head) LINK->ilist->head = temp; /* inserted at tail of list, replace tail */ if (LINK->current == &dummy_node) LINK->ilist->tail = temp; LINK->current = temp; } Void add_list(list *ilist_, Anyptr element_, long idx_) { struct LOC_add V; V.ilist = ilist_; V.element = element_; V.idx = idx_; V.current = V.ilist->head; V.last = V.current; while (V.current->next != NULL) { /*not at end of list... */ if (V.idx > V.current->idx) { /* not found yet... */ V.last = V.current; V.current = V.current->next; } else { /* proper place to insert new node */ if (V.idx == V.current->idx) { /* replace element */ Free(V.current->e); V.current->e = V.element; V.ilist->next = V.current; return; } else { /* add new element to list */ insert_(&V); V.ilist->next = V.current; return; } } } /* else before dummy node (may or may not be empty) */ insert_(&V); V.ilist->next = V.current; } Anyptr recall_list(list *ilist, long idx) { inode *current; current = ilist->head; while (current->next != NULL) { /* NOT at dummy node */ if (idx <= current->idx) { /* not found yet... */ if (idx == current->idx) /* retrieve element */ return (current->e); else /* index not found */ return NULL; } current = current->next; } return NULL; /* maybe matches desired index */ } /* Local variables for remove: */ struct LOC_remove { list *ilist; inode *current, *last; } ; Local Void delete__(struct LOC_remove *LINK) { LINK->ilist->count--; /* delete element pointer */ Free(LINK->current->e); /* fix previous node to point to next node, unless current is present head of list */ if (LINK->current != LINK->last) LINK->last->next = LINK->current->next; /* deleted at head of list, replace head */ if (LINK->current == LINK->ilist->head) LINK->ilist->head = LINK->current->next; /* deleted at 'next' on list, replace next */ if (LINK->current == LINK->ilist->next) LINK->ilist->next = LINK->current->next; /* deleted at tail of list, replace tail */ if (LINK->current == LINK->ilist->tail) { if (LINK->current != LINK->last) LINK->ilist->tail = LINK->last; else LINK->ilist->tail = &dummy_node; } /* finally, remove inode */ Free(LINK->current); } Void remove_list(list *ilist_, long idx) { struct LOC_remove V; V.ilist = ilist_; V.current = V.ilist->head; V.last = V.current; while (V.current->next != NULL) { /*not at end of list... */ if (idx <= V.current->idx) { /* not found yet... */ if (idx == V.current->idx) { /* delete element */ delete__(&V); return; } else /* index not found */ return; } V.last = V.current; V.current = V.current->next; } /* else empty list */ } Void kill_list(list *ilist) { inode *current, *last; current = ilist->head; while (current != &dummy_node) { /*not at end of list... */ last = current; current = current->next; Free(last->e); Free(last); } /* else already empty list */ /* always set list up for possible reuse */ init_list(ilist); } Anyptr head_list(list *ilist) { Anyptr Result; Result = ilist->head->e; if( ilist->head->next == NULL ) ; /* do nothing... leave at dummy node */ else ilist->next = ilist->head->next; return Result; } Anyptr tail_list(list *ilist) { Anyptr Result; Result = ilist->tail->e; if( ilist->tail->next == NULL ) ; /* do nothing... leave at dummy node */ else ilist->next = ilist->tail->next; return Result; } Anyptr next_list(list *ilist) { Anyptr Result; Result = ilist->next->e; if( ilist->next->next == NULL ) ; /* do nothing... leave at dummy node */ else ilist->next = ilist->next->next; return Result; } boolean member_list(list *ilist, long idx) { inode *current; current = ilist->head; while (current->next != NULL) { /* not at dummy node */ if (idx <= current->idx) { /* not found yet... */ if (idx == current->idx) /* element found */ return true; else /* index not found */ return false; } current = current->next; } return false; /* maybe matches desired index */ } Void setnext_list(list *ilist, long idx) { ilist->next = ilist->head; while (ilist->next->next != NULL) { if (idx > ilist->next->idx) { /* not found yet... */ ilist->next = ilist->next->next; } else { /* maybe matches desired index */ if (idx == ilist->next->idx) { /* set next */ return; } else { /* index not found */ ilist->next = &dummy_node; return; } } } } void _lists_init() { static int _was_initialized = 0; if (_was_initialized++) return; dummy_node.next = &dummy_node; } /* End. */