123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- /*
- * CDE - Common Desktop Environment
- *
- * Copyright (c) 1993-2012, The Open Group. All rights reserved.
- *
- * These libraries and programs are free software; you can
- * redistribute them and/or modify them under the terms of the GNU
- * Lesser General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * These libraries and programs are distributed in the hope that
- * they will be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with these libraries and programs; if not, write
- * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
- * Floor, Boston, MA 02110-1301 USA
- */
- /*
- * $TOG: buffer.C /main/9 1998/04/17 11:50:56 mgreess $
- *
- * Copyright (c) 1992 HaL Computer Systems, Inc. All rights reserved.
- * UNPUBLISHED -- rights reserved under the Copyright Laws of the United
- * States. Use of a copyright notice is precautionary only and does not
- * imply publication or disclosure.
- *
- * This software contains confidential information and trade secrets of HaL
- * Computer Systems, Inc. Use, disclosure, or reproduction is prohibited
- * without the prior express written permission of HaL Computer Systems, Inc.
- *
- * RESTRICTED RIGHTS LEGEND
- * Use, duplication, or disclosure by the Government is subject to
- * restrictions as set forth in subparagraph (c)(l)(ii) of the Rights in
- * Technical Data and Computer Software clause at DFARS 252.227-7013.
- * HaL Computer Systems, Inc.
- * 1315 Dell Avenue, Campbell, CA 95008
- *
- */
- #include "utility/buffer.h"
- /***********************************************************/
- // Constructor
- /***********************************************************/
- /*
- buffer::buffer(const int sz)
- {
- _alloc(sz);
- }
- */
- buffer::buffer(buffer& original): v_swap_order(original.v_swap_order)
- {
- _alloc(original.v_bufsz);
- memcpy(v_aptr, original.v_aptr, original.v_bufsz);
- }
- void buffer::_alloc(const int sz)
- {
- int void_ptr_size = sizeof(voidPtr);
- v_bufsz = sz;
- if ( sz > 0 ) {
- v_base = ::new char[void_ptr_size+sz];
- memset(v_base, (char)0, void_ptr_size+sz);
- v_align_offset = int(void_ptr_size - long(v_base) % void_ptr_size);
- if (v_align_offset != void_ptr_size)
- v_base += v_align_offset;
- else
- v_align_offset = 0;
- v_allocated = 1;
- } else {
- v_base = 0;
- v_allocated = 0;
- }
- v_aptr = v_eptr = v_base;
- }
- /***********************************************************/
- // Destructor
- /***********************************************************/
- buffer::~buffer()
- {
- if ( v_allocated == 1 ) {
- free((char*)(v_base-v_align_offset));
- }
- }
- void buffer::reset()
- {
- v_eptr = v_base;
- v_aptr = v_base;
- memset(v_base, (char)0, v_bufsz);
- }
-
- /*
- void buffer::set_chunk(char* ptr, const int sz)
- {
- bufsz = sz;
- eptr = aptr = base = ptr;
- allocated = 0;
- }
- void buffer::set_content_sz(const int sz)
- {
- eptr = base + sz;
- }
- */
- /***********************************************************/
- // Expand buffer chunk to newsz.
- /***********************************************************/
- int buffer::expand_chunk(const int newsz)
- {
- if ( v_allocated == 0 )
- throw(stringException("expand a buffer with reference memory"));
- int cursz = buf_sz();
- if ( newsz <= cursz ) {
- v_bufsz = newsz;
- return 0;
- }
- int void_ptr_size = sizeof(voidPtr);
- //MESSAGE(cerr, "real expand");
- //char* x = (char*)malloc(void_ptr_size+newsz);
- char* x = ::new char [void_ptr_size+newsz];
- int delta = int(void_ptr_size - long(x) % void_ptr_size);
- x += delta ;
- memcpy(x, v_base, cursz);
- memset(x+cursz, char(0), newsz - cursz);
- v_aptr -= long(v_base);
- v_eptr -= long(v_base);
- //free((char*)(v_base-v_align_offset));
- delete (char*)(v_base-v_align_offset);
- v_align_offset = delta;
- v_base = x;
- v_aptr += long(v_base);
- v_eptr += long(v_base);
- v_bufsz = newsz;
- return 0;
- }
- #define CASTBNDEXCEPT
- /***********************************************************/
- // Get sz chars to the array x. x is supposed allocated
- /***********************************************************/
- buffer& buffer::get(char *x, int sz)
- {
- if ( sz + v_aptr > v_eptr ) {
- MESSAGE(cerr, "buffer::get(): underflow");
- throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sz + v_aptr) ));
- }
- memcpy(x, v_aptr, sz);
- v_aptr += sz;
- return *this;
- }
- /***********************************************************/
- // skip i chars.
- /***********************************************************/
- buffer& buffer::skip(int i)
- {
- if ( i + v_aptr > v_eptr )
- MESSAGE(cerr, "buffer::skip(): underflow");
- throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(i + v_aptr) ));
- v_aptr += i;
- return *this;
- }
- /***********************************************************/
- // Get a char to y.
- /***********************************************************/
- buffer& buffer::get(char& y)
- {
- if ( v_aptr > v_eptr) {
- MESSAGE(cerr, "buffer::get(char&): underflow");
- throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(1+v_aptr)));
- }
- y = *v_aptr;
- v_aptr++;
- return *this;
- }
- buffer& buffer::getusc(int& y)
- {
- y = *(unsigned char*)v_aptr;
- v_aptr++;
- return *this;
- }
- /***********************************************************/
- // Get an integer to y. option can be ASCII or BINARY.
- /***********************************************************/
- buffer& buffer::get(int& y)
- {
- unsigned int x;
- get(x);
- y = (int)x;
- return *this;
- }
- buffer& buffer::get(unsigned int& y)
- {
- if ( v_aptr + sizeof(y) > v_eptr ) {
- MESSAGE(cerr, "buffer::get(int&): underflow");
- throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(unsigned)+v_aptr)));
- }
- get((char*)&y, sizeof(y));
- #ifdef PORTABLE_DB
- if ( v_swap_order == true )
- ORDER_SWAP_UINT(y);
- #endif
- return *this;
- }
- /***********************************************************/
- // Get a long to y. option can be ASCII or BINARY.
- /***********************************************************/
- buffer& buffer::get(long& y)
- {
- //MESSAGE(cerr, "WARNING: buffer::get(long& y) +++++++++++++++++++++++");
- if ( v_aptr + sizeof(y) > v_eptr ) {
- MESSAGE(cerr, "buffer::get(long&): underflow");
- throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(long)+v_aptr)));
- }
- get((char*)&y, sizeof(y));
- #ifdef PORTABLE_DB
- if ( v_swap_order == true )
- ORDER_SWAP_LONG(y);
- #endif
- return *this;
- }
- /***********************************************************/
- // Get a float to y.
- /***********************************************************/
- buffer& buffer::get(float& y)
- {
- if ( v_aptr + sizeof(y) > v_eptr ) {
- MESSAGE(cerr, "buffer::get(float &): underflow");
- throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(float)+v_aptr)));
- }
- get((char*)&y, sizeof(y));
- #ifdef PORTABLE_DB
- if ( v_swap_order == true )
- ORDER_SWAP_FLOAT(y);
- #endif
- return *this;
- }
- buffer& buffer::get(unsigned short& y)
- {
- if ( v_aptr + sizeof(y) > v_eptr ) {
- MESSAGE(cerr, "buffer::get(float &): underflow");
- throw ( CASTBNDEXCEPT boundaryException( long(v_aptr), long(v_eptr), long(sizeof(float)+v_aptr)));
- }
- get((char*)&y, sizeof(y));
- #ifdef PORTABLE_DB
- if ( v_swap_order == true )
- ORDER_SWAP_USHORT(y);
- #endif
- return *this;
- }
- /***********************************************************/
- // Put a char to buffer.
- /***********************************************************/
- buffer& buffer::put(const char content, Boolean exp_buf)
- {
- //return put((char*)&content, sizeof(content));
- if ( (int) v_bufsz == content_sz() )
- {
- if ( exp_buf == true )
- expand_chunk(v_bufsz + 10);
- else {
- MESSAGE( cerr, "buffer::put(const char): overflow");
- throw ( CASTBNDEXCEPT boundaryException(content_sz(), v_bufsz, 1) );
- }
- }
- *v_eptr = content;
- v_eptr++;
- return *this;
- }
- buffer& buffer::put(const unsigned char content, Boolean exp_buf)
- {
- return put((char*)&content, sizeof(content), exp_buf);
- }
- /***********************************************************/
- // Put a unsigned int to buffer.
- /***********************************************************/
- buffer& buffer::put(const int content, Boolean exp_buf)
- {
- return put((unsigned int)content, exp_buf);
- }
- buffer& buffer::put(const unsigned int content, Boolean exp_buf)
- {
- #ifdef PORTABLE_DB
- if ( v_swap_order == true )
- ORDER_SWAP_UINT(content);
- #endif
- return put((char*)&content, sizeof(content), exp_buf);
- }
- /***********************************************************/
- // Put a long to buffer.
- /***********************************************************/
- buffer& buffer::put(const long content, Boolean exp_buf)
- {
- //MESSAGE(cerr, "WARNING: buffer::put(long& y) =====================");
- #ifdef PORTABLE_DB
- if ( v_swap_order == true )
- ORDER_SWAP_LONG(content);
- #endif
- return put((char*)&content, sizeof(content), exp_buf);
- }
- /***********************************************************/
- // Put a unsigned short to buffer.
- /***********************************************************/
- buffer& buffer::put(const unsigned short content, Boolean exp_buf)
- {
- #ifdef PORTABLE_DB
- if ( v_swap_order == true )
- ORDER_SWAP_USHORT(content);
- #endif
- return put((char*)&content, sizeof(content), exp_buf);
- }
- /***********************************************************/
- // Put a float to buffer.
- /***********************************************************/
- buffer& buffer::put(const float content, Boolean exp_buf)
- {
- #ifdef PORTABLE_DB
- if ( v_swap_order == true )
- ORDER_SWAP_FLOAT(content);
- #endif
- return put((char*)&content, sizeof(content), exp_buf);
- }
- /***********************************************************/
- // Put sz chars to buffer.
- /***********************************************************/
- buffer& buffer::put(const char* content, int sz, Boolean exp_buf)
- {
- if ( sz > (int)(v_bufsz - content_sz()) ) {
- if ( exp_buf == true )
- expand_chunk(v_bufsz + sz);
- else {
- MESSAGE( cerr, "buffer::put(char*, int): overflow");
- throw ( CASTBNDEXCEPT boundaryException(content_sz(), v_bufsz, sz) );
- }
- }
-
- //memcpy(v_eptr, content, sz);
- //debug(cerr, int(v_base));
- //debug(cerr, int(v_eptr));
- for ( int i=0; i<sz; i++ ) {
- v_eptr[i] = content[i];
- //debug(cerr, int(v_eptr[i]));
- }
- v_eptr += sz;
- return *this;
- }
- Boolean operator ==(buffer&x, buffer& y)
- {
- if ( x.content_sz() != y.content_sz() ) {
- debug(cerr, x.content_sz());
- debug(cerr, y.content_sz());
- }
- unsigned char* x_buf = (unsigned char*)x.get_base();
- unsigned char* y_buf = (unsigned char*)y.get_base();
- for ( int i=0; i<x.content_sz(); i++ ) {
- if ( x_buf[i] != y_buf[i] ) {
- debug(cerr, i);
- debug(cerr, x_buf[i]);
- debug(cerr, y_buf[i]);
- //return false;
- }
- }
- return true;
- }
- /***********************************************************/
- // show content
- /***********************************************************/
- ostream& operator<<(ostream& s, buffer& b)
- {
- //debug(s, b.bufsz);
- //debug(s, b.content_sz());
- //debug(s, b.base);
- //debug(s, b.eptr);
- //debug(s, b.aptr);
- int x = b.v_eptr - b.v_base ;
- int i;
- for ( i = 0; i < x; i++ ) {
- s << b.v_base[i];
- /*
- if ( isprint(b.v_base[i]) )
- cout << b.v_base[i];
- else
- cout << int(b.v_base[i]);
- */
- }
- MESSAGE(cerr, "buffer=");
- for ( i = 0; i < x; i++ ) {
- cout << int(b.v_base[i]) << " ";
- }
- cout << endl;
- return s;
- }
- /*
- void buffer::cdrIn(buffer& buf)
- {
- buf.get(v_bufsz);
- buf.get(v_base, v_bufsz);
- }
- void buffer::cdrOut(buffer& buf)
- {
- buf.put(v_bufsz);
- buf.put(v_base, v_bufsz);
- }
- */
|