buffer.h

00001 // -*- C++ -*- 00002 /* 00003 $Id: buffer_8h-source.html,v 1.1 2004/10/05 21:11:59 mentat Exp $ 00004 00005 VBuffer Class - Extention of STL vector 00006 Copyright (C) 1999-2002 Jesse Lovelace <jllovela@eos.ncsu.edu> 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 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 // zero old buffer data 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 //operator const byte *() const { return data(); } 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 // Temporary byte array and size, mutable 00206 mutable scoped_array<byte> m_data; 00207 mutable unsigned long int m_size; 00208 00209 }; 00210 00211 } // !GNUMessenger 00212 00213 #endif // !GM_BUFFER_H

Generated on Tue Oct 5 14:41:47 2004 for GNU Messenger by doxygen 1.3.8