OpenIndiana and Solaris port
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / api / smart_ptr.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: smart_ptr.cc /main/4 1996/06/11 17:11:33 cde-hal $
25  *
26  * Copyright (c) 1992 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
52 #include "api/smart_ptr.h"
53 #include "object/compressed_pstring.h"
54
55
56 smart_ptr::smart_ptr(info_lib* lib_ptr,
57                      const char* ibase_name, 
58                      int composite_pos,
59                      const handler& query_hd,
60                      int index_selector,
61                      composite_tag tag
62                     ) 
63 {
64    info_base* ibase = lib_ptr -> get_info_base(ibase_name);
65
66    if ( ibase == 0 ) {
67        throw(stringException(
68           form("unknown base %s in %s\n", ibase_name, 
69                lib_ptr -> get_info_lib_path()
70             )));
71    }
72 //debug(cerr, ibase -> num_of_docs());
73
74    switch (tag) {
75      case SET:
76       {
77       cset_handlerPtr set_ptr = ibase -> get_set(composite_pos);
78
79       if ( set_ptr == 0 )
80          throw(stringException("NULL set ptr"));
81
82       _init((*set_ptr)->get_first_oid(query_hd, index_selector),
83             set_ptr -> its_store()
84            );
85       }
86       break;
87      case LIST:
88       {
89       dl_list_handlerPtr list_ptr = ibase -> get_list(composite_pos);
90
91       if ( list_ptr == 0 )
92          throw(stringException("NULL set ptr"));
93
94       _init((*list_ptr)->get_first_oid(query_hd, index_selector),
95             list_ptr -> its_store()
96            );
97       }
98       break;
99      default:
100       throw(stringException("unknown tag"));
101    }
102
103 }
104
105 smart_ptr::smart_ptr(info_base* ibase, 
106                      int composite_pos,
107                      const handler& query_hd,
108                      int index_selector,
109                      composite_tag tag
110                     ) 
111 {
112    switch (tag) {
113      case SET:
114       {
115       cset_handlerPtr set_ptr = ibase -> get_set(composite_pos);
116
117       if ( set_ptr == 0 )
118          throw(stringException("NULL set ptr"));
119
120       _init((*set_ptr)->get_first_oid(query_hd, index_selector),
121             set_ptr -> its_store()
122            );
123       }
124       break;
125      case LIST:
126       {
127       dl_list_handlerPtr list_ptr = ibase -> get_list(composite_pos);
128
129       if ( list_ptr == 0 )
130          throw(stringException("NULL set ptr"));
131
132       _init((*list_ptr)->get_first_oid(query_hd, index_selector),
133             list_ptr -> its_store()
134            );
135       }
136       break;
137      default:
138       throw(stringException("unknown tag"));
139    }
140
141 }
142
143 /* inlined */
144 /*
145 smart_ptr::smart_ptr()
146 {
147 }
148
149 smart_ptr::~smart_ptr()
150 {
151 }
152 */
153
154 void smart_ptr::_init(const oid_t& id, const abs_storage* x)
155 {
156 //MESSAGE(cerr, "smart_ptr::_init()");
157    obj_id.become(id);
158 //obj_id.asciiOut(cerr); cerr << "\n";
159
160    store = (abs_storage*)x;
161    obj_ptr = 0;
162
163    handler::operator->(); 
164
165 //obj_id.asciiOut(cerr); cerr << "\n";
166 //debug(cerr, int(obj_ptr));
167 //MESSAGE(cerr, "smart_ptr::_init() done");
168 }
169
170    
171 handler* smart_ptr::get_handler(int i, c_code_t code)
172 {
173    handler* x =  _get_component(i); 
174    x -> operator->();                      
175
176    if ( x -> its_oid().ccode() != code )   
177        throw(stringException("invalid node data class code"));
178
179    return x;
180 }
181
182 int smart_ptr::get_int(int i)
183 {
184    handler* x = get_handler(i, INTEGER_CODE);
185    int y= (*(integer_handler*)x) -> get();
186    delete x;
187
188    return y;
189 }
190
191 const char* smart_ptr::get_string(int i, buffer& buf)
192 {
193   handler* x = _get_component(i);
194
195   x -> operator->(); // this will bring the its_oid field up-to-date
196
197   char* str;
198
199   switch ( x -> its_oid().ccode() ) {
200      case STRING_CODE:
201        {
202        str = (*(pstring_handler*)x) -> get(buf);
203        break;
204        }
205      case COMPRESSED_STRING_CODE:
206        {
207        str = (*(compressed_pstring_handler*)x) -> get(buf);
208        break;
209        }
210
211      default:
212        throw(stringException("invalid node data class code"));
213   }
214
215   delete x;
216
217   return str;
218 }
219
220 const char* smart_ptr::get_string(int i)
221 {
222   handler* x = _get_component(i);
223
224   x -> operator->(); // this will bring the its_oid field up-to-date
225
226   char* str;
227
228   switch ( x -> its_oid().ccode() ) {
229      case STRING_CODE:
230        {
231        str = (*(pstring_handler*)x) -> get();
232        break;
233        }
234      case COMPRESSED_STRING_CODE:
235        {
236        str = (*(compressed_pstring_handler*)x) -> get();
237        break;
238        }
239
240      default:
241        throw(stringException("invalid node data class code"));
242   }
243
244   delete x;
245
246   return str;
247 }
248
249
250 int smart_ptr::get_string_size(int i)
251 {
252   handler* x = _get_component(i);
253   x -> operator->(); // this will bring the its_oid field up-to-date
254
255   int y ;
256
257   switch ( x -> its_oid().ccode() ) {
258      case STRING_CODE:
259        y = (*(pstring_handler*)x) -> size();
260        break;
261
262      case COMPRESSED_STRING_CODE:
263        y = (*(compressed_pstring_handler*)x) -> size();
264        break;
265
266      default:
267        throw(stringException("invalid node data class code"));
268   }
269
270   delete x;
271
272   return y;
273 }
274
275 oid_t smart_ptr::get_oid(int i)
276 {
277    handler* x = get_handler(i, OID_CODE);
278    oid_t y = (*(oid_handler*)x) -> my_coid();
279    delete x;
280
281    return y;
282 }
283
284 void smart_ptr::update_oid(int i, const oid_t& x)
285 {
286    static char buf[64];
287
288    handler* z = get_handler(i, OID_CODE);
289    oid_handler* y = (oid_handler*)z;
290
291    snprintf(buf, sizeof(buf), "%d.%d\n", x.ccode(), (int)x.icode());
292    istringstream in(buf);
293
294    (*y) -> asciiIn(in);
295
296    delete y;
297 }
298
299 void smart_ptr::update_string(int i, istream& in)
300 {
301   handler* x = _get_component(i);
302
303   x -> operator->(); // this will bring the its_oid field up-to-date
304
305   io_status ok;
306   switch ( x -> its_oid().ccode() ) {
307      case STRING_CODE:
308        ok =(*(pstring_handler*)x) -> asciiIn(in);
309        break;
310
311      case COMPRESSED_STRING_CODE:
312        ok =(*(compressed_pstring_handler*)x) -> _asciiIn(in);
313        break;
314
315      default:
316        throw(stringException("invalid node data class code"));
317   }
318
319   if(ok) { ; }
320   delete x;
321 }
322
323 void smart_ptr::update_string(int i, const char* buf, int size)
324 {
325   handler* x = _get_component(i);
326
327   x -> operator->(); // this will bring the its_oid field up-to-date
328
329   switch ( x -> its_oid().ccode() ) {
330      case STRING_CODE:
331        (*(pstring_handler*)x) -> update(buf, size);
332        break;
333
334      case COMPRESSED_STRING_CODE:
335        (*(compressed_pstring_handler*)x) -> _asciiIn(buf, size);
336        break;
337
338      default:
339        throw(stringException("invalid node data class code"));
340   }
341   delete x;
342 }
343