Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / object / dl_list.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: dl_list.cc /main/5 1996/06/11 17:24:12 cde-hal $
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 "object/dl_list.h"
52
53 dl_list::dl_list(c_code_t c_cd) : composite(c_cd)
54 {
55    v_num_indices = 0;
56    v_indices = 0;
57
58    set_mode(SWAP_ALLOWED, false);
59 }
60
61 dl_list::dl_list(const dl_list& x) : composite(x)
62 {
63    throw(stringException("dl_list(const dl_list&) not done yet"));
64 }
65
66 dl_list::~dl_list()
67 {
68 /*
69 MESSAGE(cerr, "in ~dl_list()");
70 debug(cerr, v_num_indices);
71 */
72
73    delete v_indices;
74 }
75
76 void dl_list::init_data_member(c_index_handlerPtr* index_arrray,
77                             int iptrs
78                            )
79 {
80    v_indices = index_arrray;
81    v_num_indices = iptrs;
82
83    set_mode(HEALTH, true);
84
85 #ifdef MEMORY_MAPPED
86    set_mode(UPDATE, true);
87 #endif
88 }
89
90 //
91 //Boolean dl_list::value_LS(root& x, Boolean safe) const
92 //{
93 //   if ( safe == true &&
94 //        ( my_oid().ccode() != DL_LIST_CODE ||
95 //          x.my_oid().ccode() != DL_LIST_CODE  
96 //        ) 
97 //      )
98 //      return false;
99 //
100 //   dl_list &y = *(dl_list*)&x;
101 //
102 //   if ( sz != y.sz )
103 //      return false;
104 //
105 ///************************************************/
106 //// Note: the components are indexed from 1 to sz
107 ///************************************************/
108 //
109 //   for ( int i=1; i<=sz; i++ ) {
110 //
111 //      handler* rootPtr1 = get_component(i);
112 //      handler* rootPtr2 = y.get_component(i);
113 //
114 //      if ( (*rootPtr1) -> value_LS( *rootPtr2 ) == true ) {
115 //         return true;
116 //      }
117 //
118 //      if ( (*rootPtr1) -> value_EQ( *rootPtr2 ) == false ) {
119 //         return false;
120 //      }
121 //   }
122 //
123 //   return false;
124 //}
125 //
126 //Boolean dl_list::value_EQ(root& x, Boolean safe) const
127 //{
128 //   if ( safe == true &&
129 //        ( my_oid().ccode() != DL_LIST_CODE ||
130 //          x.my_oid().ccode() != DL_LIST_CODE  
131 //        ) 
132 //      )
133 //      return false;
134 //
135 //   dl_list &y = *(dl_list*)&x;
136 //
137 //   if ( sz != y.sz )
138 //      return false;
139 //
140 ///************************************************/
141 //// Note: the components are index from 1 to sz
142 ///************************************************/
143 //
144 //   for ( int i=1; i<=sz; i++ ) {
145 //
146 //      handler* rootPtr1 = get_component(i);
147 //      handler* rootPtr2 = y.get_component(i);
148 //
149 //      if ( (*rootPtr1) -> value_EQ( *rootPtr2 ) == false ) {
150 //         return false;
151 //      }
152 //   }
153 //
154 //   return true;
155 //}
156
157
158 handler* dl_list::get_component(int index) 
159 {
160    if ( v_sz == 0 ) 
161       return 0;
162
163    const oid_t* cell_ptr = &v_dl_list_head;
164
165    for ( int i=1; i<index-1; i++ ) {
166
167        if ( cell_ptr == 0 || cell_ptr -> icode() == 0 ) 
168           throw(stringException("broken chain"));
169
170        dl_list_cell_handler cell(*cell_ptr, storage_ptr);
171
172
173        cell_ptr = &cell -> get_forward_ptr();
174
175    }
176
177    return new handler (*cell_ptr, storage_ptr);
178 }
179
180 oid_list_handler* dl_list::get_locs(handler& query, int index)
181 {
182    if ( !INRANGE(index, 0, (int) v_num_indices-1) )
183       throw(boundaryException(0, v_num_indices-1, index));
184
185    if ( v_indices[index] == 0 ) 
186       throw(stringException("NULL index handler ptr"));
187
188    return (*v_indices[index]) -> get_loc_list(query);
189 }
190
191 oid_t dl_list::first_cell_oid() const 
192 {
193    return v_dl_list_head;
194 }
195
196 oid_t dl_list::last_cell_oid() const 
197 {
198    return v_dl_list_tail;
199 }
200
201
202 Boolean 
203 dl_list::insert_before(dl_list_cell_handler& new_cell,
204                        dl_list_cell_handler& old_cell   
205                       ) 
206 {
207    oid_t prev_oid(old_cell -> get_backward_ptr());
208
209    new_cell -> set_forward_ptr(old_cell.its_oid());
210    old_cell -> set_backward_ptr(new_cell.its_oid());
211
212    if ( prev_oid.icode() != 0 ) {
213
214 // NOTE: non-new-list-head case
215       dl_list_cell_handler prev_cell_hd(prev_oid, storage_ptr);
216
217       new_cell -> set_backward_ptr(prev_cell_hd.its_oid());
218       prev_cell_hd -> set_forward_ptr(new_cell.its_oid());
219
220    } else {
221
222 // NOTE: new-list-head case
223       v_dl_list_head.become(new_cell.its_oid());
224
225    }
226
227    v_sz++;
228    set_mode(HEALTH, true);
229
230    insert_to_indices(new_cell);
231
232    return true;
233 }
234
235 Boolean 
236 dl_list::insert_after(dl_list_cell_handler& new_cell,
237                       dl_list_cell_handler& old_cell   
238                       ) 
239 {
240    oid_t next_oid(old_cell -> get_forward_ptr());
241
242    new_cell -> set_backward_ptr(old_cell.its_oid());
243    old_cell -> set_forward_ptr(new_cell.its_oid());
244
245    if ( next_oid.icode() != 0 ) {
246
247 // NOTE: non-new-list-tail case
248       dl_list_cell_handler next_cell_hd(next_oid, storage_ptr);
249
250       new_cell -> set_forward_ptr(next_cell_hd.its_oid());
251       next_cell_hd -> set_backward_ptr(new_cell.its_oid());
252
253    } else {
254
255 // NOTE: new-list-tail case
256       new_cell -> get_forward_ptr().become(ground);
257       v_dl_list_tail.become(new_cell.its_oid());
258
259    }
260
261    v_sz++;
262    set_mode(HEALTH, true);
263
264    insert_to_indices(new_cell);
265
266    return true;
267 }
268
269 Boolean 
270 dl_list::insert_as_head(const dl_list_cell_handler& new_cell)
271 {
272 #ifdef A16_BROWSER
273    dl_list_cell_handler* z = 0;
274 #endif
275
276    if ( v_dl_list_head.icode() != 0 ) {
277
278       dl_list_cell_handler first_cell(v_dl_list_head, 
279                                       storage_ptr
280                                      );
281
282 #ifdef A16_BROWSER
283       z = (dl_list_cell_handler*)&new_cell;
284       first_cell -> set_backward_ptr(z -> its_oid());
285 #else
286       first_cell -> set_backward_ptr(new_cell.its_oid());
287 #endif
288
289    } 
290
291    ((dl_list_cell_handler&)new_cell) -> set_forward_ptr(v_dl_list_head);
292
293 #ifdef A16_BROWSER
294    v_dl_list_head.become(z -> its_oid());
295 #else
296    v_dl_list_head.become(new_cell.its_oid());
297 #endif
298
299    if ( v_dl_list_tail.icode() == 0 )
300       v_dl_list_tail.become(v_dl_list_head);
301
302    v_sz++;
303    set_mode(HEALTH, true);
304
305    insert_to_indices(new_cell);
306
307    return true;
308 }
309
310 Boolean 
311 dl_list::insert_as_tail(dl_list_cell_handler& new_cell)
312 {
313    if ( v_dl_list_tail.icode() != 0 ) {
314
315       dl_list_cell_handler last_cell(v_dl_list_tail, 
316                                      storage_ptr
317                                      );
318
319       last_cell -> set_forward_ptr(new_cell.its_oid());
320    } 
321
322    new_cell -> set_backward_ptr(v_dl_list_tail);
323    v_dl_list_tail.become(new_cell.its_oid());
324
325    if ( v_dl_list_head.icode() == 0 )
326       v_dl_list_head.become(v_dl_list_tail);
327
328    v_sz++;
329    set_mode(HEALTH, true);
330
331    new_cell -> set_mode(UPDATE, true);
332
333    insert_to_indices(new_cell);
334
335    return true;
336 }
337
338 io_status dl_list::asciiOut(ostream& out) 
339 {
340 /*   
341 MESSAGE(cerr, "HEAD");
342 v_dl_list_head.asciiOut(out); cerr << "\n";
343
344 MESSAGE(cerr, "TAIL");
345 v_dl_list_tail.asciiOut(out); cerr << "\n";
346 */
347    out << "OID_T:\n";
348    my_oid().asciiOut(out); 
349    out << "\n";
350
351    if ( v_sz == 0 ) return done;
352
353    oid_t* cell_ptr = &v_dl_list_head;
354
355    for ( unsigned int i=1; i<=v_sz; i++ ) {
356
357        if ( cell_ptr == 0 ) {
358           throw(stringException("broken chain"));
359        }
360
361        if ( cell_ptr -> icode() == 0 ) {
362           debug(cerr, i);
363           debug(cerr, v_sz);
364           throw(stringException("dl_list::get_component(): broken chain"));
365        }
366
367        dl_list_cell_handler cell(*cell_ptr, storage_ptr);
368
369
370        cell -> asciiOut(cerr); cerr << "\n";
371        cell_ptr = &cell -> get_forward_ptr();
372
373    }
374
375    return done;
376 }
377
378 void dl_list::batch_index_begin()
379 {
380    for ( unsigned int i=0; i<v_num_indices; i++ ) {
381       if ( v_indices[i] != 0 ) {
382         (*v_indices[i]) -> batch_index_begin();
383       }
384    }
385 }
386
387 void dl_list::batch_index_end()
388 {
389    for ( unsigned int i=0; i<v_num_indices; i++ ) {
390       if ( v_indices[i] != 0 ) {
391          (*v_indices[i]) -> batch_index_end();
392       }
393    }
394 }
395
396 io_status dl_list::asciiIn(istream& in)
397 {
398    batch_index_begin();
399    io_status ok = batch_asciiIn(in);
400    batch_index_end();
401    return ok;
402 }
403
404 io_status dl_list::batch_asciiIn(istream& in)
405 {
406    char ccode_buf[LBUFSIZ];
407    int c;
408    handler* hd_ptr = 0;
409
410    while ( (c = in.get()) != EOF ) {
411
412       in.putback(c);
413
414       in.getline(ccode_buf, LBUFSIZ);
415       char* x = strrchr(ccode_buf, 'L');
416       if ( x ) *x = 0;
417
418       int ccode; 
419       sscanf(ccode_buf, "%u", &ccode);
420
421       hd_ptr = new handler(ccode, storage_ptr);
422
423
424       if ( filter::assigned() == false )
425          (*hd_ptr) -> asciiIn(in);
426       else
427          (*hd_ptr) -> asciiIn(filter::filter_func()(in));
428
429 /*
430 MESSAGE(cerr, "dllist: ID read in");
431 hd_ptr -> its_oid().asciiOut(cerr); cerr << "\n";
432 */
433
434
435       insert_as_tail(*(dl_list_cell_handler*)hd_ptr) ;
436  
437       delete hd_ptr;
438    }
439    return done;
440 }
441
442 Boolean 
443 dl_list::insert_to_indices(const dl_list_cell_handler& new_object)
444 {
445 #ifdef A16_BROWSER
446    dl_list_cell_handler* z = (dl_list_cell_handler*)&new_object;
447 #endif
448
449    if ( v_indices[0] != 0 ) {
450
451 #ifdef A16_BROWSER
452       (*v_indices[0]) -> insert_key_loc(new_object, z -> its_oid());
453 #else
454       (*v_indices[0]) -> insert_key_loc(new_object, new_object.its_oid());
455 #endif
456
457    }
458
459    for ( unsigned int i = 1; i < v_num_indices; i++ ) {
460
461       if ( v_indices[i] == 0 ) continue;
462
463       handler* y = (*(composite_handler*)&new_object) 
464                     -> get_component(i);
465
466 /*
467 MESSAGE(cerr, "dlist: comp id:");
468 y -> its_oid().asciiOut(cerr); cerr << "\n";
469 MESSAGE(cerr, "obj id: (its_oid and my_oid)");
470 new_object.its_oid().asciiOut(cerr); cerr << "\n";
471 (*(dl_list_cell_handler*)&new_object) -> my_oid().asciiOut(cerr); cerr << "\n";
472 */
473
474
475       if ( y == 0 ) continue;
476
477 // A16 backward compatible
478 #ifdef A16_BROWSER
479       (*v_indices[i]) -> insert_key_loc(*y, z -> its_oid());
480 #else
481       (*v_indices[i]) -> insert_key_loc(*y, new_object.its_oid());
482 #endif
483
484 //debug(cerr, y -> its_oid());
485 //debug(cerr, new_object.its_oid());
486     
487        if ( !(y -> its_oid() == new_object.its_oid()) )
488           delete y;
489    }
490    set_mode(HEALTH, true);
491
492    return true;
493 }
494
495
496 int dl_list::cdr_sizeof()
497 {
498    return composite::cdr_sizeof() + 
499           v_dl_list_head.cdr_sizeof() +
500           v_dl_list_tail.cdr_sizeof() +
501           sizeof(v_num_indices);
502 }
503
504 io_status dl_list::cdrOut(buffer& buf)
505 {
506    composite::cdrOut(buf);
507    v_dl_list_head.cdrOut(buf);
508    v_dl_list_tail.cdrOut(buf);
509    buf.put(v_num_indices);
510    return done;
511 }
512
513 io_status dl_list::cdrIn(buffer& buf)
514 {
515    composite::cdrIn(buf);
516    v_dl_list_head.cdrIn(buf);
517    v_dl_list_tail.cdrIn(buf);
518    buf.get(v_num_indices);
519    return done;
520 }
521
522 oid_t dl_list::get_first_oid(const handler& query, int index)
523 {
524    if ( !INRANGE(index, 0, (int) v_num_indices-1) ) {
525        MESSAGE(cerr, "cset::get_first_oid(): invalid index");
526        throw(boundaryException(0, v_num_indices-1, index));
527    }
528
529    if ( v_indices[index] == 0 ) {
530        throw(stringException("cset::get_first_oid(): NULL index ptr"));
531    }
532
533    return (*v_indices[index]) -> first_of_invlist(query);
534 }
535
536 void dl_list::commit()
537 {
538    for ( unsigned int i = 1; i < v_num_indices; i++ )
539    {
540       if ( v_indices[i] ) 
541         v_indices[i] -> commit() ;
542    }
543 }
544
545
546 MMDB_BODIES(dl_list)
547 HANDLER_BODIES(dl_list)