Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / object / pstring.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: pstring.C /main/6 1996/09/16 14:26:44 mgreess $
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 #include "object/pstring.h"
52
53 #ifdef C_API
54 buffer* pstring::v_io_buf_ptr;
55 #define v_io_buf (*v_io_buf_ptr)
56 #else
57 buffer pstring::v_io_buf(LBUFSIZ);
58 #endif
59    
60 void pstring::init_persistent_info(persistent_info* x)
61 {
62    root::init_persistent_info(x);
63    if ( get_mode(OLD_OBJECT) == false ) {
64      char* y = 0;
65      if ( storage_ptr )
66        storage_ptr -> insertString(v_str_ptr.loc, y, 0);
67    }
68 }
69
70 pstring::pstring(c_code_t c_id) : primitive(c_id),
71    v_sz(0)
72 {
73    //
74    // CDExc21900
75    // Make sure the entire union is initialized for 64bit architectures.
76    //
77    memset((char*) &v_str_ptr, 0, sizeof(v_str_ptr));
78 }
79
80 pstring::pstring(const char* x, int leng, c_code_t c_id) : 
81 primitive(c_id)
82 {
83    _init(x, leng);
84 }
85
86 pstring::pstring(pstring& x) : primitive(x)
87 {
88    _init((const char*)x.get(), x.v_sz);
89 }
90
91 void pstring::_init(const char* x, int leng) 
92 {
93    v_sz = leng;
94
95    if ( v_sz == 0 ) {
96      v_str_ptr.loc = 0;
97      return;
98    }
99
100    v_str_ptr.p = new char[leng+1]; 
101    memcpy(v_str_ptr.p, x, leng);
102    v_str_ptr.p[leng] = 0;
103 }
104
105 pstring::~pstring()
106 {
107    if ( get_mode(PERSISTENT) == false && NULL != v_str_ptr.p ) 
108       delete v_str_ptr.p;
109 }
110
111
112 // /////////////////////////////////////
113 // should not delete what get() returns!
114 // /////////////////////////////////////
115 char* pstring::get(buffer& string_buffer)
116 {
117 //MESSAGE(cerr, "get()");
118    if ( get_mode(PERSISTENT) == true ) {
119
120 /*
121 MESSAGE(cerr, "persistent");
122 debug(cerr, v_sz);
123 debug(cerr, v_str_ptr.loc);
124 debug(cerr, f_oid);
125 */
126
127       string_buffer.reset();
128       string_buffer.expand_chunk(v_sz+1);
129
130       char* this_v_str_ptr = string_buffer.get_base();
131
132       storage_ptr -> 
133            readString(v_str_ptr.loc, this_v_str_ptr, v_sz);
134
135
136 /*
137 for (int i=0; i<v_sz; i++)
138 {
139   cerr << int(this_v_str_ptr[i]) << " " ;
140 }
141   cerr << "\n";
142 */
143
144
145       this_v_str_ptr[v_sz] = 0;
146
147       string_buffer.set_content_sz(v_sz);
148
149       return this_v_str_ptr;
150
151    } else {
152
153       return v_str_ptr.p;
154
155    }
156 }
157
158
159 /*
160 Boolean pstring::value_LS(root& x, Boolean safe) const
161 {
162    if ( safe == true &&
163         ( f_oid.ccode() != STRING_CODE ||
164           x.my_oid().ccode() != STRING_CODE
165         ) 
166       ) 
167      return false;
168
169    Boolean ok;
170    char* this_v_str_ptr;
171    char* x_v_str_ptr;
172
173    if ( get_mode(PERSISTENT) == true ) {
174       this_v_str_ptr = new char[v_sz+1];
175       storage_ptr -> readString(v_str_ptr.loc, this_v_str_ptr, v_sz);
176    } else
177       this_v_str_ptr = v_str_ptr.p;
178       
179    pstring &casted_x = *(pstring*)&x;
180
181    if ( casted_x.get_mode(PERSISTENT) == true ) {
182       x_v_str_ptr = new char[v_sz+1];
183       storage_ptr -> 
184           readString(casted_x.v_str_ptr.loc, x_v_str_ptr, casted_x.v_sz);
185    } else
186       x_v_str_ptr = casted_x.v_str_ptr.p;
187
188    int min_len = MIN(v_sz, casted_x.v_sz);
189
190    for ( int i=0; i<min_len; i++ ) {
191          if ( this_v_str_ptr[i] < x_v_str_ptr[i] ) {
192             ok = true;
193             break;
194           }
195
196          if ( this_v_str_ptr[i] > x_v_str_ptr[i] ) {
197             ok = false;
198             break;
199           }
200     }
201
202     if ( i == min_len && v_sz < casted_x.v_sz )
203        ok = true;
204     else
205        ok = false;
206
207     if ( get_mode(PERSISTENT) == true )
208        delete this_v_str_ptr;
209
210     if ( casted_x.get_mode(PERSISTENT) == true )
211        delete x_v_str_ptr;
212
213     return ok;
214 }
215
216 Boolean pstring::value_EQ(root& x, Boolean safe) const
217 {
218    if ( safe == true &&
219         ( f_oid.ccode() != STRING_CODE ||
220            x.my_oid().ccode() != STRING_CODE
221         ) 
222       )
223      return false;
224
225    pstring &casted_x = *(pstring*)&x;
226
227    if ( v_sz != casted_x.v_sz )
228      return false;
229
230    Boolean ok;
231    char* this_v_str_ptr;
232    char* x_v_str_ptr;
233
234    if ( get_mode(PERSISTENT) == true ) {
235       this_v_str_ptr = new char[v_sz+1];
236       storage_ptr -> readString(v_str_ptr.loc, this_v_str_ptr, v_sz);
237    } else
238       this_v_str_ptr = v_str_ptr.p;
239       
240    if ( casted_x.get_mode(PERSISTENT) == true ) {
241       x_v_str_ptr = new char[v_sz+1];
242       storage_ptr -> 
243           readString(casted_x.v_str_ptr.loc, x_v_str_ptr, casted_x.v_sz);
244    } else
245       x_v_str_ptr = casted_x.v_str_ptr.p;
246
247    //ok = ( bcmp(this_v_str_ptr, x_v_str_ptr, v_sz) == 0 ) ? true : false;
248    ok = ( memcmp(this_v_str_ptr, x_v_str_ptr, v_sz) == 0 ) ? true : false;
249
250    if ( get_mode(PERSISTENT) == true )
251       delete this_v_str_ptr;
252
253    if ( casted_x.get_mode(PERSISTENT) == true )
254       delete x_v_str_ptr;
255
256     return ok;
257 }
258 */
259
260 io_status pstring::asciiOut(ostream& out) 
261 {
262 /*
263 MESSAGE(cerr, "pstring::asciiOut():");
264 debug(cerr, v_sz);
265 debug(cerr, (void*)this);
266 my_oid().asciiOut(cerr);
267 cerr << "\n";
268 */
269
270    const char* x = get();
271
272    out << v_sz << '\t';
273
274    for ( unsigned int i=0; i<v_sz; i++ )
275       out << x[i];
276
277     return done;
278 }
279
280 io_status pstring::asciiIn(istream& in) 
281 {
282 /*
283 MESSAGE(cerr, "pstring::asciiIn():");
284 debug(cerr, (void*)this);
285 my_oid().asciiOut(cerr);
286 cerr << "\n";
287 */
288
289    _asciiIn(in);
290    pstring::update(v_io_buf.get_base(), v_io_buf.content_sz());
291    return done;
292 }
293
294 io_status pstring::asciiIn(const char* buf, int size) 
295 {
296    v_io_buf.reset();
297    v_io_buf.expand_chunk(size);
298    memcpy(v_io_buf.get_base(), buf, size);
299    v_io_buf.set_content_sz(size);
300    return done;
301 }
302
303 void pstring::_asciiIn(istream& in) 
304 {
305    if ( ! cc_is_digit(in) )
306       throw (stringException("a digit expected"));
307
308    int len;
309    in >> len;
310
311    int tab = in.get(); // expect a '\t'
312
313    if ( tab != '\t' ) {
314       debug(cerr, len);
315       debug(cerr, tab);
316       throw(stringException("'\\t' expected"));
317    }
318
319    v_io_buf.reset();
320    v_io_buf.expand_chunk(len);
321
322    if (  len > 0 && 
323          ( !in.read(v_io_buf.get_base(), len) || in.gcount() != len ) 
324       ) {
325       debug(cerr, len);
326       debug(cerr, v_io_buf.get_base());
327       throw(stringException("pstring::asciiIn(): read failed"));
328    }
329
330    int ret = in.get(); // expect a '\n'
331
332    if ( ret != '\n' ) {
333       debug(cerr, ret);
334       throw(stringException(form("'\\n' expected. %c seen. (count=%d)", ret, len)));
335    }
336
337    v_io_buf.set_content_sz(len);
338 }
339
340 Boolean pstring::update(pstring& new_value)
341 {
342    return update((const char*)new_value.get(), new_value.v_sz);
343 }
344
345 Boolean pstring::update(const char* new_value, int new_value_sz)
346 {
347
348 /*
349 MESSAGE(cerr, "pstring:: update value:");
350 debug(cerr, v_str_ptr.loc);
351 debug(cerr, f_oid);
352 debug(cerr, v_sz);
353
354 debug(cerr, new_value_sz);
355 for (int i=0; i<new_value_sz; i++)
356 {
357   cerr << int(new_value[i]) << " " ;
358 }
359   cerr << "\n";
360 */
361
362
363
364    int old_sz = v_sz;
365    v_sz = new_value_sz;
366
367    if ( get_mode(PERSISTENT) == true ) {
368 //MESSAGE(cerr, "persist case");
369
370       if ( v_str_ptr.loc == 0 ) {
371          storage_ptr -> insertString(v_str_ptr.loc, new_value, v_sz);
372       } else {
373          storage_ptr -> updateString(v_str_ptr.loc, new_value, v_sz); 
374       }
375
376    } else  {
377
378       if ( old_sz < new_value_sz ) {
379          delete v_str_ptr.p;
380          v_str_ptr.p = new char[v_sz+1];
381       } 
382       memcpy(v_str_ptr.p, new_value, v_sz);
383       *(v_str_ptr.p + v_sz) = '\0';
384    }
385
386    set_mode(UPDATE, true);
387
388 //debug(cerr, this -> get());
389
390    return true;
391 }
392
393
394 int pstring::cdr_sizeof()
395 {
396    return primitive::cdr_sizeof() + 
397           sizeof(v_str_ptr.loc) + 
398           sizeof(v_sz);
399 }
400
401 io_status pstring::cdrOut(buffer& buf)
402 {
403    primitive::cdrOut(buf);
404    buf.put(v_str_ptr.loc);
405    buf.put(v_sz);
406    return done;
407 }
408
409 io_status pstring::cdrIn(buffer& buf)
410 {
411    primitive::cdrIn(buf);
412    buf.get(v_str_ptr.loc);
413    buf.get(v_sz);
414    return done;
415 }
416
417 MMDB_BODIES(pstring)
418
419 pstring_handler::pstring_handler(const oid_t& v_oid, storagePtr _store) :
420 handler(v_oid, _store)
421 {
422 }
423
424 pstring_handler::pstring_handler(const char* str, int sz, storagePtr _store) :
425 handler(STRING_CODE, _store)
426 {
427    (this -> operator->()) -> update(str, sz);
428 }
429
430 pstring_handler::~pstring_handler()
431 {
432 }
433
434 pstring* pstring_handler::operator ->()
435 {
436    return (pstring*)handler::operator->();
437 }
438
439