Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / dstr / dlist.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  * $XConsortium: dlist.C /main/5 1996/08/21 15:51:48 drk $
25  *
26  * Copyright (c) 1993 HAL Computer Systems International, Ltd.
27  * All rights reserved.  Unpublished -- rights reserved under
28  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
29  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
30  * OR DISCLOSURE.
31  * 
32  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
34  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
36  * INTERNATIONAL, LTD.
37  * 
38  *                         RESTRICTED RIGHTS LEGEND
39  * Use, duplication, or disclosure by the Government is subject
40  * to the restrictions as set forth in subparagraph (c)(l)(ii)
41  * of the Rights in Technical Data and Computer Software clause
42  * at DFARS 252.227-7013.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49
50
51 #include "dstr/dlist.h"
52
53 #ifdef C_API
54 #include "utility/c_stream.h"
55 #else
56 #include <sstream>
57 using namespace std;
58 #endif
59
60    
61 dlist::dlist(Boolean x) : 
62         v_ct(0), v_head(0), v_tail(0), remove_cells_when_done(x)
63 {
64 }
65
66 dlist::~dlist() 
67 {
68    if ( remove_cells_when_done == true )
69       remove();
70 }
71
72 void dlist::remove() 
73 {
74    long ind = first();
75    while ( ind ) {
76       dlist_cell *p = (dlist_cell*)ind;
77       next(ind);
78       delete p;
79    }
80 }
81
82 void dlist::append(dlist* tail_list)
83 {
84    if ( tail_list == 0 ) return;
85
86    if ( v_tail != 0 )
87       v_tail -> v_succ = tail_list -> v_head;
88
89    if ( tail_list -> v_head )
90       tail_list -> v_head -> v_pred = v_tail;
91
92    if ( v_head == 0 )
93       v_head = tail_list -> v_head;
94
95    v_tail = tail_list -> v_tail;
96    v_ct += tail_list -> v_ct;
97
98    tail_list -> v_head = tail_list -> v_tail = 0;
99    tail_list -> v_ct = 0;
100 }
101
102 void dlist::insert_before(dlist_cell* ref, dlist_cell* x) 
103 {
104 /*
105 debug(cerr, "insert before");
106 debug(cerr, int(x));
107 debug(cerr, int(head));
108 debug(cerr, int(tail));
109 */
110    if ( v_head == 0 ) {
111       x -> v_pred = x -> v_succ = 0;
112       v_head = v_tail = x;
113    } else
114    if ( ref == v_head ) {
115       x -> v_succ = v_head ;
116       x -> v_pred = 0;
117       v_head = x;
118       ref -> v_pred = x;
119    } else {
120       ref -> v_pred -> v_succ = x;
121       x -> v_pred = ref -> v_pred;
122       x -> v_succ = ref;
123       ref -> v_pred = x;
124    }
125    v_ct++;
126 /*
127 debug(cerr, "insert before done");
128 debug(cerr, int(head));
129 debug(cerr, int(tail));
130 */
131 }
132
133 void dlist::insert_after(dlist_cell* ref, dlist_cell* x) 
134 {
135    if ( v_head == 0 ) {
136       x -> v_pred = x -> v_succ = 0;
137       v_head = v_tail = x;
138    } else
139    if ( ref == v_tail ) {
140       ref -> v_succ = x;
141       x -> v_succ = 0;
142       v_tail = x;
143       x -> v_pred = ref;
144    } else {
145       ref -> v_succ -> v_pred = x;
146       x -> v_succ = ref -> v_succ;
147       x -> v_pred = ref;
148       ref -> v_succ = x;
149    }
150    v_ct++;
151 }
152
153 void dlist::insert_as_head(dlist_cell* x)
154 {
155    if ( v_head == 0 ) {
156       x -> v_pred = x -> v_succ = 0;
157       v_head = v_tail = x;
158    } else {
159       v_head -> v_pred = x;
160       x -> v_pred = 0;
161       x -> v_succ = v_head;
162       v_head = x;
163    }
164    v_ct++;
165 }
166
167 void dlist::insert_as_tail(dlist_cell* x)
168 {
169    if ( v_head == 0 ) {
170       x -> v_pred = x -> v_succ = 0;
171       v_head = v_tail = x;
172    } else {
173       v_tail -> v_succ = x;
174       x -> v_succ = 0;
175       x -> v_pred = v_tail;
176       v_tail = x;
177    } 
178    v_ct++;
179 }
180
181 void dlist::delete_cell(dlist_cell* x) 
182 {
183 /*
184 MESSAGE(cerr, "delete_cell()");
185 debug(cerr, int(x));
186 debug(cerr, int(head));
187 debug(cerr, int(tail));
188 debug(cerr, ct);
189 */
190
191    if ( x == 0 ) 
192       return ;
193
194    if ( x == v_head ) {
195       if ( v_ct == 1 )
196          v_head = v_tail = 0;
197       else {
198          v_head = v_head -> v_succ;
199          v_head -> v_pred = 0;
200       } 
201    } else
202       if ( x == v_tail ) {
203          v_tail = v_tail -> v_pred;
204          v_tail -> v_succ = 0;
205       } else {
206          x -> v_pred -> v_succ = x -> v_succ ;
207          x -> v_succ -> v_pred = x -> v_pred ;
208       } 
209
210    v_ct--;
211
212    x -> v_succ = x -> v_pred = 0;
213 }
214
215 long dlist::first()
216 {
217    return long(v_head);
218 }
219
220 long dlist::last()
221 {
222    return long (v_tail);
223 }
224
225 void dlist::next(long& index)
226 {
227    if ( index == long(v_tail) )
228       index = 0;
229    else
230       index = long( ((dlist_cell*)(index)) -> v_succ );
231 }
232