#ifndef cbyte_H
#define cbyte_H
#include <string.h>
#include <iterator>
using namespace std;
#ifndef _BYTE_
#define _BYTE_
typedef unsigned char byte;
#endif //#ifndef _BYTE_
class bytesiterator : public iterator<input_iterator_tag, byte> {
private:
const byte *__p_vbyte;
public:
bytesiterator ( const byte *__pv_byte ) : __p_vbyte(__pv_byte) { };
virtual ~bytesiterator() {};
bytesiterator &operator++() {++this->__p_vbyte;return *this;}
bytesiterator operator++(int) {bytesiterator tmp(*this); operator++(); return tmp;}
bytesiterator &operator--() {--this->__p_vbyte;return *this;}
bytesiterator operator--(int) {bytesiterator tmp(*this); operator--(); return tmp;}
bool operator ==(const bytesiterator &__r_cbyte) {return this->__p_vbyte==__r_cbyte.__p_vbyte;}
bool operator !=(const bytesiterator &__r_cbyte) {return this->__p_vbyte!=__r_cbyte.__p_vbyte;}
int operator *() {return *this->__p_vbyte;}
};
class bytesiteratorreverse : public iterator<input_iterator_tag, byte> {
private:
const byte *__p_vbyte;
public:
bytesiteratorreverse ( const byte *__pv_byte ) : __p_vbyte(__pv_byte) { };
virtual ~bytesiteratorreverse() {};
bytesiteratorreverse &operator++() {--this->__p_vbyte;return *this;}
bytesiteratorreverse operator++(int) {bytesiteratorreverse tmp(*this); operator++(); return tmp;}
bytesiteratorreverse &operator--() {++this->__p_vbyte;return *this;}
bytesiteratorreverse operator--(int) {bytesiteratorreverse tmp(*this); operator--(); return tmp;}
bool operator ==(const bytesiteratorreverse &__r_cbyte) {return this->__p_vbyte==__r_cbyte.__p_vbyte;}
bool operator !=(const bytesiteratorreverse &__r_cbyte) {return this->__p_vbyte!=__r_cbyte.__p_vbyte;}
int operator *() {return *this->__p_vbyte;}
};
#ifndef _bytecontainer_
#define _bytecontainer_
class bytes {
protected:
byte *__p_vbytes;
size_t _ln;
public:
typedef bytesiterator iterator;
typedef bytesiteratorreverse reverse_iterator;
bytes ( const byte* __p_vbytes , size_t _ln) {
this->__p_vbytes = new byte[_ln];
this->_ln = _ln;
memcpy( this
->__p_vbytes
, __p_vbytes
, _ln
); }
bytes ( ) {
this->__p_vbytes = NULL;
this->_ln = 0;
}
bytes ( size_t _ln ) {
this->__p_vbytes = new byte[_ln];
this->_ln = _ln;
memset( this
->__p_vbytes
, 0 , _ln
); }
virtual ~bytes () { delete[] this->__p_vbytes; }
size_t leght() { return this->_ln; }
byte* __p() { return this->__p_vbytes; }
void clear() { if ( this->__p_vbytes ) delete[] this->__p_vbytes; this->_ln = 0; }
iterator begin() {
//bytesiterator _v_tmp( (const byte*)this->__p_vbytes );
iterator _v_tmp( (const byte*)this->__p_vbytes );
return _v_tmp;
}
iterator end() {
//bytesiterator _v_tmp( (const byte*)(&this->__p_vbytes[this->_ln]) );
iterator _v_tmp( (const byte*)(&this->__p_vbytes[this->_ln]) );
return _v_tmp;
}
reverse_iterator rbegin() {
//reverse_iterator _v_tmp( (const byte*)(&this->__p_vbytes[this->_ln-1]) );
reverse_iterator _v_tmp( (const byte*)(&this->__p_vbytes[this->_ln-1]) );
return _v_tmp;
}
reverse_iterator rend() {
//reverse_iterator _v_tmp( (const byte*)(this->__p_vbytes-sizeof(byte)) );
reverse_iterator _v_tmp( (const byte*)(this->__p_vbytes-sizeof(byte)) );
return _v_tmp;
}
bytes &operator+=( bytes &__p_cbyte) {
byte *__p_vbytes = this->__p_vbytes;
size_t _ln = 0;
size_t _pos = 0;
if ( __p_cbyte.__p_vbytes!=NULL && __p_cbyte._ln>0 ) {
if ( this->_ln>0 && this->__p_vbytes!=NULL ) {
_ln = this->_ln + __p_cbyte._ln;
_pos = this->_ln;
} else {
_ln = __p_cbyte._ln;
}
__p_vbytes = new byte[_ln];
memcpy( &__p_vbytes
[_pos
] , __p_cbyte.__p_vbytes
, __p_cbyte._ln
);
if ( this->__p_vbytes!=NULL && this->_ln>0 ) {
memcpy( __p_vbytes
, this
->__p_vbytes
, this
->_ln
); delete[] this->__p_vbytes;
}
}
this->__p_vbytes = __p_vbytes;
this->_ln = _ln;
return *this;
}
bytes &operator=( bytes &__p_cbyte ) {
byte *__p_vbytes = NULL;
if ( this != &__p_cbyte ) {
this->_ln = __p_cbyte._ln;
if ( this->_ln>0 && __p_cbyte.__p_vbytes != NULL ) {
__p_vbytes = new byte[this->_ln];
memcpy( __p_vbytes
, __p_cbyte.__p_vbytes
, this
->_ln
); } else {
this->_ln = 0;
}
delete[] this->__p_vbytes;
this->__p_vbytes = __p_vbytes;
}
return *this;
}
operator byte*() {
return this->__p_vbytes;
}
operator size_t() {
return this->_ln;
}
operator long int() {
return (long int)this->_ln;
}
operator unsigned long int() {
return (unsigned long int)this->_ln;
}
bool operator ==( bytes &__pc_byte ) {
size_t _szt_pos = 0;
if ( __pc_byte.__p_vbytes == this->__p_vbytes ) {
return true;
}
if ( (this->_ln == __pc_byte._ln) && (__p_vbytes != NULL) && (this->__p_vbytes!=NULL) ) {
while ( _szt_pos<this->_ln ) {
if ( __pc_byte.__p_vbytes[_szt_pos] == this->__p_vbytes[_szt_pos] ) {
_szt_pos++;
} else {
return false;
}
};
return true;
}
return false;
}
};
#endif //#ifndef _bytecontainer_
#endif // cbyte_H