Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / utility / buffer.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  * $TOG: buffer.C /main/9 1998/04/17 11:50:56 mgreess $
25  *
26  * Copyright (c) 1992 HaL Computer Systems, Inc.  All rights reserved.
27  * UNPUBLISHED -- rights reserved under the Copyright Laws of the United
28  * States.  Use of a copyright notice is precautionary only and does not
29  * imply publication or disclosure.
30  * 
31  * This software contains confidential information and trade secrets of HaL
32  * Computer Systems, Inc.  Use, disclosure, or reproduction is prohibited
33  * without the prior express written permission of HaL Computer Systems, Inc.
34  * 
35  *                         RESTRICTED RIGHTS LEGEND
36  * Use, duplication, or disclosure by the Government is subject to
37  * restrictions as set forth in subparagraph (c)(l)(ii) of the Rights in
38  * Technical Data and Computer Software clause at DFARS 252.227-7013.
39  *                        HaL Computer Systems, Inc.
40  *                  1315 Dell Avenue, Campbell, CA  95008
41  * 
42  */
43
44
45 #include "utility/buffer.h"
46
47 /***********************************************************/
48 // Constructor
49 /***********************************************************/
50 /*
51 buffer::buffer(const int sz)
52 {
53    _alloc(sz);
54 }
55 */
56
57 buffer::buffer(buffer& original): v_swap_order(original.v_swap_order)
58 {
59    _alloc(original.v_bufsz);
60    memcpy(v_aptr, original.v_aptr, original.v_bufsz);
61 }
62
63 void buffer::_alloc(const int sz)
64 {
65    int void_ptr_size = sizeof(voidPtr);
66
67    v_bufsz = sz;
68    if ( sz > 0 ) {
69
70       v_base = ::new char[void_ptr_size+sz];
71
72       memset(v_base, (char)0, void_ptr_size+sz);
73
74       v_align_offset = int(void_ptr_size - long(v_base) % void_ptr_size);
75       if (v_align_offset != void_ptr_size)
76         v_base += v_align_offset;
77       else
78         v_align_offset = 0;
79
80       v_allocated = 1;
81
82    } else {
83       v_base = 0;
84       v_allocated = 0;
85    }
86    v_aptr = v_eptr = v_base;
87 }
88
89
90 /***********************************************************/
91 // Destructor
92 /***********************************************************/
93 buffer::~buffer()
94 {
95    if ( v_allocated == 1 ) {
96      free((char*)(v_base-v_align_offset));
97    }
98 }
99
100 void buffer::reset()
101 {
102    v_eptr = v_base;
103    v_aptr = v_base;
104    memset(v_base, (char)0, v_bufsz);
105 }
106    
107 /*
108 void buffer::set_chunk(char* ptr, const int sz)
109 {
110    bufsz = sz;
111    eptr = aptr = base = ptr;
112    allocated = 0;
113 }
114
115 void buffer::set_content_sz(const int sz)
116 {
117    eptr = base + sz;
118 }
119 */
120
121 /***********************************************************/
122 // Expand buffer chunk to newsz.
123 /***********************************************************/
124 int buffer::expand_chunk(const int newsz)
125 {
126    if ( v_allocated == 0 )
127       throw(stringException("expand a buffer with reference memory"));
128
129    int cursz = buf_sz();
130
131    if ( newsz <= cursz ) {
132       v_bufsz = newsz;
133       return 0;
134    }
135
136    int void_ptr_size = sizeof(voidPtr);
137
138 //MESSAGE(cerr, "real expand");
139
140    //char* x = (char*)malloc(void_ptr_size+newsz);
141    char* x = ::new char [void_ptr_size+newsz];
142
143    int delta = int(void_ptr_size - long(x) % void_ptr_size);
144    x += delta ;
145
146    memcpy(x, v_base, cursz);
147
148    memset(x+cursz, char(0), newsz - cursz);
149
150    v_aptr -= long(v_base);
151    v_eptr -= long(v_base);
152
153    //free((char*)(v_base-v_align_offset));
154    delete (char*)(v_base-v_align_offset);
155    v_align_offset = delta;
156
157    v_base = x;
158    v_aptr += long(v_base);
159    v_eptr += long(v_base);
160    v_bufsz = newsz;
161
162    return 0;
163 }
164
165 #define CASTBNDEXCEPT
166
167 /***********************************************************/
168 // Get sz chars to the array x. x is supposed allocated
169 /***********************************************************/
170 buffer& buffer::get(char *x, int sz)
171 {
172    if ( sz + v_aptr > v_eptr ) {
173       MESSAGE(cerr, "buffer::get(): underflow");
174       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sz + v_aptr) ));
175    }
176
177    memcpy(x, v_aptr, sz);
178    v_aptr += sz;
179    return *this;
180 }
181
182 /***********************************************************/
183 // skip i chars.
184 /***********************************************************/
185 buffer& buffer::skip(int i)
186 {
187    if ( i + v_aptr > v_eptr ) 
188       MESSAGE(cerr, "buffer::skip(): underflow");
189       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(i + v_aptr) ));
190
191    v_aptr += i;
192    return *this;
193 }
194
195
196 /***********************************************************/
197 // Get a char to y.
198 /***********************************************************/
199 buffer& buffer::get(char& y)
200 {
201    if ( v_aptr > v_eptr) {
202       MESSAGE(cerr, "buffer::get(char&): underflow");
203       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(1+v_aptr)));
204    }
205
206    y = *v_aptr;
207    v_aptr++;
208    return *this;
209 }
210
211 buffer& buffer::getusc(int& y)
212 {
213    y = *(unsigned char*)v_aptr;
214    v_aptr++;
215    return *this;
216 }
217
218 /***********************************************************/
219 // Get an integer to y. option can be ASCII or BINARY.
220 /***********************************************************/
221 buffer& buffer::get(int& y)
222 {
223    unsigned int x;
224    get(x);
225    y = (int)x;
226    return *this;
227 }
228
229 buffer& buffer::get(unsigned int& y)
230 {
231    if ( v_aptr + sizeof(y) > v_eptr ) {
232       MESSAGE(cerr, "buffer::get(int&): underflow");
233       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(unsigned)+v_aptr)));
234    }
235
236    get((char*)&y, sizeof(y));
237
238 #ifdef PORTABLE_DB
239    if ( v_swap_order == true )
240       ORDER_SWAP_UINT(y);
241 #endif
242
243    return *this;
244 }
245
246 /***********************************************************/
247 // Get a long to y. option can be ASCII or BINARY.
248 /***********************************************************/
249 buffer& buffer::get(long& y)
250 {
251 //MESSAGE(cerr, "WARNING: buffer::get(long& y) +++++++++++++++++++++++");
252    if ( v_aptr + sizeof(y) > v_eptr ) {
253       MESSAGE(cerr, "buffer::get(long&): underflow");
254       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(long)+v_aptr)));
255    }
256
257    get((char*)&y, sizeof(y));
258
259 #ifdef PORTABLE_DB
260    if ( v_swap_order == true )
261       ORDER_SWAP_LONG(y);
262 #endif
263
264    return *this;
265 }
266
267 /***********************************************************/
268 // Get a float to y. 
269 /***********************************************************/
270 buffer& buffer::get(float& y)
271 {
272    if ( v_aptr + sizeof(y) > v_eptr ) {
273       MESSAGE(cerr, "buffer::get(float &): underflow");
274       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(float)+v_aptr)));
275    }
276
277    get((char*)&y, sizeof(y));
278
279 #ifdef PORTABLE_DB
280    if ( v_swap_order == true )
281       ORDER_SWAP_FLOAT(y);
282 #endif
283
284    return *this;
285 }
286
287 buffer& buffer::get(unsigned short& y)
288 {
289    if ( v_aptr + sizeof(y) > v_eptr ) {
290       MESSAGE(cerr, "buffer::get(float &): underflow");
291       throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(float)+v_aptr)));
292    }
293
294    get((char*)&y, sizeof(y));
295
296 #ifdef PORTABLE_DB
297    if ( v_swap_order == true )
298       ORDER_SWAP_USHORT(y);
299 #endif
300
301    return *this;
302 }
303
304 /***********************************************************/
305 // Put a char to buffer.
306 /***********************************************************/
307 buffer& buffer::put(const char content, Boolean exp_buf) 
308 {
309    //return put((char*)&content, sizeof(content));
310
311    if ( (int) v_bufsz == content_sz() )
312    {
313       if ( exp_buf == true )
314         expand_chunk(v_bufsz + 10);
315       else {
316          MESSAGE( cerr, "buffer::put(const char): overflow");
317          throw ( CASTBNDEXCEPT boundaryException(content_sz(), v_bufsz, 1) );
318       }
319    }
320
321    *v_eptr = content;
322    v_eptr++;
323    return *this;
324 }
325
326 buffer& buffer::put(const unsigned char content, Boolean exp_buf) 
327 {
328    return put((char*)&content, sizeof(content), exp_buf);
329 }
330
331 /***********************************************************/
332 // Put a unsigned int to buffer.
333 /***********************************************************/
334 buffer& buffer::put(const int content, Boolean exp_buf)
335 {
336    return put((unsigned int)content, exp_buf);
337 }
338
339 buffer& buffer::put(const unsigned int content, Boolean exp_buf)
340 {
341 #ifdef PORTABLE_DB
342    if ( v_swap_order == true )
343       ORDER_SWAP_UINT(content);
344 #endif
345
346    return put((char*)&content, sizeof(content), exp_buf);
347 }
348
349 /***********************************************************/
350 // Put a long to buffer.
351 /***********************************************************/
352 buffer& buffer::put(const long content, Boolean exp_buf)
353 {
354 //MESSAGE(cerr, "WARNING: buffer::put(long& y) =====================");
355 #ifdef PORTABLE_DB
356    if ( v_swap_order == true )
357       ORDER_SWAP_LONG(content);
358 #endif
359
360    return put((char*)&content, sizeof(content), exp_buf);
361 }
362
363 /***********************************************************/
364 // Put a unsigned short to buffer.
365 /***********************************************************/
366 buffer& buffer::put(const unsigned short content, Boolean exp_buf)
367 {
368 #ifdef PORTABLE_DB
369    if ( v_swap_order == true )
370       ORDER_SWAP_USHORT(content);
371 #endif
372
373    return put((char*)&content, sizeof(content), exp_buf);
374 }
375
376 /***********************************************************/
377 // Put a float to buffer.
378 /***********************************************************/
379 buffer& buffer::put(const float content, Boolean exp_buf)
380 {
381 #ifdef PORTABLE_DB
382    if ( v_swap_order == true )
383       ORDER_SWAP_FLOAT(content);
384 #endif
385
386    return put((char*)&content, sizeof(content), exp_buf);
387 }
388
389 /***********************************************************/
390 // Put sz chars to buffer.
391 /***********************************************************/
392 buffer& buffer::put(const char* content, int sz, Boolean exp_buf) 
393 {
394    if ( sz > (int)(v_bufsz - content_sz()) ) {
395       if ( exp_buf == true ) 
396          expand_chunk(v_bufsz + sz);
397       else {
398          MESSAGE( cerr, "buffer::put(char*, int): overflow");
399          throw ( CASTBNDEXCEPT boundaryException(content_sz(), v_bufsz, sz) );
400       } 
401    } 
402       
403    //memcpy(v_eptr, content, sz);
404
405 //debug(cerr, int(v_base));
406 //debug(cerr, int(v_eptr));
407
408    for ( int i=0; i<sz; i++ ) {
409      v_eptr[i] = content[i];
410 //debug(cerr, int(v_eptr[i]));
411    }
412
413    v_eptr += sz;
414    return *this;
415 }
416
417 Boolean operator ==(buffer&x, buffer& y)
418 {
419    if ( x.content_sz() != y.content_sz() ) {
420       debug(cerr, x.content_sz());
421       debug(cerr, y.content_sz());
422    }
423
424    unsigned char* x_buf = (unsigned char*)x.get_base();
425    unsigned char* y_buf = (unsigned char*)y.get_base();
426
427    for ( int i=0; i<x.content_sz(); i++ ) {
428        if ( x_buf[i] != y_buf[i] ) {
429           debug(cerr, i);
430           debug(cerr, x_buf[i]);
431           debug(cerr, y_buf[i]);
432           //return false;
433        }
434    }
435
436    return true;
437 }
438
439 /***********************************************************/
440 // show content
441 /***********************************************************/
442
443 ostream& operator<<(ostream& s, buffer& b)
444 {
445
446    //debug(s, b.bufsz);
447    //debug(s, b.content_sz());
448    //debug(s, b.base);
449    //debug(s, b.eptr);
450    //debug(s, b.aptr);
451
452    int x = b.v_eptr - b.v_base ;
453
454    int i;
455    for ( i = 0; i < x; i++ ) {
456       s << b.v_base[i];
457
458 /*
459       if ( isprint(b.v_base[i]) )
460          cout << b.v_base[i];
461       else
462          cout << int(b.v_base[i]);
463 */
464    }
465
466 MESSAGE(cerr, "buffer=");
467    for ( i = 0; i < x; i++ ) {
468       cout << int(b.v_base[i]) << " ";
469    }
470 cout << endl;
471
472    return s;
473 }
474
475 /*
476 void buffer::cdrIn(buffer& buf)
477 {
478    buf.get(v_bufsz);
479    buf.get(v_base, v_bufsz);
480 }
481
482 void buffer::cdrOut(buffer& buf)
483 {
484    buf.put(v_bufsz);
485    buf.put(v_base, v_bufsz);
486 }
487 */
488