Remove UXPDS support
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Basic / OrderList.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 // $XConsortium: OrderList.C /main/6 1996/08/21 15:41:43 drk $
24 #include <stdio.h>
25
26 #define C_OrderList
27 #define L_Basic
28 #include <Prelude.h>
29
30 #if !defined(USL)
31 #include <strings.h>
32 #endif
33
34 // //////////////////////////////////////////////////////////////
35 // Public methods
36 // //////////////////////////////////////////////////////////////
37
38 ListEntry::~ListEntry()
39 {
40 }
41
42 OrderList::~OrderList()
43 {
44         if (f_size)
45                 clear();
46 }
47
48 void 
49 OrderList::clear()
50 {
51         ListEntry *node;
52
53     f_cursor = f_head;
54     while (f_cursor != NULL) {
55         node = f_cursor;
56         f_cursor = f_cursor->f_next;
57         delete node;
58     }
59     f_head = NULL;
60     f_tail = NULL;
61     f_cursor = NULL;
62         f_size = 0;
63     return;
64 }
65
66 int
67 OrderList::add(ListEntry *node, AddCode where, bool mvcursor)
68 {
69     if (node != NULL) {
70         if (f_cursor == NULL)
71           insertNew(node);
72         else {
73             switch (where)
74               {
75                 case addAfter:
76                   if (f_cursor == f_tail)
77                     insertTail(node);
78                   else
79                     insertAfter(node);
80                   break;
81                 case addBefore:
82                   if (f_cursor == f_head)
83                     insertHead(node);
84                   else
85                     insertBefore(node);
86                   break;
87                 case addHead:
88                   insertHead(node);
89                   break;
90                 case addTail:
91                   insertTail(node);
92                   break;
93                 default:
94                   return OLIST_ERROR;
95               }
96         }
97         if ((mvcursor) || (f_cursor == NULL))
98           f_cursor = node;
99     }
100     else
101         return OLIST_ERROR;
102
103         f_size++;
104
105     return OLIST_OK;
106 }
107
108 int
109 OrderList::remove()
110 {
111     ListEntry      *node;
112     int             status = OLIST_OK;
113
114     node = f_cursor;
115     if ((node == f_head) && (node == f_tail)) {
116         f_head = NULL;
117         f_cursor = NULL;
118         f_tail = NULL;
119         status = OLIST_LAST_REMOVD;
120     }
121     else if (node == f_head) {
122         f_head = node->f_next;
123         f_cursor = node->f_next;
124         f_cursor->f_prev = NULL;
125         status = OLIST_HEAD_REMOVD;
126     }
127     else if (node == f_tail) {
128         f_tail = node->f_prev;
129         f_cursor = node->f_prev;
130         f_cursor->f_next = NULL;
131         status = OLIST_TAIL_REMOVD;
132     }
133     else {
134         node->f_prev->f_next = node->f_next;
135         node->f_next->f_prev = node->f_prev;
136         f_cursor = node->f_next;
137     }
138
139     delete node;
140
141     f_size--;
142
143     return status;
144 }
145
146 ListEntry *
147 OrderList::extract()
148 {
149     ListEntry      *node;
150 //  int             status = OLIST_OK;
151
152     node = f_cursor;
153     if ((node == f_head) && (node == f_tail)) {
154         f_head = NULL;
155         f_cursor = NULL;
156         f_tail = NULL;
157 //      status = OLIST_LAST_REMOVD;
158     }
159     else if (node == f_head) {
160         f_head = node->f_next;
161         f_cursor = node->f_next;
162         f_cursor->f_prev = NULL;
163 //      status = OLIST_HEAD_REMOVD;
164     }
165     else if (node == f_tail) {
166         f_tail = node->f_prev;
167         f_cursor = node->f_prev;
168         f_cursor->f_next = NULL;
169 //      status = OLIST_TAIL_REMOVD;
170     }
171     else {
172         node->f_prev->f_next = node->f_next;
173         node->f_next->f_prev = node->f_prev;
174         f_cursor = node->f_next;
175     }
176
177     // delete node;
178
179     f_size--;
180
181     return node;
182 }
183
184 int
185 OrderList::next()
186 {
187   if (f_cursor != NULL)
188     f_cursor = f_cursor->f_next;
189
190   return OLIST_OK;
191 }
192
193 int
194 OrderList::prev()
195 {
196     int             status = OLIST_ERROR;
197     ListEntry      *index;
198
199     if (f_cursor != NULL) {
200         index = f_cursor->f_prev;
201         if (index != NULL) {
202             f_cursor = index;
203             status = OLIST_OK;
204         }
205     }
206     return status;
207 }
208
209 int
210 OrderList::head()
211 {
212     int             status = OLIST_ERROR;
213
214     if (f_head != NULL) {
215         f_cursor = f_head;
216         status = OLIST_OK;
217     }
218     return status;
219 }
220
221 int
222 OrderList::tail()
223 {
224     int             status = OLIST_ERROR;
225
226     if (f_tail != NULL) {
227         f_cursor = f_tail;
228         status = OLIST_OK;
229     }
230     return status;
231 }
232
233 ListEntry    *
234 OrderList::iterate(bool (*fn)(ListEntry *, void *), void *usr_def)
235 {
236     // NOTE: keep this routine in sync with iterate below !
237     ListEntry      *index;
238     bool         keep_going = TRUE;
239     ListEntry      *entry = NULL;
240
241         // Iterate from head (arbitrary choice)
242     index = f_head;
243     while ((index != NULL) && (keep_going)) {
244         keep_going = (*fn) (index, usr_def);
245         if (!(keep_going))
246             entry = index;
247         else
248             index = index->f_next;
249     }
250     return entry;
251 }
252 ListEntry    *
253 OrderList::iterate(bool (*fn)(OrderList *, ListEntry *, void *), void *usr_def)
254 {
255     // NOTE: keep this routine in sync with iterate above
256
257     ListEntry      *index;
258     bool         keep_going = TRUE;
259     ListEntry      *entry = NULL;
260
261         // Iterate from head (arbitrary choice)
262     index = f_head;
263     while ((index != NULL) && (keep_going)) {
264         keep_going = (*fn) (this, index, usr_def);
265         if (!(keep_going))
266             entry = index;
267         else
268             index = index->f_next;
269     }
270     return entry;
271 }
272
273 // //////////////////////////////////////////////////////////////
274 // Private methods
275 // //////////////////////////////////////////////////////////////
276
277 void 
278 OrderList::insertNew(ListEntry *node)
279 {
280         f_head = node;
281         f_tail = node;
282         node->f_prev = NULL;
283         node->f_next = NULL;
284         return;
285 }
286
287 void 
288 OrderList::insertAfter(ListEntry *node)
289 {
290         f_cursor->f_next->f_prev = node;
291         node->f_next = f_cursor->f_next;
292         f_cursor->f_next = node;
293         node->f_prev = f_cursor;
294         return;
295 }
296
297 void 
298 OrderList::insertBefore(ListEntry *node)
299 {
300         f_cursor->f_prev->f_next = node;
301         node->f_prev = f_cursor->f_prev;
302         f_cursor->f_prev = node;
303         node->f_next = f_cursor;
304         return;
305 }
306
307 void 
308 OrderList::insertTail(ListEntry *node)
309 {
310         ListEntry          *tnode;
311
312         tnode = f_tail;
313         f_tail = node;
314         tnode->f_next = node;
315         node->f_prev = tnode;
316         node->f_next = NULL;
317         return;
318 }
319
320 void 
321 OrderList::insertHead(ListEntry *node)
322 {
323         ListEntry          *tnode;
324
325         tnode = f_head;
326         f_head = node;
327         tnode->f_prev = node;
328         node->f_next = tnode;
329         node->f_prev = NULL;
330         return;
331 }
332
333