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