00001 #ifndef __BINARY_DATA_CPP__
00002 #define __BINARY_DATA_CPP__
00003
00004 #include <iterator>
00005 #include <string>
00006
00007 #include <utilities/binary_data.h>
00008
00009 namespace nitro
00010 {
00011 BinaryData::BinaryData( void ) : Data()
00012 {
00013 }
00014
00015 BinaryData::BinaryData( const char & Ch )
00016 {
00017 try
00018 {
00019 Data.push_back( Ch );
00020 }
00021 catch( nitro::exception e )
00022 {
00023 throw( nitro::exception( std::string( "BinaryData::BinaryData( const char & Ch )::" ) + e.what() , e.code() ) );
00024 }
00025 catch( ... )
00026 {
00027 throw( nitro::exception( std::string( "BinaryData::BinaryData( const char & Ch )::An error occured" ) , 0 ) );
00028 }
00029 }
00030
00031 BinaryData::BinaryData( const char * Buffer )
00032 {
00033 try
00034 {
00035
00036 AppendData( Buffer , strlen( Buffer ) + 1 );
00037 }
00038 catch( nitro::exception e )
00039 {
00040 throw( nitro::exception( std::string( "BinaryData::BinaryData( const char * Buffer )::" ) + e.what() , e.code() ) );
00041 }
00042 catch( ... )
00043 {
00044 throw( nitro::exception( std::string( "BinaryData::BinaryData( const char * Buffer )::An error occured" ) , 0 ) );
00045 }
00046 }
00047
00048 BinaryData::BinaryData( const char * Buffer , const std::size_t & Length )
00049 {
00050 try
00051 {
00052 AppendData( Buffer , Length );
00053 }
00054 catch( nitro::exception e )
00055 {
00056 throw( nitro::exception( std::string( "BinaryData::BinaryData( const char * Buffer , const std::size_t & Length )::" ) + e.what() , e.code() ) );
00057 }
00058 catch( ... )
00059 {
00060 throw( nitro::exception( std::string( "BinaryData::BinaryData( const char * Buffer , const std::size_t & Length )::An error occured" ) , 0 ) );
00061 }
00062 }
00063
00064 BinaryData::BinaryData( const BinaryData & Data )
00065 {
00066 try
00067 {
00068 Release();
00069
00070 AppendData( Data.GetBuffer() , Data.GetBufferLength() );
00071 }
00072 catch( nitro::exception e )
00073 {
00074 throw( nitro::exception( std::string( "BinaryData::BinaryData( const BinaryData & Data )::" ) + e.what() , e.code() ) );
00075 }
00076 catch( ... )
00077 {
00078 throw( nitro::exception( std::string( "BinaryData::BinaryData( const BinaryData & Data )::An error occured" ) , 0 ) );
00079 }
00080 }
00081
00082 BinaryData::operator const char * () const
00083 {
00084 try
00085 {
00086 return( GetBuffer() );
00087 }
00088 catch( nitro::exception e )
00089 {
00090 throw( nitro::exception( std::string( "BinaryData::operator const char * () const::" ) + e.what() , e.code() ) );
00091 }
00092 catch( ... )
00093 {
00094 throw( nitro::exception( std::string( "BinaryData::operator const char * () const::An error occured" ) , 0 ) );
00095 }
00096 }
00097
00098 BinaryData BinaryData::operator=( const BinaryData & Data )
00099 {
00100 try
00101 {
00102 Release();
00103
00104 AppendData( Data.GetBuffer() , Data.GetBufferLength() );
00105
00106 return( *this );
00107 }
00108 catch( nitro::exception e )
00109 {
00110 throw( nitro::exception( std::string( "BinaryData::operator=( const BinaryData & Data )::" ) + e.what() , e.code() ) );
00111 }
00112 catch( ... )
00113 {
00114 throw( nitro::exception( std::string( "BinaryData::operator=( const BinaryData & Data )::An error occured" ) , 0 ) );
00115 }
00116 }
00117
00118 void BinaryData::AppendData( const char & Ch )
00119 {
00120 try
00121 {
00122 Data.push_back( Ch );
00123 }
00124 catch( nitro::exception e )
00125 {
00126 throw( nitro::exception( std::string( "BinaryData::AppendData( const char & Ch )::" ) + e.what() , e.code() ) );
00127 }
00128 catch( ... )
00129 {
00130 throw( nitro::exception( std::string( "BinaryData::AppendData( const char & Ch )::An error occured" ) , 0 ) );
00131 }
00132 }
00133
00134 void BinaryData::AppendData( const char * Buffer )
00135 {
00136 try
00137 {
00138 AppendData( Buffer , strlen( Buffer ) );
00139 }
00140 catch( nitro::exception e )
00141 {
00142 throw( nitro::exception( std::string( "BinaryData::AppendData( const char * Buffer )::" ) + e.what() , e.code() ) );
00143 }
00144 catch( ... )
00145 {
00146 throw( nitro::exception( std::string( "BinaryData::AppendData( const char * Buffer )::An error occured" ) , 0 ) );
00147 }
00148 }
00149
00150 void BinaryData::AppendData( const std::string & Buffer )
00151 {
00152 try
00153 {
00154 AppendData( Buffer.c_str() , Buffer.size() );
00155 }
00156 catch( nitro::exception e )
00157 {
00158 throw( nitro::exception( std::string( "BinaryData::AppendData( const char * Buffer )::" ) + e.what() , e.code() ) );
00159 }
00160 catch( ... )
00161 {
00162 throw( nitro::exception( std::string( "BinaryData::AppendData( const char * Buffer )::An error occured" ) , 0 ) );
00163 }
00164 }
00165
00166 void BinaryData::AppendData( const char * Buffer , const std::size_t & theLength )
00167 {
00168 try
00169 {
00170 std::size_t OldSize( Data.size() );
00171 Data.resize( OldSize + theLength );
00172 memcpy( &( Data[ OldSize ] ) , Buffer , theLength );
00173 }
00174 catch( nitro::exception e )
00175 {
00176 throw( nitro::exception( std::string( "BinaryData::AppendData( const char * Buffer , const std::size_t & theLength )::" ) + e.what() , e.code() ) );
00177 }
00178 catch( ... )
00179 {
00180 throw( nitro::exception( std::string( "BinaryData::AppendData( const char * Buffer , const std::size_t & theLength )::An error occured" ) , 0 ) );
00181 }
00182 }
00183
00184 std::size_t BinaryData::GetBufferLength( void ) const
00185 {
00186 try
00187 {
00188 return( Data.size() );
00189 }
00190 catch( nitro::exception e )
00191 {
00192 throw( nitro::exception( std::string( "BinaryData::GetBufferLength( void ) const::" ) + e.what() , e.code() ) );
00193 }
00194 catch( ... )
00195 {
00196 throw( nitro::exception( std::string( "BinaryData::GetBufferLength( void ) const::An error occured" ) , 0 ) );
00197 }
00198 }
00199
00200 const char * BinaryData::GetBuffer( void ) const
00201 {
00202 try
00203 {
00204 return( &( Data[ 0 ] ) );
00205 }
00206 catch( nitro::exception e )
00207 {
00208 throw( nitro::exception( std::string( "BinaryData::GetBuffer( void )::" ) + e.what() , e.code() ) );
00209 }
00210 catch( ... )
00211 {
00212 throw( nitro::exception( std::string( "BinaryData::GetBuffer( void )::An error occured" ) , 0 ) );
00213 }
00214 }
00215
00216 void BinaryData::SetByte( std::size_t BytePos , char ByteValue )
00217 {
00218 try
00219 {
00220 Data[ BytePos ] = ByteValue;
00221 }
00222 catch( nitro::exception e )
00223 {
00224 throw( nitro::exception( std::string( "BinaryData::SetByte( std::size_t BytePos , char ByteValue )::" ) + e.what() , e.code() ) );
00225 }
00226 catch( ... )
00227 {
00228 throw( nitro::exception( std::string( "BinaryData::SetByte( std::size_t BytePos , char ByteValue )::An error occured" ) , 0 ) );
00229 }
00230 }
00231
00232 void BinaryData::Release( void )
00233 {
00234 try
00235 {
00236 Data.clear();
00237 Data.resize( 0 );
00238 }
00239 catch( nitro::exception e )
00240 {
00241 throw( nitro::exception( std::string( "BinaryData::Release( void )::" ) + e.what() , e.code() ) );
00242 }
00243 catch( ... )
00244 {
00245 throw( nitro::exception( std::string( "BinaryData::Release( void )::An error occured" ) , 0 ) );
00246 }
00247 }
00248
00249 void BinaryData::Reserve( const std::size_t & theLength )
00250 {
00251 try
00252 {
00253 if( Data.size() < theLength )
00254 {
00255 Data.reserve( theLength );
00256 }
00257 Data.resize( 0 );
00258 }
00259 catch( nitro::exception e )
00260 {
00261 throw( nitro::exception( std::string( "BinaryData::Reserve( const std::size_t & theLength )::" ) + e.what() , e.code() ) );
00262 }
00263 catch( ... )
00264 {
00265 throw( nitro::exception( std::string( "BinaryData::Reserve( const std::size_t & theLength )::An error occured" ) , 0 ) );
00266 }
00267 }
00268
00269 BinaryData::~BinaryData()
00270 {
00271 try
00272 {
00273 Release();
00274 }
00275 catch( ... )
00276 {
00277 }
00278 }
00279
00280 int BinaryData::GetFirstOccurance( const char * OriginalBuffer , const std::size_t OriginalBufferLength , const char * Buffer , const std::size_t BufferLength , const std::size_t StartFrom )
00281 {
00282 try
00283 {
00284 for( std::size_t i( StartFrom ) ; i < OriginalBufferLength ; i++ )
00285 {
00286 std::size_t j( 0 );
00287
00288 for( ; j < BufferLength && i + j < OriginalBufferLength && OriginalBuffer[ i + j ] == Buffer[ j ] ; j++ );
00289
00290 if( j == BufferLength )
00291 {
00292 return( ( int )i );
00293 }
00294 }
00295
00296 return( -1 );
00297 }
00298 catch( nitro::exception e )
00299 {
00300 throw( nitro::exception( std::string( "BinaryData::GetFirstOccurance( const char * OriginalBuffer , const std::size_t OriginalBufferLength , const char * Buffer , const std::size_t BufferLength , const std::size_t StartFrom /* = 0 */ )::" ) + e.what() , e.code() ) );
00301 }
00302 catch( ... )
00303 {
00304 throw( nitro::exception( std::string( "BinaryData::GetFirstOccurance( const char * OriginalBuffer , const std::size_t OriginalBufferLength , const char * Buffer , const std::size_t BufferLength , const std::size_t StartFrom /* = 0 */ )::An error occured while executing template< class container , class raw >int GetFirstOccurance( const container & c , const raw & r )" ) , 0 ) );
00305 }
00306 }
00307
00308 int BinaryData::GetFirstOccurance( const BinaryData & c , const char * Buffer , const std::size_t BufferLength , const std::size_t StartFrom )
00309 {
00310 try
00311 {
00312 return( GetFirstOccurance( c.GetBuffer() , c.GetBufferLength() , Buffer , BufferLength ) );
00313 }
00314 catch( nitro::exception e )
00315 {
00316 throw( nitro::exception( std::string( "GetFirstOccurance( const BinaryData & c , const char * Buffer , const std::size_t BufferLength )::" ) + e.what() , e.code() ) );
00317 }
00318 catch( ... )
00319 {
00320 throw( nitro::exception( std::string( "GetFirstOccurance( const BinaryData & c , const char * Buffer , const std::size_t BufferLength )::An error occured while executing template< class container , class raw >int GetFirstOccurance( const container & c , const raw & r )" ) , 0 ) );
00321 }
00322 }
00323
00324 int BinaryData::GetOccuranceCount( const char * OriginalBuffer , const std::size_t OriginalBufferLength , const char * Buffer , const std::size_t BufferLength )
00325 {
00326 try
00327 {
00328 std::size_t Counter( 0 );
00329 int Tmp;
00330 for( int i( 0 ) ; i < ( int )OriginalBufferLength ; i++ )
00331 {
00332 Tmp = GetFirstOccurance( OriginalBuffer , OriginalBufferLength , Buffer , BufferLength , i );
00333
00334 if( Tmp != -1 )
00335 {
00336 Counter++;
00337 i = ( int )( Tmp + BufferLength - 1 );
00338 }
00339 }
00340
00341 return( ( int )Counter );
00342 }
00343 catch( nitro::exception e )
00344 {
00345 throw( nitro::exception( std::string( "BinaryData::GetOccuranceCount( const char * OriginalBuffer , const std::size_t OriginalBufferLength , const char * Buffer , const std::size_t BufferLength )::" ) + e.what() , e.code() ) );
00346 }
00347 catch( ... )
00348 {
00349 throw( nitro::exception( std::string( "BinaryData::GetOccuranceCount( const char * OriginalBuffer , const std::size_t OriginalBufferLength , const char * Buffer , const std::size_t BufferLength )::An error occured" ) , 0 ) );
00350 }
00351 }
00352
00353 int BinaryData::GetOccuranceCount( const BinaryData & c , const char * Buffer , const std::size_t BufferLength )
00354 {
00355 try
00356 {
00357 return( GetOccuranceCount( c.GetBuffer() , c.GetBufferLength() , Buffer , BufferLength ) );
00358 }
00359 catch( nitro::exception e )
00360 {
00361 throw( nitro::exception( std::string( "BinaryData::GetOccuranceCount( const BinaryData & c , const char * Buffer , const std::size_t BufferLength )::" ) + e.what() , e.code() ) );
00362 }
00363 catch( ... )
00364 {
00365 throw( nitro::exception( std::string( "BinaryData::GetOccuranceCount( const BinaryData & c , const char * Buffer , const std::size_t BufferLength )::An error occured" ) , 0 ) );
00366 }
00367 }
00368
00369 void BinaryData::ReplaceBuffer( BinaryData & c , const std::size_t StartPos , const std::size_t ReplaceLength , const char * Buffer , const std::size_t BufferLength )
00370 {
00371 try
00372 {
00373 c.Data.erase( c.Data.begin() + StartPos , c.Data.begin() + StartPos + ReplaceLength );
00374
00375 std::insert_iterator< std::vector< char > > InsertIterator( c.Data , c.Data.begin() + StartPos );
00376
00377 for( std::size_t i( 0 ) ; i < BufferLength ; i++ )
00378 {
00379 *InsertIterator = Buffer[ i ];
00380 }
00381 }
00382 catch( nitro::exception e )
00383 {
00384 throw( nitro::exception( std::string( "ReplaceBuffer( BinaryData & c , std::size_t StartPos , std::size_t ReplaceLength , const char * Buffer , const std::size_t BufferLength )::" ) + e.what() , e.code() ) );
00385 }
00386 catch( ... )
00387 {
00388 throw( nitro::exception( std::string( "ReplaceBuffer( BinaryData & c , std::size_t StartPos , std::size_t ReplaceLength , const char * Buffer , const std::size_t BufferLength )::An error occured" ) , 0 ) );
00389 }
00390 }
00391
00392 void BinaryData::ReplaceBuffer( BinaryData & c , const char * NeedleBuffer , const std::size_t NeedleBufferLength , const char * Buffer , const std::size_t BufferLength )
00393 {
00394 try
00395 {
00396 int Cursor( 0 );
00397 int StartFrom( 0 );
00398
00399 for( ; ( Cursor = GetFirstOccurance( c , NeedleBuffer , NeedleBufferLength , StartFrom ) ) != -1 ; )
00400 {
00401 ReplaceBuffer( c , Cursor , NeedleBufferLength , Buffer , BufferLength );
00402 StartFrom = ( int )( Cursor + BufferLength );
00403 }
00404 }
00405 catch( nitro::exception e )
00406 {
00407 throw( nitro::exception( std::string( "BinaryData::ReplaceBuffer( BinaryData & c , const char * NeedleBuffer , const std::size_t NeedleBufferLength , const char * Buffer , const std::size_t BufferLength )::" ) + e.what() , e.code() ) );
00408 }
00409 catch( ... )
00410 {
00411 throw( nitro::exception( std::string( "BinaryData::ReplaceBuffer( BinaryData & c , const char * NeedleBuffer , const std::size_t NeedleBufferLength , const char * Buffer , const std::size_t BufferLength )::An error occured" ) , 0 ) );
00412 }
00413 }
00414
00415 BEGIN_TESTING_SECTION()
00416
00417 ENABLE_CLASS_TESTING( BinaryData )
00418
00419 CLASS_MEMBER_FUNCTION_TESTING_0( BinaryData , GetBufferLength , tstGetBufferLength , std::size_t , std::size_t )
00420 CLASS_MEMBER_FUNCTION_TESTING_1( BinaryData , AppendData1 , tstAppendData1 , const char * , void , NO_RET )
00421 CLASS_MEMBER_FUNCTION_TESTING_2( BinaryData , tstAppendData , tstAppendData2 , const char * , std::size_t , void , NO_RET )
00422 CLASS_MEMBER_FUNCTION_TESTING_0( BinaryData , GetBuffer , tstGetBuffer , const char * , const char * )
00423 CLASS_MEMBER_FUNCTION_TESTING_2( BinaryData , SetByte , tstSetByte , std::size_t , char , void , NO_RET )
00424 CLASS_MEMBER_FUNCTION_TESTING_0( BinaryData , Release , tstRelease , void , NO_RET )
00425
00426 END_TESTING_SECTION()
00427 }
00428
00429 #endif