dtinfo: remove register keyword
[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 #include <strings.h>
30
31 // //////////////////////////////////////////////////////////////
32 // Public methods
33 // //////////////////////////////////////////////////////////////
34
35 ListEntry::~ListEntry()
36 {
37 }
38
39 OrderList::~OrderList()
40 {
41         if (f_size)
42                 clear();
43 }
44
45 void 
46 OrderList::clear()
47 {
48         ListEntry *node;
49
50     f_cursor = f_head;
51     while (f_cursor != NULL) {
52         node = f_cursor;
53         f_cursor = f_cursor->f_next;
54         delete node;
55     }
56     f_head = NULL;
57     f_tail = NULL;
58     f_cursor = NULL;
59         f_size = 0;
60     return;
61 }
62
63 int
64 OrderList::add(ListEntry *node, AddCode where, bool mvcursor)
65 {
66     if (node != NULL) {
67         if (f_cursor == NULL)
68           insertNew(node);
69         else {
70             switch (where)
71               {
72                 case addAfter:
73                   if (f_cursor == f_tail)
74                     insertTail(node);
75                   else
76                     insertAfter(node);
77                   break;
78                 case addBefore:
79                   if (f_cursor == f_head)
80                     insertHead(node);
81                   else
82                     insertBefore(node);
83                   break;
84                 case addHead:
85                   insertHead(node);
86                   break;
87                 case addTail:
88                   insertTail(node);
89                   break;
90                 default:
91                   return OLIST_ERROR;
92               }
93         }
94         if ((mvcursor) || (f_cursor == NULL))
95           f_cursor = node;
96     }
97     else
98         return OLIST_ERROR;
99
100     f_size++;
101
102     return OLIST_OK;
103 }
104
105 int
106 OrderList::remove()
107 {
108     ListEntry      *node;
109     int             status = OLIST_OK;
110
111     node = f_cursor;
112     if ((node == f_head) && (node == f_tail)) {
113         f_head = NULL;
114         f_cursor = NULL;
115         f_tail = NULL;
116         status = OLIST_LAST_REMOVD;
117     }
118     else if (node == f_head) {
119         f_head = node->f_next;
120         f_cursor = node->f_next;
121         f_cursor->f_prev = NULL;
122         status = OLIST_HEAD_REMOVD;
123     }
124     else if (node == f_tail) {
125         f_tail = node->f_prev;
126         f_cursor = node->f_prev;
127         f_cursor->f_next = NULL;
128         status = OLIST_TAIL_REMOVD;
129     }
130     else {
131         node->f_prev->f_next = node->f_next;
132         node->f_next->f_prev = node->f_prev;
133         f_cursor = node->f_next;
134     }
135
136     delete node;
137
138     f_size--;
139
140     return status;
141 }
142
143 ListEntry *
144 OrderList::extract()
145 {
146     ListEntry      *node;
147 //  int             status = OLIST_OK;
148
149     node = f_cursor;
150     if ((node == f_head) && (node == f_tail)) {
151         f_head = NULL;
152         f_cursor = NULL;
153         f_tail = NULL;
154 //      status = OLIST_LAST_REMOVD;
155     }
156     else if (node == f_head) {
157         f_head = node->f_next;
158         f_cursor = node->f_next;
159         f_cursor->f_prev = NULL;
160 //      status = OLIST_HEAD_REMOVD;
161     }
162     else if (node == f_tail) {
163         f_tail = node->f_prev;
164         f_cursor = node->f_prev;
165         f_cursor->f_next = NULL;
166 //      status = OLIST_TAIL_REMOVD;
167     }
168     else {
169         node->f_prev->f_next = node->f_next;
170         node->f_next->f_prev = node->f_prev;
171         f_cursor = node->f_next;
172     }
173
174     // delete node;
175
176     f_size--;
177
178     return node;
179 }
180
181 int
182 OrderList::next()
183 {
184   if (f_cursor != NULL)
185     f_cursor = f_cursor->f_next;
186
187   return OLIST_OK;
188 }
189
190 int
191 OrderList::prev()
192 {
193     int             status = OLIST_ERROR;
194     ListEntry      *index;
195
196     if (f_cursor != NULL) {
197         index = f_cursor->f_prev;
198         if (index != NULL) {
199             f_cursor = index;
200             status = OLIST_OK;
201         }
202     }
203     return status;
204 }
205
206 int
207 OrderList::head()
208 {
209     int             status = OLIST_ERROR;
210
211     if (f_head != NULL) {
212         f_cursor = f_head;
213         status = OLIST_OK;
214     }
215     return status;
216 }
217
218 int
219 OrderList::tail()
220 {
221     int             status = OLIST_ERROR;
222
223     if (f_tail != NULL) {
224         f_cursor = f_tail;
225         status = OLIST_OK;
226     }
227     return status;
228 }
229
230 ListEntry    *
231 OrderList::iterate(bool (*fn)(ListEntry *, void *), void *usr_def)
232 {
233     // NOTE: keep this routine in sync with iterate below !
234     ListEntry      *index;
235     bool         keep_going = TRUE;
236     ListEntry      *entry = NULL;
237
238         // Iterate from head (arbitrary choice)
239     index = f_head;
240     while ((index != NULL) && (keep_going)) {
241         keep_going = (*fn) (index, usr_def);
242         if (!(keep_going))
243             entry = index;
244         else
245             index = index->f_next;
246     }
247     return entry;
248 }
249 ListEntry    *
250 OrderList::iterate(bool (*fn)(OrderList *, ListEntry *, void *), void *usr_def)
251 {
252     // NOTE: keep this routine in sync with iterate above
253
254     ListEntry      *index;
255     bool         keep_going = TRUE;
256     ListEntry      *entry = NULL;
257
258         // Iterate from head (arbitrary choice)
259     index = f_head;
260     while ((index != NULL) && (keep_going)) {
261         keep_going = (*fn) (this, index, usr_def);
262         if (!(keep_going))
263             entry = index;
264         else
265             index = index->f_next;
266     }
267     return entry;
268 }
269
270 // //////////////////////////////////////////////////////////////
271 // Private methods
272 // //////////////////////////////////////////////////////////////
273
274 void 
275 OrderList::insertNew(ListEntry *node)
276 {
277         f_head = node;
278         f_tail = node;
279         node->f_prev = NULL;
280         node->f_next = NULL;
281         return;
282 }
283
284 void 
285 OrderList::insertAfter(ListEntry *node)
286 {
287         f_cursor->f_next->f_prev = node;
288         node->f_next = f_cursor->f_next;
289         f_cursor->f_next = node;
290         node->f_prev = f_cursor;
291         return;
292 }
293
294 void 
295 OrderList::insertBefore(ListEntry *node)
296 {
297         f_cursor->f_prev->f_next = node;
298         node->f_prev = f_cursor->f_prev;
299         f_cursor->f_prev = node;
300         node->f_next = f_cursor;
301         return;
302 }
303
304 void 
305 OrderList::insertTail(ListEntry *node)
306 {
307         ListEntry          *tnode;
308
309         tnode = f_tail;
310         f_tail = node;
311         tnode->f_next = node;
312         node->f_prev = tnode;
313         node->f_next = NULL;
314         return;
315 }
316
317 void 
318 OrderList::insertHead(ListEntry *node)
319 {
320         ListEntry          *tnode;
321
322         tnode = f_head;
323         f_head = node;
324         tnode->f_prev = node;
325         node->f_next = tnode;
326         node->f_prev = NULL;
327         return;
328 }
329
330