Add GNU LGPL headers to all .c .C and .h files
[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 <stream.h>
57 #endif
58
59    
60 dlist::dlist(Boolean x) : 
61         v_ct(0), v_head(0), v_tail(0), remove_cells_when_done(x)
62 {
63 }
64
65 dlist::~dlist() 
66 {
67    if ( remove_cells_when_done == true )
68       remove();
69 }
70
71 void dlist::remove() 
72 {
73    long ind = first();
74    while ( ind ) {
75       dlist_cell *p = (dlist_cell*)ind;
76       next(ind);
77       delete p;
78    }
79 }
80
81 void dlist::append(dlist* tail_list)
82 {
83    if ( tail_list == 0 ) return;
84
85    if ( v_tail != 0 )
86       v_tail -> v_succ = tail_list -> v_head;
87
88    if ( tail_list -> v_head )
89       tail_list -> v_head -> v_pred = v_tail;
90
91    if ( v_head == 0 )
92       v_head = tail_list -> v_head;
93
94    v_tail = tail_list -> v_tail;
95    v_ct += tail_list -> v_ct;
96
97    tail_list -> v_head = tail_list -> v_tail = 0;
98    tail_list -> v_ct = 0;
99 }
100
101 void dlist::insert_before(dlist_cell* ref, dlist_cell* x) 
102 {
103 /*
104 debug(cerr, "insert before");
105 debug(cerr, int(x));
106 debug(cerr, int(head));
107 debug(cerr, int(tail));
108 */
109    if ( v_head == 0 ) {
110       x -> v_pred = x -> v_succ = 0;
111       v_head = v_tail = x;
112    } else
113    if ( ref == v_head ) {
114       x -> v_succ = v_head ;
115       x -> v_pred = 0;
116       v_head = x;
117       ref -> v_pred = x;
118    } else {
119       ref -> v_pred -> v_succ = x;
120       x -> v_pred = ref -> v_pred;
121       x -> v_succ = ref;
122       ref -> v_pred = x;
123    }
124    v_ct++;
125 /*
126 debug(cerr, "insert before done");
127 debug(cerr, int(head));
128 debug(cerr, int(tail));
129 */
130 }
131
132 void dlist::insert_after(dlist_cell* ref, dlist_cell* x) 
133 {
134    if ( v_head == 0 ) {
135       x -> v_pred = x -> v_succ = 0;
136       v_head = v_tail = x;
137    } else
138    if ( ref == v_tail ) {
139       ref -> v_succ = x;
140       x -> v_succ = 0;
141       v_tail = x;
142       x -> v_pred = ref;
143    } else {
144       ref -> v_succ -> v_pred = x;
145       x -> v_succ = ref -> v_succ;
146       x -> v_pred = ref;
147       ref -> v_succ = x;
148    }
149    v_ct++;
150 }
151
152 void dlist::insert_as_head(dlist_cell* x)
153 {
154    if ( v_head == 0 ) {
155       x -> v_pred = x -> v_succ = 0;
156       v_head = v_tail = x;
157    } else {
158       v_head -> v_pred = x;
159       x -> v_pred = 0;
160       x -> v_succ = v_head;
161       v_head = x;
162    }
163    v_ct++;
164 }
165
166 void dlist::insert_as_tail(dlist_cell* x)
167 {
168    if ( v_head == 0 ) {
169       x -> v_pred = x -> v_succ = 0;
170       v_head = v_tail = x;
171    } else {
172       v_tail -> v_succ = x;
173       x -> v_succ = 0;
174       x -> v_pred = v_tail;
175       v_tail = x;
176    } 
177    v_ct++;
178 }
179
180 void dlist::delete_cell(dlist_cell* x) 
181 {
182 /*
183 MESSAGE(cerr, "delete_cell()");
184 debug(cerr, int(x));
185 debug(cerr, int(head));
186 debug(cerr, int(tail));
187 debug(cerr, ct);
188 */
189
190    if ( x == 0 ) 
191       return ;
192
193    if ( x == v_head ) {
194       if ( v_ct == 1 )
195          v_head = v_tail = 0;
196       else {
197          v_head = v_head -> v_succ;
198          v_head -> v_pred = 0;
199       } 
200    } else
201       if ( x == v_tail ) {
202          v_tail = v_tail -> v_pred;
203          v_tail -> v_succ = 0;
204       } else {
205          x -> v_pred -> v_succ = x -> v_succ ;
206          x -> v_succ -> v_pred = x -> v_pred ;
207       } 
208
209    v_ct--;
210
211    x -> v_succ = x -> v_pred = 0;
212 }
213
214 long dlist::first()
215 {
216    return long(v_head);
217 }
218
219 long dlist::last()
220 {
221    return long (v_tail);
222 }
223
224 void dlist::next(long& index)
225 {
226    if ( index == long(v_tail) )
227       index = 0;
228    else
229       index = long( ((dlist_cell*)(index)) -> v_succ );
230 }
231