]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Other/Maintained/Tools/Pccts/sorcerer/lib/sorlist.c
Add in the 1st version of ECP.
[mirror_edk2.git] / EdkCompatibilityPkg / Other / Maintained / Tools / Pccts / sorcerer / lib / sorlist.c
1 /*
2 * slist.c
3 *
4 * SOFTWARE RIGHTS
5 *
6 * We reserve no LEGAL rights to SORCERER -- SORCERER is in the public
7 * domain. An individual or company may do whatever they wish with
8 * source code distributed with SORCERER or the code generated by
9 * SORCERER, including the incorporation of SORCERER, or its output, into
10 * commerical software.
11 *
12 * We encourage users to develop software with SORCERER. However, we do
13 * ask that credit is given to us for developing SORCERER. By "credit",
14 * we mean that if you incorporate our source code into one of your
15 * programs (commercial product, research project, or otherwise) that you
16 * acknowledge this fact somewhere in the documentation, research report,
17 * etc... If you like SORCERER and have developed a nice tool with the
18 * output, please mention that you developed it using SORCERER. In
19 * addition, we ask that this header remain intact in our source code.
20 * As long as these guidelines are kept, we expect to continue enhancing
21 * this system and expect to make other tools available as they are
22 * completed.
23 *
24 * SORCERER 1.00B
25 * Terence Parr
26 * AHPCRC, University of Minnesota
27 * 1992-1994
28 */
29 #include <stdio.h>
30 #include <setjmp.h>
31
32 #ifdef PCCTS_USE_STDARG
33 #include <stdarg.h>
34 #else
35 #include <varargs.h>
36 #endif
37
38 #include "CASTBase.h"
39 #include "sorlist.h"
40
41 /* Iterate over a list of elements; returns ptr to a new element
42 * in list upon every call and NULL when no more are left.
43 * Very useful like this:
44 *
45 * cursor = mylist;
46 * while ( (p=slist_iterate(mylist,&cursor)) ) {
47 * / * place with element p * /
48 * }
49 *
50 * The cursor must be initialized to point to the list to iterate over.
51 */
52 void *
53 #ifdef __USE_PROTOS
54 slist_iterate(SList *list, SList **cursor)
55 #else
56 slist_iterate(list, cursor)
57 SList *list, **cursor;
58 #endif
59 {
60 void *e;
61
62 if ( list==NULL || cursor == NULL || *cursor==NULL ) return NULL;
63 if ( list== *cursor ) { *cursor = (*cursor)->next; }
64 e = (*cursor)->elem;
65 (*cursor) = (*cursor)->next;
66 return e;
67 }
68
69 /*
70 * add an element to a list.
71 *
72 * Any non-empty list has a sentinel node whose 'elem' pointer is really
73 * a pointer to the last element. (i.e. length(list) = #elemIn(list)+1).
74 * Elements are appended to the list.
75 */
76 void
77 #ifdef __USE_PROTOS
78 slist_add( SList **list, void *e )
79 #else
80 slist_add( list, e )
81 SList **list;
82 void *e;
83 #endif
84 {
85 SList *p, *tail;
86 require(e!=NULL, "slist_add: attempting to add NULL list element");
87
88 p = newSList;
89 require(p!=NULL, "slist_add: cannot alloc new list node");
90 p->elem = e;
91 if ( *list == NULL )
92 {
93 SList *sentinel = newSList;
94 require(sentinel!=NULL, "slist_add: cannot alloc sentinel node");
95 *list=sentinel;
96 sentinel->next = p;
97 sentinel->elem = (char *)p; /* set tail pointer */
98 }
99 else /* find end of list */
100 {
101 tail = (SList *) (*list)->elem; /* get tail pointer */
102 tail->next = p;
103 (*list)->elem = (char *) p; /* reset tail */
104 }
105 }
106
107 void
108 #ifdef __USE_PROTOS
109 slist_free(SList *list)
110 #else
111 slist_free(list)
112 SList *list;
113 #endif
114 {
115 SList *p,*q;
116
117 if ( list==NULL ) return; /* empty list */
118 for (p = list->next; p!=NULL; p=q)
119 {
120 q = p->next;
121 free(p);
122 }
123 }