00001 //------------------------------------------------------------------------------ 00002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN) 00003 // Author: Lukasz Janyst <ljanyst@cern.ch> 00004 //------------------------------------------------------------------------------ 00005 // XRootD is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // XRootD is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with XRootD. If not, see <http://www.gnu.org/licenses/>. 00017 //------------------------------------------------------------------------------ 00018 00019 #ifndef __XRD_CL_BUFFER_HH__ 00020 #define __XRD_CL_BUFFER_HH__ 00021 00022 #include <cstdlib> 00023 #include <stdint.h> 00024 #include <new> 00025 #include <cstring> 00026 #include <string> 00027 00028 namespace XrdCl 00029 { 00030 //---------------------------------------------------------------------------- 00032 //---------------------------------------------------------------------------- 00033 class Buffer 00034 { 00035 public: 00036 //------------------------------------------------------------------------ 00038 //------------------------------------------------------------------------ 00039 Buffer( uint32_t size = 0 ): pBuffer(0), pSize(0), pCursor(0) 00040 { 00041 if( size ) 00042 { 00043 Allocate( size ); 00044 } 00045 } 00046 00047 //------------------------------------------------------------------------ 00049 //------------------------------------------------------------------------ 00050 virtual ~Buffer() { Free(); } 00051 00052 //------------------------------------------------------------------------ 00054 //------------------------------------------------------------------------ 00055 const char *GetBuffer( uint32_t offset = 0 ) const 00056 { 00057 return pBuffer+offset; 00058 } 00059 00060 //------------------------------------------------------------------------ 00062 //------------------------------------------------------------------------ 00063 char *GetBuffer( uint32_t offset = 0 ) 00064 { 00065 return pBuffer+offset; 00066 } 00067 00068 //------------------------------------------------------------------------ 00070 //------------------------------------------------------------------------ 00071 void ReAllocate( uint32_t size ) 00072 { 00073 pBuffer = (char *)realloc( pBuffer, size ); 00074 if( !pBuffer ) 00075 throw std::bad_alloc(); 00076 pSize = size; 00077 } 00078 00079 //------------------------------------------------------------------------ 00081 //------------------------------------------------------------------------ 00082 void Free() 00083 { 00084 free( pBuffer ); 00085 pBuffer = 0; 00086 pSize = 0; 00087 pCursor = 0; 00088 } 00089 00090 //------------------------------------------------------------------------ 00092 //------------------------------------------------------------------------ 00093 void Allocate( uint32_t size ) 00094 { 00095 if( !size ) 00096 return; 00097 00098 pBuffer = (char *)malloc( size ); 00099 if( !pBuffer ) 00100 throw std::bad_alloc(); 00101 pSize = size; 00102 } 00103 00104 //------------------------------------------------------------------------ 00106 //------------------------------------------------------------------------ 00107 void Zero() 00108 { 00109 memset( pBuffer, 0, pSize ); 00110 } 00111 00112 //------------------------------------------------------------------------ 00114 //------------------------------------------------------------------------ 00115 uint32_t GetSize() const 00116 { 00117 return pSize; 00118 } 00119 00120 //------------------------------------------------------------------------ 00122 //------------------------------------------------------------------------ 00123 uint32_t GetCursor() const 00124 { 00125 return pCursor; 00126 } 00127 00128 //------------------------------------------------------------------------ 00130 //------------------------------------------------------------------------ 00131 void SetCursor( uint32_t cursor ) 00132 { 00133 pCursor = cursor; 00134 } 00135 00136 //------------------------------------------------------------------------ 00138 //------------------------------------------------------------------------ 00139 void AdvanceCursor( uint32_t delta ) 00140 { 00141 pCursor += delta; 00142 } 00143 00144 //------------------------------------------------------------------------ 00146 //------------------------------------------------------------------------ 00147 void Append( const char *buffer, uint32_t size ) 00148 { 00149 uint32_t remaining = pSize-pCursor; 00150 if( remaining < size ) 00151 ReAllocate( pCursor+size ); 00152 00153 memcpy( pBuffer+pCursor, buffer, size ); 00154 pCursor += size; 00155 } 00156 00157 //------------------------------------------------------------------------ 00159 //------------------------------------------------------------------------ 00160 void Append( const char *buffer, uint32_t size, uint32_t offset ) 00161 { 00162 uint32_t remaining = pSize-offset; 00163 if( remaining < size ) 00164 ReAllocate( offset+size ); 00165 00166 memcpy( pBuffer+offset, buffer, size ); 00167 } 00168 00169 //------------------------------------------------------------------------ 00171 //------------------------------------------------------------------------ 00172 char *GetBufferAtCursor() 00173 { 00174 return GetBuffer( pCursor ); 00175 } 00176 00177 //------------------------------------------------------------------------ 00179 //------------------------------------------------------------------------ 00180 const char *GetBufferAtCursor() const 00181 { 00182 return GetBuffer( pCursor ); 00183 } 00184 00185 //------------------------------------------------------------------------ 00187 //------------------------------------------------------------------------ 00188 void FromString( const std::string str ) 00189 { 00190 ReAllocate( str.length()+1 ); 00191 memcpy( pBuffer, str.c_str(), str.length() ); 00192 pBuffer[str.length()] = 0; 00193 } 00194 00195 //------------------------------------------------------------------------ 00197 //------------------------------------------------------------------------ 00198 std::string ToString() const 00199 { 00200 char *bf = new char[pSize+1]; 00201 bf[pSize] = 0; 00202 memcpy( bf, pBuffer, pSize ); 00203 std::string tmp = bf; 00204 delete [] bf; 00205 return tmp; 00206 } 00207 00208 //------------------------------------------------------------------------ 00210 //------------------------------------------------------------------------ 00211 void Grab( char *buffer, uint32_t size ) 00212 { 00213 Free(); 00214 pBuffer = buffer; 00215 pSize = size; 00216 } 00217 00218 //------------------------------------------------------------------------ 00220 //------------------------------------------------------------------------ 00221 char *Release() 00222 { 00223 char *buffer = pBuffer; 00224 pBuffer = 0; 00225 pSize = 0; 00226 pCursor = 0; 00227 return buffer; 00228 } 00229 00230 private: 00231 char *pBuffer; 00232 uint32_t pSize; 00233 uint32_t pCursor; 00234 }; 00235 } 00236 00237 #endif // __XRD_CL_BUFFER_HH__