00001
00002
00003
00004
00005
00006
00007 #ifndef BUFFERBASE_H
00008 #define BUFFERBASE_H
00009
00010 #ifdef USE_STD
00011 #include <iostream>
00012 #include <string>
00013 using namespace std;
00014 #else
00015 #include <iostream.h>
00016 #include <string.h>
00017 #endif
00018
00019 #include "utility.h"
00020
00021 class BufferBase
00022 {
00023 public:
00024
00025 virtual string isa() const { return "BufferBase"; }
00026
00027 static const int goodbit = 0x01;
00028 static const int failbit = 0x02;
00029 static const int eobbit = 0x04;
00030 static const int badbit = 0x08;
00031
00032 enum SeekDir{ beg, cur, end };
00033
00034 BufferBase();
00035
00036 BufferBase( const BufferBase& );
00037
00038 virtual ~BufferBase();
00039
00040 virtual BufferBase& operator = ( BufferBase& );
00041
00042 void write( BufferBase& );
00043 BufferBase& operator << ( BufferBase& );
00044 void read( BufferBase& );
00045 BufferBase& operator >> ( BufferBase& );
00046
00047 void write( bytepack& );
00048 BufferBase& operator << ( bytepack& );
00049 void read( bytepack& );
00050 BufferBase& operator >> ( bytepack& );
00051
00052 void read( ostream& );
00053 BufferBase& operator >> ( ostream& );
00054
00055 virtual void seekp( int, SeekDir dir = beg );
00056 virtual void seekg( int, SeekDir dir = beg );
00057
00058 virtual void clear( int state = BufferBase::goodbit );
00059 virtual void setstate( int state );
00060 virtual void clrstate( int state );
00061 virtual int rdstate();
00062 virtual bool good();
00063 virtual bool fail();
00064 virtual bool eob();
00065 virtual bool bad();
00066
00067 virtual int tellp();
00068 virtual int tellg();
00069
00070 virtual void put( char );
00071 virtual void get( char& );
00072 virtual char get();
00073
00074 virtual void put( int, char );
00075 virtual void get( int, char& );
00076 virtual char get( int );
00077
00078 virtual int size();
00079
00080 virtual void erase();
00081
00082 protected:
00083
00084 int _size;
00085 int _putpos;
00086 int _getpos;
00087 int _state;
00088
00089 };
00090
00091 inline
00092 BufferBase::BufferBase()
00093 : _size(0),
00094 _putpos(0), _getpos(0),
00095 _state( BufferBase::goodbit )
00096 {}
00097
00098 inline
00099 BufferBase::BufferBase( const BufferBase& rhs )
00100 : _size( rhs._size ),
00101 _putpos( rhs._putpos ), _getpos( rhs._getpos ),
00102 _state( rhs._state )
00103 {}
00104
00105 inline
00106 BufferBase::~BufferBase() {}
00107
00108 inline
00109 BufferBase& BufferBase::operator = ( BufferBase& rhs )
00110 {
00111 if ( this != &rhs ) {
00112 _size = rhs._size;
00113 _putpos = rhs._putpos;
00114 _getpos = rhs._getpos;
00115 _state = rhs._state;
00116 }
00117 return *this;
00118 }
00119
00120 inline
00121 void BufferBase::write( BufferBase& buffer )
00122 {
00123 buffer.seekg(0);
00124 while ( !buffer.eob() ) { put( buffer.get() ); }
00125 }
00126
00127 inline
00128 BufferBase& BufferBase::operator << ( BufferBase& buffer )
00129 {
00130 write(buffer);
00131 return *this;
00132 }
00133
00134 inline
00135 void BufferBase::read( BufferBase& buffer )
00136 {
00137 seekg(0);
00138 while ( !eob() ) buffer.put( get() );
00139 }
00140
00141 inline
00142 BufferBase& BufferBase::operator >> ( BufferBase& buffer )
00143 {
00144 read(buffer);
00145 return *this;
00146 }
00147
00148 inline
00149 void BufferBase::write( bytepack& bp )
00150 {
00151 for( int i = 0; i < bp.size(); ++i ) put( bp[i] );
00152 }
00153
00154 inline
00155 BufferBase& BufferBase::operator << ( bytepack& bp )
00156 {
00157 write(bp);
00158 return *this;
00159 }
00160
00161 inline
00162 void BufferBase::read( bytepack& bp )
00163 {
00164 for( int i = 0; i < bp.size(); ++i ) bp[i] = get();
00165 }
00166
00167 inline
00168 BufferBase& BufferBase::operator >> ( bytepack& bp )
00169 {
00170 read(bp);
00171 return *this;
00172 }
00173
00174 inline
00175 void BufferBase::read( ostream& os )
00176 {
00177 while ( !eob() ) os.put( get() );
00178 }
00179
00180 inline
00181 BufferBase& BufferBase::operator >> ( ostream& os )
00182 {
00183 read(os);
00184 return *this;
00185 }
00186
00187 inline void BufferBase::clear( int state ) { _state = state; }
00188
00189 inline void BufferBase::setstate( int state ) { _state |= state; }
00190 inline void BufferBase::clrstate( int state ) { _state &= !state; }
00191
00192 inline int BufferBase::rdstate() { return _state; }
00193
00194 inline bool BufferBase::good() { return _state == 1; }
00195 inline bool BufferBase::fail() { return (_state & BufferBase::failbit) != 0; }
00196 inline bool BufferBase::bad() { return (_state & BufferBase::badbit) != 0; }
00197 inline bool BufferBase::eob() { return (_state & BufferBase::eobbit) != 0; }
00198
00199 inline
00200 void BufferBase::seekp( int pos, BufferBase::SeekDir dir )
00201 {
00202 int tmp = pos;
00203
00204 if ( dir == BufferBase::cur ) tmp += _putpos;
00205 if ( dir == BufferBase::end ) tmp += _size;
00206
00207 if ( ( tmp >= 0 ) && ( tmp <= _size ) ) _putpos = tmp;
00208 else setstate( BufferBase::failbit );
00209 }
00210
00211 inline
00212 void BufferBase::seekg( int pos, BufferBase::SeekDir dir )
00213 {
00214 clrstate( BufferBase::eobbit );
00215
00216 int tmp = pos;
00217
00218 if ( dir == BufferBase::cur ) tmp += _getpos;
00219 if ( dir == BufferBase::end ) tmp += _size;
00220
00221 if ( ( tmp >= 0 ) && ( tmp < _size ) ) _getpos = tmp;
00222 else setstate( BufferBase::failbit );
00223
00224 if ( _getpos == _size ) setstate( BufferBase::eobbit );
00225 }
00226
00227 inline
00228 int BufferBase::tellp() { return _putpos; }
00229
00230 inline
00231 int BufferBase::tellg() { return _getpos; }
00232
00233 inline
00234 void BufferBase::put( char c )
00235 {
00236 if ( _putpos++ == _size ) ++_size;
00237 }
00238
00239 inline
00240 void BufferBase::get( char& c )
00241 {
00242 if ( !eob() ) {
00243 if ( ++_getpos == _size ) setstate( BufferBase::eobbit );
00244 }
00245 else setstate( BufferBase::failbit );
00246 }
00247
00248 inline
00249 char BufferBase::get()
00250 {
00251 char c;
00252 get(c);
00253 return c;
00254 }
00255
00256 inline
00257 void BufferBase::put( int pos, char c )
00258 {
00259 seekp(pos);
00260 put(c);
00261 }
00262
00263 inline
00264 void BufferBase::get( int pos, char& c )
00265 {
00266 seekg(pos);
00267 get(c);
00268 }
00269
00270 inline
00271 char BufferBase::get( int pos )
00272 {
00273 seekg(pos);
00274 return get();
00275 }
00276
00277 inline
00278 int BufferBase::size() { return _size; }
00279
00280 inline
00281 void BufferBase::erase()
00282 {
00283 _size = 0;
00284 _putpos = 0;
00285 _getpos = 0;
00286 clear( BufferBase::goodbit );
00287 }
00288
00289 #endif
00290