Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / object / oid_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: oid_list.cc /main/5 1996/07/18 14:43:28 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 "object/oid_list.h"
52
53 void oid_list::init_persistent_info(persistent_info* x)
54 {
55    ostring* u = list_ptr.p; 
56
57    root::init_persistent_info(x);
58
59    if ( get_mode(OLD_OBJECT) == false ) {
60
61 //MESSAGE(cerr, "oid_list::init_persistent_info");
62 //debug(cerr, (void*)this);
63 //debug(cerr, my_oid());
64
65      char* w = 0;
66      int   v= 0;
67
68 //MESSAGE(cerr, "oid_list::init_persistent_info(), new object case");
69 //debug(cerr, (void*)this);
70 //debug(cerr, (void*)list_ptr.p);
71
72      if (u) {
73        w = u -> get();
74        v = u -> size();
75      } 
76
77      if ( storage_ptr )
78        storage_ptr -> insertString(list_ptr.loc, w, v);
79
80
81 //debug(cerr, list_ptr.loc);
82 //debug(cerr, v_sz);
83    }
84
85    delete u;
86 }
87
88 oid_list::oid_list(c_code_t c_id) : composite(c_id),
89    f_internal_index(false), chk_index(0)
90 {
91    list_ptr.loc = 0;
92    init_data_member(0);
93 }
94
95 oid_list::oid_list(int num_oids, c_code_t c_id) : composite(c_id),
96    f_internal_index(false), chk_index(0)
97 {
98    list_ptr.loc = 0;
99    init_data_member(num_oids);
100 }
101
102 oid_list::oid_list(oid_list& x) : 
103    composite(x), chk_index(0)
104 {
105    list_ptr.loc = 0;
106    init_data_member(x.v_sz);
107
108    for ( unsigned int i=1; i<=v_sz; i++ )
109       update_component(i, x(i));
110 }
111
112 oid_list::~oid_list()
113 {
114    if ( get_mode(PERSISTENT) == false ) {
115       delete list_ptr.p;
116    }
117
118    delete chk_index;
119 }
120
121 void oid_list::init_data_member(int leng) 
122 {
123    int char_leng = leng * OID_T_SZ ;
124    char* ptr = new char[char_leng];
125
126    oid_t x(ground);
127   
128    char* y = ptr;
129    for ( int i=0; i<leng; i++ ) {
130       x.to_char_string(y, false);
131       y += OID_T_SZ;
132    }
133
134    list_ptr.loc = 0; // to please purify
135    list_ptr.p = new ostring(ptr, char_leng); 
136
137 //MESSAGE(cerr, "oid_list::init_data_member(int leng)");
138 //debug(cerr, (void*)this);
139 //debug(cerr, (void*)list_ptr.p);
140
141    delete ptr;
142    v_sz = leng;
143    set_mode(UPDATE, true);
144 }
145
146 void oid_list::build_internal_index() 
147 {
148    if ( get_mode(OLD_OBJECT) == true &&
149         get_mode(PERSISTENT) == true &&
150         v_sz > 1000 
151       ) 
152       chk_index = new chunks_index(storage_ptr, list_ptr.loc);
153    else
154       chk_index = 0;
155 }
156
157 void oid_list::reqest_build_internal_index() 
158 {
159    f_internal_index = true;
160 }
161
162 Boolean oid_list::expand_space(int extra_oids) 
163 {
164 /*
165 MESSAGE(cerr, "in expand_space()");
166 debug(cerr, my_oid());
167 debug(cerr, extra_oids);
168 debug(cerr, v_sz);
169 debug(cerr, int(list_ptr.loc));
170 debug(cerr, int(&list_ptr.loc));
171 */
172
173    Boolean ok;
174
175    int extra_char_leng = extra_oids * OID_T_SZ;
176
177    if ( get_mode(PERSISTENT) == true ) {
178
179       int total_char_leng = extra_char_leng + v_sz * OID_T_SZ;
180
181       char* ptr = new char[total_char_leng];
182
183       memset(ptr, (char)0, total_char_leng);
184
185       if ( list_ptr.loc == 0  ) {
186          storage_ptr -> insertString(list_ptr.loc, ptr, total_char_leng);
187       } else {
188          storage_ptr -> appendString(list_ptr.loc, ptr, total_char_leng);
189       }
190
191       delete ptr;
192
193       ok = true;
194
195    } else {
196       ok = list_ptr.p -> expand( extra_char_leng );
197    }
198
199    v_sz += extra_oids;
200
201    set_mode(UPDATE, true);
202
203    return ok;
204 }
205
206
207 io_status oid_list::asciiOut(ostream& out) 
208 {
209    if ( !out ) 
210       return fail;
211
212    out << v_sz << '\n';
213
214    int ind = first();
215
216    while (ind ) {
217
218       oid_t x = (*this)(ind);
219
220       if ( x.eq(ground) == false ) {
221          x.asciiOut(out);
222          out << "\n";
223       }
224     
225      next(ind);
226    }
227
228    return done;
229 }
230
231 oid_t oid_list::operator()(int ind) 
232 {
233 /*
234 MESSAGE(cerr, "oid_list::operator()");
235 debug(cerr, my_oid());
236
237 debug(cerr, ind);
238 debug(cerr, v_sz);
239 debug(cerr, int(this));
240 debug(cerr, int(list_ptr.p));
241 */
242
243    if ( !INRANGE(ind, 1, (int) v_sz) ) {
244       MESSAGE(cerr, "oid_list::opeartor(): out of range");
245       throw(boundaryException(1, v_sz, ind));
246    }
247
248    char z[OID_T_SZ];
249    int offset = (ind - 1) * OID_T_SZ; 
250
251    if ( get_mode(PERSISTENT) == true ) 
252    {
253
254       if ( list_ptr.loc == 0 )
255          throw(stringException("zero loc value"));
256
257       if ( f_internal_index == true && chk_index == 0 ) {
258          build_internal_index();
259       }
260
261       if ( chk_index ) {
262          str_index_record_t* anchor = 
263             chk_index -> chunk_location(offset);
264
265          storage_ptr ->
266             readString(anchor -> loc, z, OID_T_SZ, 
267                        offset - anchor -> str_offset);
268
269       } else
270          storage_ptr ->
271                readString(list_ptr.loc, z, OID_T_SZ, offset);
272
273    } else {
274
275       if ( list_ptr.p == 0 )
276          throw(stringException("zero list_ptr.p value"));
277
278       memcpy(z, list_ptr.p -> get() + offset, OID_T_SZ);
279
280    }
281 //MESSAGE(cerr, "oid_list::operator() done");
282
283 #ifdef PORTABLE_DB
284    return oid_t(z, false, swap_order());
285 #else
286    return oid_t(z, false, false);
287 #endif
288
289 }
290
291 Boolean 
292 oid_list::insert_component(const oid_t& new_comp_oid)
293 {
294 /*
295 MESSAGE(cerr, "oid_list::insert_component");
296 debug(cerr, v_sz);
297 debug(cerr, (void*)this);
298 debug(cerr, int(list_ptr.p));
299 new_comp_oid.asciiOut(cerr); cerr << "\n";
300 */
301
302    char z[OID_T_SZ];
303    new_comp_oid.to_char_string(z, swap_order());
304
305    if ( get_mode(PERSISTENT) == true ) {
306
307       if ( list_ptr.loc == 0 ) {
308
309          storage_ptr ->
310               insertString(list_ptr.loc, z, OID_T_SZ);
311
312       } else {
313          storage_ptr ->
314                appendString(list_ptr.loc, z, OID_T_SZ);
315       }
316
317    } else {
318       list_ptr.p -> append(z, OID_T_SZ);
319    } 
320
321    v_sz++;
322
323    set_mode(UPDATE, true);
324
325    return true;
326 }
327
328 Boolean 
329 oid_list::update_component(int index, const oid_t& new_oid)
330 {
331 //MESSAGE(cerr, "oid_list::update_component()");
332 //debug(cerr, my_oid());
333    if ( !INRANGE(index, 1, (int) v_sz) ) {
334       MESSAGE(cerr, "oid_list update(): out of range");
335       throw(boundaryException(1, v_sz, index));
336    }
337
338    char z[OID_T_SZ];
339
340    Boolean swap;
341
342    if ( get_mode(PERSISTENT) == true && swap_order() == true )
343       swap = true;
344    else
345       swap = false;
346
347    new_oid.to_char_string(z, swap);
348
349    if ( get_mode(PERSISTENT) == true ) {
350
351       if ( list_ptr.loc == 0 )
352          throw(stringException("zero list_ptr.p value"));
353
354       storage_ptr ->
355          updateString(list_ptr.loc, z,
356                       OID_T_SZ, (index-1) * OID_T_SZ
357                      );
358
359    } else {
360
361 //MESSAGE(cerr, "oid_list::update_component(): vm update");
362
363       if ( list_ptr.p == 0 )
364          throw(stringException("zero list_ptr.p value"));
365
366       list_ptr.p -> update(z, OID_T_SZ, (index-1)*OID_T_SZ);
367
368    } 
369 //MESSAGE(cerr, "oid_list::update_component() done");
370    return true;
371 }
372
373 io_status oid_list::asciiIn(istream& in) 
374 {
375 //MESSAGE(cerr, "oid_list::asciiIn()");
376    if ( in.get() != LIST_MARK_CHAR ) 
377       throw(formatException("should be a LIST_MARK_CHAR"));
378
379    if ( in.get() != '\n' ) 
380       throw(formatException("should be a \n"));
381
382    dlist temp_list;
383    dlist_void_ptr_cell* y = 0;
384
385    char c;
386    while ( (c = in.get()) != LIST_MARK_CHAR ) {
387
388       in.putback(c);
389
390       oid_t *x = new oid_t(ground);
391       x -> asciiIn(in);
392       y = new dlist_void_ptr_cell(x);
393
394       temp_list.insert_as_tail(y);
395
396       v_sz++;
397    }
398
399    if ( in.get() != '\n' ) {
400       throw(formatException("bad oid_list input stream"));
401    }
402
403    int oid_array_sz = v_sz * OID_T_SZ;
404    char *oid_array = new char[oid_array_sz];
405
406    long ind = temp_list.first();
407
408    int i = 0;
409
410    char z[OID_T_SZ];
411
412    while ( ind ) {
413       y = (dlist_void_ptr_cell*)(ind);
414
415       ((oid_t*)(y -> void_ptr())) -> 
416          to_char_string(z, swap_order());
417
418       delete ((oid_t*)(y -> void_ptr()));
419
420       memcpy(oid_array + i * OID_T_SZ, z, OID_T_SZ);
421    
422       i++;
423       temp_list.next(ind);
424    }
425  
426 // save the list
427    if ( get_mode(PERSISTENT) == true ) {
428
429       if ( list_ptr.loc == 0 )
430          storage_ptr -> insertString(list_ptr.loc, oid_array, oid_array_sz);
431       else
432          storage_ptr -> updateString(list_ptr.loc, oid_array, oid_array_sz);
433
434       set_mode(UPDATE, true);
435
436    } else {
437       delete list_ptr.p;
438       list_ptr.p = new ostring(0);
439       list_ptr.p -> set(oid_array, v_sz);
440    }
441
442    delete oid_array;
443
444    return done;
445 }
446
447 Boolean oid_list::remove_component(const oid_t& x)
448 {
449    int ind = first();
450    while ( ind ) {
451    
452       if ( (*this)(ind).eq(x) == true ) {
453
454          update_component(ind, ground);
455
456          set_mode(UPDATE, true);
457
458          return true;
459       }
460
461      next(ind);
462    }
463    return false;
464 }
465
466
467
468 int oid_list::cdr_sizeof()
469 {
470    return composite::cdr_sizeof() + sizeof(list_ptr.loc);
471 }
472
473 io_status oid_list::cdrOut(buffer& buf)
474 {
475 /*
476 MESSAGE(cerr, "oid_list::cdrOut()");
477 debug(cerr, (void*)this);
478 debug(cerr, (void*)list_ptr.loc);
479 debug(cerr, my_oid());
480 */
481    composite::cdrOut(buf);
482    buf.put(list_ptr.loc);
483    return done;
484 }
485
486 io_status oid_list::cdrIn(buffer& buf)
487 {
488 //MESSAGE(cerr, "oid_list::cdrIn()");
489 //debug(cerr, (void*)this);
490    composite::cdrIn(buf);
491    buf.get(list_ptr.loc);
492 //debug(cerr, (void*)list_ptr.loc);
493 //debug(cerr, my_oid());
494    return done;
495 }
496
497 MMDB_BODIES(oid_list)
498
499 oid_list_handler::oid_list_handler(int num_oids, storagePtr store):
500 handler(OID_LIST_CODE, store)
501 {
502    ((oid_list*)obj_ptr) -> init_data_member(num_oids);
503 }
504
505 oid_list_handler::oid_list_handler(const oid_t& v_oid, storagePtr store):
506 handler(v_oid, store)
507 {
508 }
509
510 oid_list_handler::~oid_list_handler()
511 {
512 }
513
514 oid_list* oid_list_handler::operator ->()
515 {
516    return (oid_list*)handler::operator->();
517 }
518