Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / dti_cc / CC_Listbase.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 librararies 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 /*
24  * $TOG: CC_Listbase.C /main/4 1998/04/17 11:44:10 mgreess $
25  * $TOG: CC_Listbase.C /main/4 1998/04/17 11:44:10 mgreess $
26  * $TOG: CC_Listbase.C /main/4 1998/04/17 11:44:10 mgreess $
27  * $TOG: CC_Listbase.C /main/4 1998/04/17 11:44:10 mgreess $
28  *
29  * Copyright (c) 1993 HAL Computer Systems International, Ltd.
30  * All rights reserved.  Unpublished -- rights reserved under
31  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
32  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
33  * OR DISCLOSURE.
34  * 
35  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
36  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
37  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
38  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
39  * INTERNATIONAL, LTD.
40  * 
41  *                         RESTRICTED RIGHTS LEGEND
42  * Use, duplication, or disclosure by the Government is subject
43  * to the restrictions as set forth in subparagraph (c)(l)(ii)
44  * of the Rights in Technical Data and Computer Software clause
45  * at DFARS 252.227-7013.
46  *
47  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
48  *                  1315 Dell Avenue
49  *                  Campbell, CA  95008
50  * 
51  */
52 #include "Exceptions.hh"
53 #include "cc_exceptions.h"
54 #include "CC_Listbase.h"
55
56 // /////////////////////////////////////////////////////////////////
57 // CC_Listbase::insert - append a new link to the end of the list
58 // /////////////////////////////////////////////////////////////////
59
60 void
61 CC_Listbase::insert (CC_Link_base *element)
62 {
63   if (!element) {
64     throw(CASTCCEXCEPT ccException());
65   }
66
67   if (!f_tail) {
68     f_tail = f_head = element;
69   }
70   else {
71     f_tail->f_next = element;
72     element->f_prev = f_tail;
73     f_tail = element;
74   }
75
76   f_length++;
77 }
78
79
80 // /////////////////////////////////////////////////////////////////
81 // CC_Listbase::insert - insert a new link into the list
82 // /////////////////////////////////////////////////////////////////
83
84 void
85 CC_Listbase::prepend (CC_Link_base *element)
86 {
87
88   if ( !element ) {
89     throw(CASTCCEXCEPT ccException());
90   }
91
92   if ( !f_head ) {
93     f_head = element;
94   }
95   else {
96     element->f_next = f_head;
97     f_head->f_prev = element;
98     f_head = element;
99   }
100
101   if (f_tail == NULL)
102     f_tail = element;
103   f_length++;
104 }
105
106
107
108 // /////////////////////////////////////////////////////////////////
109 // CC_Listbase::remove - remove element pointed to by iterator
110 // /////////////////////////////////////////////////////////////////
111 CC_Link_base *
112 CC_Listbase::remove (CC_List_Iterator_base &iterator)
113 {
114   // Make sure the iterator points to this this.
115   if (iterator.f_list != this)
116     throw (CASTCCEXCEPT ccException());
117
118   // Make sure the iterator is pointing to an element. 
119   if (iterator.f_current == NULL)
120     return(NULL);
121
122   // NOTE: If two iterators are active in the list at the same time
123   // it is possible to blow away an element that another iterator
124   // is pointing at (either previous or current).  We could make this
125   // safer by only marking elements as deleted for now, then actually
126   // delete the items when there are no more iterators pointing at it.
127   //    19:41 22-Jul-93 DJB 
128
129   // Link around the link we're removing.
130   if (iterator.f_previous != NULL) {
131     iterator.f_previous->f_next = iterator.f_current->f_next;
132     if ( f_tail != iterator.f_current ) { 
133       iterator.f_current->f_next->f_prev  = iterator.f_previous;
134     }
135   }
136   else {   // must be at the head 
137     f_head = iterator.f_current->f_next;
138     if ( f_head ) {
139       f_head->f_prev = NULL;
140     }
141   }
142   
143   if (iterator.f_current == f_tail) {
144     f_tail = iterator.f_previous;
145     if ( f_tail ) { 
146       f_tail->f_next = NULL;
147     }
148     
149   }
150
151   // Increment the iterator. 
152   CC_Link_base *entry = iterator.f_current;
153   iterator.f_current = iterator.f_current->f_next;
154
155   f_length--;
156
157   return (entry);
158 }
159
160 CC_Link_base *
161 CC_Listbase::removeFirst()
162 {
163   if ( f_head ) {
164
165     CC_Link_base *remove_item = f_head;
166     f_head = f_head->f_next;
167     remove_item->f_next = NULL;
168
169     if ( f_head ) {
170       f_head->f_prev = NULL;
171     }
172
173     if ( f_tail == remove_item ) { /* only one item on the list */
174       f_tail = NULL;
175     }
176
177     f_length--;
178     return(remove_item);
179   }
180
181   return(NULL);
182 }
183
184 //------------------------------------------------------------------
185 CC_Link_base *
186 CC_Listbase::removeLast()
187 {
188   if ( f_tail ) {
189     CC_Link_base *remove_item = f_tail;
190     f_tail = f_tail->f_prev;
191     remove_item->f_prev = NULL;
192
193     if ( f_tail ) {
194       f_tail->f_next = NULL;
195     }
196     if ( f_head == remove_item ) { // one item left on the list
197       f_head = NULL;
198     }
199     f_length--;
200     return(remove_item);
201   }
202
203   return(NULL);
204 }
205
206 // /////////////////////////////////////////////////////////////////
207 // CC_List_Iterator::CC_List_Iterator - class constructor
208 // /////////////////////////////////////////////////////////////////
209
210 CC_List_Iterator_base::CC_List_Iterator_base (CC_Listbase *list)
211 : f_list (list)
212 {
213   if ( !list ) {
214     throw(CASTCCEXCEPT ccException());
215   }
216
217   reset();
218 }
219
220 // /////////////////////////////////////////////////////////////////
221 // CC_List_Iterator::reset - reset the iterator to the list start
222 // /////////////////////////////////////////////////////////////////
223
224 void
225 CC_List_Iterator_base::reset()
226 {
227   f_current = f_previous = NULL;
228 }
229
230
231 // /////////////////////////////////////////////////////////////////
232 // CC_List_Iterator:: operator ++ - increment the list iterator
233 // /////////////////////////////////////////////////////////////////
234
235 CC_Boolean 
236 CC_List_Iterator_base::operator++()
237 {
238   if (!f_current) { // havn't touched the first element 
239     f_current = f_list->first();
240     f_previous = NULL;
241     if ( f_current ) return ( TRUE );
242     else return(FALSE);
243   }
244
245   f_previous = f_current;
246   f_current = f_current->f_next;
247
248   if ( f_current ) {
249     return(TRUE);
250   }
251   else {
252     return(FALSE);
253   }
254 }      
255
256 CC_Boolean
257 CC_List_Iterator_base::operator--()
258 {
259   if (!f_current) {
260     return(FALSE);
261   }
262
263   f_current = f_previous;
264   if ( f_previous ) {
265     f_previous = f_previous->f_prev;
266   }
267
268   if ( f_current ) { return(TRUE); }
269   else { return(FALSE); }
270 }
271