00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
#ifndef GM_BUFFER_H
00024
#define GM_BUFFER_H
00025
00026
#include <string>
00027
#include <vector>
00028
#include <memory>
00029
#include "gm/misc.h"
00030
00031
namespace GNUMessenger
00032 {
00033
using namespace std;
00034
00037 class VBuffer:
public vector<byte>
00038 {
00039
public:
00040
00042 VBuffer(): vector<byte>(), m_size(0) {}
00043
00046 ~VBuffer()
00047 {
00048 zero();
00049
00050
if (m_data.
get())
00051 memset(m_data.
get(), 0, m_size *
sizeof(byte));
00052 }
00053
00057 VBuffer(
const string& str):vector<byte>(str.length()) {
00058
for (
unsigned int i = 0; i < str.length(); i++)
00059 (*this)[i] = ((byte)str[i]);
00060 }
00061
00065 VBuffer(
const char * str,
unsigned long length):vector<byte>(length)
00066 {
00067
for (
unsigned int i = 0; i < length; i++)
00068 (*this)[i] = ((byte)str[i]);
00069
00070 }
00071
00074 VBuffer(
const VBuffer& buf):vector<byte>()
00075 {
00076 reserve(buf.size());
00077 insert(end(), buf.begin(), buf.end());
00078 }
00079
00083 VBuffer(byte * str,
unsigned long length):vector<byte>(length)
00084 {
00085
for (
unsigned int i = 0; i < length; i++)
00086 (*this)[i] = (str[i]);
00087
00088 }
00089
00090
void wipe()
00091 {
00092 zero();
00093 clear();
00094 }
00095
00099 const byte *
data()
const {
00100 vector<byte>::const_iterator it = begin();
00101
00102
00103
if (m_data.
get())
00104 memset(m_data.
get(), 0,m_size *
sizeof(byte));
00105
00106 m_size = static_cast<unsigned long>(size());
00107 m_data.
reset(
new byte[size()] );
00108
00109
for (
unsigned long i = 0; i < size(); i++)
00110 {
00111 m_data[i] = *it;
00112 it++;
00113 }
00114
00115
return m_data.
get();
00116 }
00117
00118
00119
00126 VBuffer operator +(
const VBuffer& buf)
00127 {
00128
VBuffer tmp(*
this);
00129 tmp.insert(tmp.end(), buf.begin(), buf.end());
00130
return tmp;
00131 }
00132
00134 VBuffer&
operator =(
const VBuffer& buf)
00135 {
00136 zero();
00137 clear();
00138 reserve(buf.size());
00139
00140 insert(end(), buf.begin(), buf.end());
00141
return *
this;
00142 }
00143
00146 VBuffer&
operator += (
const VBuffer& buf )
00147 {
00148 reserve(size() + buf.size());
00149 insert(end(), buf.begin(), buf.end());
00150
return *
this;
00151 }
00152
00154 VBuffer&
operator += (
const byte& b )
00155 {
00156 push_back(b);
00157
return *
this;
00158 }
00159
00161 VBuffer sub(
unsigned long start,
unsigned long length = 0)
const
00162
{
00163
VBuffer temp;
00164
00165
unsigned long i;
00166
00167
if ((start >= size()) || (length > size()))
00168
return temp;
00169
00170
if (length == 0)
00171 temp.reserve(size() - start);
00172
else
00173 temp.reserve(length);
00174
00175
00176 vector<byte>::const_iterator itStart = begin();
00177
if (start != 0)
00178
for (i = 0; i < start; i++)
00179 itStart++;
00180
00181 vector<byte>::const_iterator itEnd = itStart;
00182
if (length == 0)
00183 itEnd = end();
00184
else
00185
for (i = 0; i < length; i++)
00186 itEnd++;
00187
00188 temp.insert(temp.begin(), itStart, itEnd);
00189
00190
return temp;
00191 }
00192
00193
private:
00194
00195
void zero()
00196 {
00197 vector<byte>::iterator it = begin();
00198
while( it != end() )
00199 {
00200 *it=0;
00201 it++;
00202 }
00203 }
00204
00205
00206
mutable scoped_array<byte> m_data;
00207
mutable unsigned long int m_size;
00208
00209 };
00210
00211 }
00212
00213
#endif // !GM_BUFFER_H