00001 #ifndef __NITRO_STRING_UTILITIES_H__
00002 #define __NITRO_STRING_UTILITIES_H__
00003
00004 #include <algorithm>
00005 #include <set>
00006 #include <string>
00007 #include <vector>
00008
00009 #include <utilities/testing_utilities.h>
00010
00011 #if !defined( WIN32_PLATFORM ) && !defined( NIX_PLATFORM ) && !defined( MINGW_PLATFORM ) && !defined( CYGWIN_PLATFORM )
00012 #define WIN32_PLATFORM
00013 #endif
00014
00015 #ifdef STRING_UTILITIES
00016 #if defined( WIN32_PLATFORM ) || defined( MINGW_PLATFORM ) || defined( CYGWIN_PLATFORM )
00017 #define STRING_UTILITIES_DLL_ENTITY __declspec( dllexport )
00018 #endif
00019
00020 #if defined( NIX_PLATFORM )
00021 #define STRING_UTILITIES_DLL_ENTITY
00022 #endif
00023 #else
00024 #ifdef WIN32_PLATFORM
00025 #define STRING_UTILITIES_DLL_ENTITY __declspec( dllimport )
00026 #pragma comment( lib , "string_utilities.lib" )
00027 #endif
00028
00029 #if defined( CYGWIN_PLATFORM ) || defined( MINGW_PLATFORM )
00030 #define STRING_UTILITIES_DLL_ENTITY __declspec( dllimport )
00031 #endif
00032
00033 #if defined( NIX_PLATFORM )
00034 #define STRING_UTILITIES_DLL_ENTITY
00035 #endif
00036 #endif
00037
00038 namespace nitro
00039 {
00052 class STRING_UTILITIES_DLL_ENTITY FSPath{
00053 public:
00054
00077 static void AddEndSlash( const char * OldPath , char * NewPath );
00078
00097 template< class str_type >static str_type AddEndSlash( const str_type & Path );
00098
00121 static void DeleteEndSlash( const char * OldPath , char * NewPath );
00122
00145 template< class str_type >static str_type DeleteEndSlash( const str_type & OldPath );
00146
00169 static char * ExtractFilePath( const char * FilePath , char * ClearedFilePath );
00170
00193 template< class str_type >static str_type ExtractFilePath( const str_type & FilePath );
00194
00217 static char * ExtractFileName( const char * FilePath , char * FileName );
00218
00241 template< class str_type >static str_type ExtractFileName( const str_type & FilePath );
00242
00265 static void ExtractModuleName( const char * file_name , char * module_name );
00266
00289 template< class str_type >static str_type ExtractModuleName( const str_type & FileName );
00290
00317 static char * ExtractFileExtension( const char * FileName , char * file_extension );
00318
00341 template< class str_type >static str_type ExtractFileExtension( const str_type & FileName );
00342
00369 template< class cont >static void ExplodePath( const std::string & Path , cont & PathSegments );
00370 };
00371
00372 template< class str_type >str_type FSPath::AddEndSlash( const str_type & Path )
00373 {
00374 try
00375 {
00376 if( Path[ Path.length() - 1 ] == '/' || Path[ Path.length() - 1 ] == '\\' )
00377 {
00378 return( Path );
00379 }
00380 else
00381 {
00382 char * TmpStr( new char[ Path.length() + 2 ] );
00383 memset( TmpStr , 0 , Path.length() + 2 );
00384
00385 AddEndSlash( Path.c_str() , TmpStr );
00386 std::string RetStr( TmpStr );
00387
00388 delete [] TmpStr;
00389
00390 return( RetStr );
00391 }
00392 }
00393 catch( nitro::exception e )
00394 {
00395 throw( nitro::exception( std::string( "FSPath::AddEndSlash( const str_type & Path )::" ) + e.what() , e.code() ) );
00396 }
00397 catch( ... )
00398 {
00399 throw( nitro::exception( std::string( "FSPath::AddEndSlash( const str_type & Path )::An error occured" ) , 0 ) );
00400 }
00401 }
00402
00403 template< class str_type > str_type FSPath::DeleteEndSlash( const str_type & OldPath )
00404 {
00405 try
00406 {
00407 char * CharChain( new char[ OldPath.length() + 2 ] );
00408 memset( CharChain , 0 , OldPath.length() + 2 );
00409
00410 DeleteEndSlash( OldPath.c_str() , CharChain );
00411 str_type RetString( CharChain );
00412
00413 delete [] CharChain;
00414
00415 return( RetString );
00416 }
00417 catch( nitro::exception e )
00418 {
00419 throw( nitro::exception( std::string( "FSPath::DeleteEndSlash( const str_type & OldPath )::" ) + e.what() , e.code() ) );
00420 }
00421 catch( ... )
00422 {
00423 throw( nitro::exception( std::string( "FSPath::DeleteEndSlash( const str_type & OldPath )::An error occured" ) , 0 ) );
00424 }
00425 }
00426
00427 template< class str_type >str_type FSPath::ExtractFilePath( const str_type & FilePath )
00428 {
00429 try
00430 {
00431 char * CharChain( new char[ FilePath.length() + 2 ] );
00432
00433 memset( CharChain , 0 , FilePath.length() + 2 );
00434
00435 str_type RetString( ExtractFilePath( FilePath.c_str() , CharChain ) );
00436
00437 delete [] CharChain;
00438
00439 return( RetString );
00440 }
00441 catch( nitro::exception e )
00442 {
00443 throw( nitro::exception( std::string( "FSPath::ExtractFilePath( const str_type & FilePath )::" ) + e.what() , e.code() ) );
00444 }
00445 catch( ... )
00446 {
00447 throw( nitro::exception( std::string( "FSPath::ExtractFilePath( const str_type & FilePath )::An error occured" ) , 0 ) );
00448 }
00449 }
00450
00451 template< class str_type >str_type FSPath::ExtractFileName( const str_type & FilePath )
00452 {
00453 try
00454 {
00455 char * CharChain( new char[ FilePath.length() + 1 ] );
00456
00457 memset( CharChain , 0 , FilePath.length() + 1 );
00458
00459 ExtractFileName( FilePath.c_str() , CharChain );
00460 str_type RetString( CharChain );
00461
00462 delete [] CharChain;
00463
00464 return( RetString );
00465 }
00466 catch( nitro::exception e )
00467 {
00468 throw( nitro::exception( std::string( "FSPath::ExtractFileName( const str_type & FilePath )::" ) + e.what() , e.code() ) );
00469 }
00470 catch( ... )
00471 {
00472 throw( nitro::exception( std::string( "FSPath::ExtractFileName( const str_type & FilePath )::An error occured" ) , 0 ) );
00473 }
00474 }
00475
00476 template< class str_type > str_type FSPath::ExtractModuleName( const str_type & FileName )
00477 {
00478 try
00479 {
00480 char * CharChain( new char[ FileName.length() + 1 ] );
00481
00482 memset( CharChain , 0 , FileName.length() + 1 );
00483
00484 ExtractModuleName( FileName.c_str() , CharChain );
00485 str_type RetString( CharChain );
00486
00487 delete [] CharChain;
00488
00489 return( RetString );
00490 }
00491 catch( nitro::exception e )
00492 {
00493 throw( nitro::exception( std::string( "FSPath::ExtractModuleName( const str_type & FileName )::" ) + e.what() , e.code() ) );
00494 }
00495 catch( ... )
00496 {
00497 throw( nitro::exception( std::string( "FSPath::ExtractModuleName( const str_type & FileName )::An error occured" ) , 0 ) );
00498 }
00499 }
00500
00501 template< class str_type > str_type FSPath::ExtractFileExtension( const str_type & FileName )
00502 {
00503 try
00504 {
00505 char * CharChain( new char[ FileName.length() + 1 ] );
00506
00507 memset( CharChain , 0 , CharChain );
00508
00509 ExtractFileExtension( FileName.c_str() , FileName.length() + 1 );
00510 str_type RetString( CharChain );
00511
00512 delete [] CharChain;
00513
00514 return( RetString );
00515 }
00516 catch( nitro::exception e )
00517 {
00518 throw( nitro::exception( std::string( "FSPath::ExtractFileExtension( const str_type & file_name )::" ) + e.what() , e.code() ) );
00519 }
00520 catch( ... )
00521 {
00522 throw( nitro::exception( std::string( "FSPath::ExtractFileExtension( const str_type & file_name )::An error occured" ) , 0 ) );
00523 }
00524 }
00525
00526 template< class cont >void FSPath::ExplodePath( const std::string & Path , cont & PathSegments )
00527 {
00528 try
00529 {
00530 char * PathPart( new char [ Path.size() + 1 ] );
00531 char * PathSegment( new char [ Path.size() + 1 ] );
00532
00533 std::string TmpPath( Path );
00534
00535 do
00536 {
00537 nitro::FSPath::ExtractFileName( TmpPath.c_str() , PathSegment );
00538 PathSegments.push_back( std::string( PathSegment ) );
00539
00540 nitro::FSPath::ExtractFilePath( TmpPath.c_str() , PathPart );
00541 TmpPath = PathPart;
00542 }
00543 while( strlen( PathPart ) );
00544
00545 delete [] PathPart;
00546 delete [] PathSegment;
00547
00548 std::reverse( PathSegments.begin() , PathSegments.end() );
00549 }
00550 catch( nitro::exception e )
00551 {
00552 throw( nitro::exception( std::string( "FSPath::ExplodePath( const std::string & Path , cont & PathSegments )::" ) + e.what() , e.code() ) );
00553 }
00554 catch( ... )
00555 {
00556 throw( nitro::exception( std::string( "FSPath::ExplodePath( const std::string & Path , cont & PathSegments )::An error occured" ) , 0 ) );
00557 }
00558 }
00559
00572 class STRING_UTILITIES_DLL_ENTITY SUtils{
00573 public:
00574
00605 static const char * TrimRight( const char * Str , char * OutStr , char Ch );
00606
00633 template< class str_type >static str_type TrimRight( const str_type & Str , char Ch );
00634
00665 static const char * TrimLeft( const char * Str , char * OutStr , char Ch );
00666
00693 template< class str_type >static str_type TrimLeft( const str_type & Str , char Ch );
00694 };
00695
00696 template< class str_type > str_type SUtils::TrimRight( const str_type & Str , char Ch )
00697 {
00698 try
00699 {
00700 return( TrimRight( Str.c_str() , Ch ) );
00701 }
00702 catch( nitro::exception e )
00703 {
00704 throw( nitro::exception( std::string( "SUtils::TrimRight( const str_type & Str , char Ch )::" ) + e.what() , e.code() ) );
00705 }
00706 catch( ... )
00707 {
00708 throw( nitro::exception( std::string( "SUtils::TrimRight( const str_type & Str , char Ch )::An error occured" ) , 0 ) );
00709 }
00710 }
00711
00712 template< class str_type > str_type SUtils::TrimLeft( const str_type & Str , char Ch )
00713 {
00714 try
00715 {
00716 return( TrimLeft( Str.c_str() , Ch ) );
00717 }
00718 catch( nitro::exception e )
00719 {
00720 throw( nitro::exception( std::string( "SUtils::TrimLeft( const str_type & Str , char Ch )::" ) + e.what() , e.code() ) );
00721 }
00722 catch( ... )
00723 {
00724 throw( nitro::exception( std::string( "SUtils::TrimLeft( const str_type & Str , char Ch )::An error occured" ) , 0 ) );
00725 }
00726 }
00727
00740 class STRING_UTILITIES_DLL_ENTITY Encoders{
00741 public:
00742
00769 static void XOREncode( char * OutputBuffer , std::size_t BufferSize , char * CodeWord );
00770
00797 static void XORDecode( char * InputBuffer , std::size_t BufferSize , char * CodeWord );
00798
00825 static void EncodeBase64( const char * bf , std::size_t count , char * outbf );
00826
00853 template< class str_type >static str_type EncodeBase64( const char * bf , std::size_t count );
00854
00881 static void DecodeBase64( const char * InBuf , char * OutBuf , std::size_t & Count );
00882
00901 template< class str_type >static str_type DecodeBase64( const char * bf );
00902
00929 static void md5( const char * Data , std::size_t DataLength , char * Hash );
00930
00957 template< class str_type >static void md5( const char * Data , std::size_t DataLength );
00958 };
00959
00960 template< class str_type > str_type Encoders::EncodeBase64( const char * bf , std::size_t count )
00961 {
00962 try
00963 {
00964 str_type out;
00965
00966 char * OutBuffer( new char[ ( std::size_t )( count * 1.5 ) ] );
00967 memset( OutBuffer , 0 , ( std::size_t )( count * 1.5 ) );
00968
00969 EncodeBase64( bf , count , OutBuffer );
00970
00971 out.AppendData( OutBuffer , ( std::size_t )( count * 1.5 ) );
00972
00973 delete [] OutBuffer;
00974
00975 return( out );
00976 }
00977 catch( nitro::exception e )
00978 {
00979 throw( nitro::exception( std::string( "Encoders::EncodeBase64( const char * bf , std::size_t count )::" ) + e.what() , e.code() ) );
00980 }
00981 catch( ... )
00982 {
00983 throw( nitro::exception( std::string( "Encoders::EncodeBase64( const char * bf , std::size_t count )::An error occured" ) , 0 ) );
00984 }
00985 }
00986
00987 template< class str_type > str_type Encoders::DecodeBase64( const char * bf )
00988 {
00989 try
00990 {
00991 str_type out;
00992
00993 char * OutBuffer( new char[ strlen( bf ) ] );
00994
00995 memset( OutBuffer , 0 , strlen( bf ) );
00996
00997 std::size_t Count;
00998
00999 DecodeBase64( bf , OutBuffer , Count );
01000
01001 out.AppendData( OutBuffer , Count );
01002
01003 delete [] OutBuffer;
01004
01005 return( out );
01006 }
01007 catch( nitro::exception e )
01008 {
01009 throw( nitro::exception( std::string( "Encoders::DecodeBase64( const char * bf )::" ) + e.what() , e.code() ) );
01010 }
01011 catch( ... )
01012 {
01013 throw( nitro::exception( std::string( "Encoders::DecodeBase64( const char * bf )::An error occured" ) , 0 ) );
01014 }
01015 }
01016
01017 template< class str_type > void Encoders::md5( const char * Data , std::size_t DataLength )
01018 {
01019 try
01020 {
01021 char Hash[ 33 ];
01022 md5( Data , DataLength , Hash );
01023 return( str_type( Hash ) );
01024 }
01025 catch( nitro::exception e )
01026 {
01027 throw( nitro::exception( std::string( "Encoders::md5( const char * Data , std::size_t DataLength )::" ) + e.what() , e.code() ) );
01028 }
01029 catch( ... )
01030 {
01031 throw( nitro::exception( std::string( "Encoders::md5( const char * Data , std::size_t DataLength )::An error occured" ) , 0 ) );
01032 }
01033 }
01034
01047 class STRING_UTILITIES_DLL_ENTITY Parsers{
01048 public:
01049
01080 template< class cont >static void ExplodeString( const std::string & Str , cont & StringSegments , char ch );
01081
01112 template< class cont >static void ExplodeString( const std::string & Str , cont & StringSegments , std::set< char > & Chars );
01113
01136 template< class cont >static void StringToSet( std::string & Str , cont & c );
01137
01169 static void GetCommandLineParameter( const char * CommandLine , const char * ParameterName , const char * DefaultValue , char * ParameterValue );
01170
01209 template< class str_type >static str_type GetCommandLineParameter( const str_type & CommandLine , const str_type & ParameterName , const str_type & DefaultValue );
01210
01245 template< class str_type >static str_type GetCommandLineParameter( int argc , const char * * argv , const str_type & ParameterName , const str_type & DefaultValue );
01246
01281 static void GetCommandLineParameter( int argc , const char * * argv , const char * ParameterName , const char * DefaultValue , char * ParameterValue );
01282
01305 static bool CommandLineParameterExists( const char * CommandLine , const char * ParameterName );
01306
01329 template< class str_type >static bool CommandLineParameterExists( const str_type & CommandLine , const str_type & ParameterName );
01330
01362 static bool CommandLineParameterExists( int argc , const char * * argv , const char * ParameterName );
01363
01394 template< class str_type >static bool CommandLineParameterExists( int argc , const char * * argv , const str_type & ParameterName );
01395
01422 template< class str_type >static bool BeginsWith( const str_type & String , const str_type & Prefix );
01423
01450 static bool BeginsWith( const char * String , const char * Prefix );
01451
01478 template< class str_type >static bool EndsWith( const str_type & String , const str_type & Suffix );
01479
01506 static bool EndsWith( const char * String , const char * Suffix );
01507
01534 template< class Iter >static std::string GetCommonPrefix( const Iter & IterBegin , const Iter & IterEnd );
01535
01570 template< class Iter1 , class Iter2 >int FindSubSequence( const Iter1 & Begin , const Iter1 & End , const Iter2 & SequenceBegin , const Iter2 & SequenceEnd );
01571 };
01572
01573 template< class cont > void Parsers::ExplodeString( const std::string & Str , cont & StrSegments , char ch )
01574 {
01575 try
01576 {
01577 std::set< char > Chars;
01578 Chars.insert( ch );
01579 ExplodeString( Str , StrSegments , Chars );
01580 }
01581 catch( nitro::exception e )
01582 {
01583 throw( nitro::exception( std::string( "Parsers::ExplodeString( const std::string & Str , cont & StrSegments , char ch )::" ) + e.what() , e.code() ) );
01584 }
01585 catch( ... )
01586 {
01587 throw( nitro::exception( std::string( "Parsers::ExplodeString( const std::string & Str , cont & StrSegments , char ch )::An error occured" ) , 0 ) );
01588 }
01589 }
01590
01591 template< class cont > void Parsers::ExplodeString( const std::string & Str , cont & StringSegments , std::set< char > & Chars )
01592 {
01593 try
01594 {
01595 std::string TmpStr( Str );
01596
01597 std::string::const_iterator IterOld( Str.begin() );
01598 std::string::const_iterator IterNew;
01599 for( std::set< char >::iterator i( Chars.begin() ) ; i != Chars.end() && IterNew != Str.end() ; i++ )
01600 {
01601 IterNew = std::find( IterOld , Str.end() , *i );
01602 }
01603
01604 std::string Segment;
01605
01606 while( IterNew != Str.end() )
01607 {
01608 Segment.resize( 0 );
01609 std::copy( IterOld , IterNew , std::back_inserter< std::string >( Segment ) );
01610 StringSegments.push_back( Segment );
01611
01612 IterOld = IterNew;
01613 IterOld++;
01614 for( std::set< char >::iterator i( Chars.begin() ) ; i != Chars.end() && IterNew != Str.end() ; i++ )
01615 {
01616 IterNew = std::find( IterOld , Str.end() , *i );
01617 }
01618 }
01619
01620 Segment = "";
01621 std::copy( IterOld , Str.end() , std::back_inserter< std::string >( Segment ) );
01622 StringSegments.push_back( Segment );
01623 }
01624 catch( nitro::exception e )
01625 {
01626 throw( nitro::exception( std::string( "Parsers::ExplodeString( const std::string & Str , cont & StrSegments , std::set< char > & Chars )::" ) + e.what() , e.code() ) );
01627 }
01628 catch( ... )
01629 {
01630 throw( nitro::exception( std::string( "Parsers::ExplodeString( const std::string & Str , cont & StrSegments , std::set< char > & Chars )::An error occured" ) , 0 ) );
01631 }
01632 }
01633
01634 template< class cont > void Parsers::StringToSet( std::string & Str , cont & c )
01635 {
01636 try
01637 {
01638 for( std::size_t i( 0 ) ; i < Str.size() ; i++ )
01639 {
01640 c.insert( Str[ i ] );
01641 }
01642 }
01643 catch( nitro::exception e )
01644 {
01645 throw( nitro::exception( std::string( "Parsers::StringToSet( std::string & Str , cont & c )::" ) + e.what() , e.code() ) );
01646 }
01647 catch( ... )
01648 {
01649 throw( nitro::exception( std::string( "Parsers::StringToSet( std::string & Str , cont & c )::An error occured" ) , 0 ) );
01650 }
01651 }
01652
01653 template< class str_type > str_type Parsers::GetCommandLineParameter( const str_type & CommandLine , const str_type & ParameterName , const str_type & DefaultValue )
01654 {
01655 try
01656 {
01657 std::size_t a( CommandLine.size() );
01658 std::size_t b( DefaultValue.size() );
01659 char * ParameterValue( new char [ ( a > b ? a : b ) + 1 ] );
01660
01661 GetCommandLineParameter( CommandLine.c_str() , ParameterName.c_str() , DefaultValue.c_str() , ParameterValue );
01662
01663 std::string Ret( ParameterValue );
01664
01665 delete [] ParameterValue;
01666
01667 return( Ret );
01668 }
01669 catch( nitro::exception e )
01670 {
01671 throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( const str_type & CommandLine , const str_type & ParameterName , const str_type & DefaultValue )::" ) + e.what() , e.code() ) );
01672 }
01673 catch( ... )
01674 {
01675 throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( const str_type & CommandLine , const str_type & ParameterName , const str_type & DefaultValue )::An error occured" ) , 0 ) );
01676 }
01677 }
01678
01679 template< class str_type > str_type Parsers::GetCommandLineParameter( int argc , const char * * argv , const str_type & ParameterName , const str_type & DefaultValue )
01680 {
01681 try
01682 {
01683 std::size_t a( 0 );
01684 for( int i( 0 ) ; i < argc ; i++ )
01685 {
01686 a += strlen( argv[ i ] );
01687 }
01688 std::size_t b( DefaultValue.size() );
01689
01690 char * ParameterValue( new char [ ( a > b ? a : b ) + 1 ] );
01691
01692 GetCommandLineParameter( argc , argv , ParameterName.c_str() , DefaultValue.c_str() , ParameterValue );
01693
01694 std::string Ret( ParameterValue );
01695
01696 delete [] ParameterValue;
01697
01698 return( Ret );
01699 }
01700 catch( nitro::exception e )
01701 {
01702 throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( const str_type & CommandLine , const str_type & ParameterName )::" ) + e.what() , e.code() ) );
01703 }
01704 catch( ... )
01705 {
01706 throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( const str_type & CommandLine , const str_type & ParameterName )::An error occured" ) , 0 ) );
01707 }
01708 }
01709
01710 template< class str_type > bool Parsers::CommandLineParameterExists( const str_type & CommandLine , const str_type & ParameterName )
01711 {
01712 try
01713 {
01714 GetCommandLineParameterExists( CommandLine.c_str() , ParameterName.c_str() );
01715 }
01716 catch( nitro::exception e )
01717 {
01718 throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( const str_type & CommandLine , const str_type & ParameterName )::" ) + e.what() , e.code() ) );
01719 }
01720 catch( ... )
01721 {
01722 throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( const str_type & CommandLine , const str_type & ParameterName )::An error occured" ) , 0 ) );
01723 }
01724 }
01725
01726 template< class str_type > bool Parsers::CommandLineParameterExists( int argc , const char * * argv , const str_type & ParameterName )
01727 {
01728 try
01729 {
01730 GetCommandLineParameterExists( ParameterName.c_str() );
01731 }
01732 catch( nitro::exception e )
01733 {
01734 throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( int argc , const char * * argv , const str_type & ParameterName )::" ) + e.what() , e.code() ) );
01735 }
01736 catch( ... )
01737 {
01738 throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( int argc , const char * * argv , const str_type & ParameterName )::An error occured" ) , 0 ) );
01739 }
01740 }
01741
01742 template< class str_type > bool Parsers::BeginsWith( const str_type & String , const str_type & Prefix )
01743 {
01744 try
01745 {
01746 return( BeginsWith( String.c_str() , Prefix.c_str() ) );
01747 }
01748 catch( nitro::exception e )
01749 {
01750 throw( nitro::exception( std::string( "Parsers::BeginsWith( const str_type & String , const str_type & Prefix )::" ) + e.what() , e.code() ) );
01751 }
01752 catch( ... )
01753 {
01754 throw( nitro::exception( std::string( "Parsers::BeginsWith( const str_type & String , const str_type & Prefix )::An error occured while validating prefix " ) + Prefix + " for string " + String , 0 ) );
01755 }
01756 }
01757
01758 template< class str_type > bool Parsers::EndsWith( const str_type & String , const str_type & Suffix )
01759 {
01760 try
01761 {
01762 return( EndsWith( String.c_str() , Suffix.c_str() ) );
01763 }
01764 catch( nitro::exception e )
01765 {
01766 throw( nitro::exception( std::string( "Parsers::EndsWith( const str_type & String , const str_type & Suffix )::" ) + e.what() , e.code() ) );
01767 }
01768 catch( ... )
01769 {
01770 throw( nitro::exception( std::string( "Parsers::EndsWith( const str_type & String , const str_type & Suffix )::An error occured" ) , 0 ) );
01771 }
01772 }
01773
01774 template< class Iter > std::string Parsers::GetCommonPrefix( const Iter & IterBegin , const Iter & IterEnd )
01775 {
01776 try
01777 {
01778 std::string Prefix( * IterBegin );
01779
01780 for( Iter i( IterBegin ) ; i != IterEnd ; i++ )
01781 {
01782 for( std::size_t j( 0 ) ; j < i->size() && j < Prefix.size() ; j++ )
01783 {
01784 if( Prefix[ j ] != ( * i )[ j ] )
01785 {
01786 Prefix.resize( j );
01787 }
01788 }
01789 }
01790
01791 return( Prefix );
01792 }
01793 catch( nitro::exception e )
01794 {
01795 throw( nitro::exception( std::string( "Parsers::GetCommonPrefix( const Iter & IterBegin , const Iter & IterEnd )::" ) + e.what() , e.code() ) );
01796 }
01797 catch( ... )
01798 {
01799 throw( nitro::exception( std::string( "Parsers::GetCommonPrefix( const Iter & IterBegin , const Iter & IterEnd )::An error occured" ) , 1 ) );
01800 }
01801 }
01802
01803 template< class Iter1 , class Iter2 >int Parsers::FindSubSequence( const Iter1 & Begin , const Iter1 & End , const Iter2 & SequenceBegin , const Iter2 & SequenceEnd )
01804 {
01805 try
01806 {
01807 for( Iter1 i( Begin ) ; i != End ; i++ )
01808 {
01809 if( * i == * SequenceBegin )
01810 {
01811 Iter2 j( SequenceBegin );
01812 for( ; i != End && j != SequenceEnd && * i == * j ; j++ , i++ );
01813
01814 if( j == SequenceEnd )
01815 {
01816 return( ( int )( i - Begin ) );
01817 }
01818 }
01819 }
01820
01821 return( -1 );
01822 }
01823 catch( nitro::exception e )
01824 {
01825 throw( nitro::exception( std::string( "Parsers::FindSubSequence( const Iter1 & Begin , const Iter1 & End , const Iter2 & SequenceBegin , const Iter2 & SequenceEnd )::" ) + e.what() , e.code() ) );
01826 }
01827 catch( ... )
01828 {
01829 throw( nitro::exception( std::string( "Parsers::FindSubSequence( const Iter1 & Begin , const Iter1 & End , const Iter2 & SequenceBegin , const Iter2 & SequenceEnd )::An error occured" ) , 1 ) );
01830 }
01831 }
01832
01843 const std::size_t OCTAL = 8;
01844
01855 const std::size_t DECIMAL = 10;
01856
01867 const std::size_t HEXADECIMAL = 16;
01868
01881 class STRING_UTILITIES_DLL_ENTITY Converters{
01882 public:
01883
01910 static int atoi( const std::string & StringValue , const std::size_t & Mode = nitro::DECIMAL );
01911
01934 template< class str_type >static double atof( str_type StringValue );
01935
01958 static double atof( const char * StringValue );
01959
01990 template< class str_type >static str_type itoa( int Value , str_type & RetValue , const std::size_t & Mode = nitro::DECIMAL );
01991
02018 static void itoa( int Value , char * OutputBuffer , const std::size_t & Mode = nitro::DECIMAL );
02019
02046 template< class str_type >static str_type ftoa( double Value , str_type & RetValue );
02047
02070 static void ftoa( double Value , char * OutputBuffer );
02071
02072 };
02073
02074 template< class str_type > double Converters::atof( str_type StringValue )
02075 {
02076 try
02077 {
02078 return( ::atof( StringValue.c_str() ) );
02079 }
02080 catch( nitro::exception e )
02081 {
02082 throw( nitro::exception( std::string( "Converters::atof( str_type StringValue )::" ) + e.what() , e.code() ) );
02083 }
02084 catch( ... )
02085 {
02086 throw( nitro::exception( std::string( "Converters::atof( str_type StringValue )::An error occured" ) , 0 ) );
02087 }
02088 }
02089
02090 template< class str_type > str_type Converters::itoa( int Value , str_type & RetValue , const std::size_t & Mode )
02091 {
02092 try
02093 {
02094 char Buffer[ 128 ];
02095
02096 memset( Buffer , 0 , 128 );
02097
02098 itoa( Value , Buffer , Mode );
02099
02100 RetValue = Buffer;
02101
02102 return( RetValue );
02103 }
02104 catch( nitro::exception e )
02105 {
02106 throw( nitro::exception( std::string( "Converters::itoa( int Value , str_type & RetValue , const std::size_t & Mode /* = nitro::DECIMAL */ )::" ) + e.what() , e.code() ) );
02107 }
02108 catch( ... )
02109 {
02110 throw( nitro::exception( std::string( "Converters::itoa( int Value , str_type & RetValue , const std::size_t & Mode /* = nitro::DECIMAL */ )::An error occured" ) , 0 ) );
02111 }
02112 }
02113
02114 template< class str_type > str_type Converters::ftoa( double Value , str_type & RetValue )
02115 {
02116 try
02117 {
02118 char Buffer[ 128 ];
02119
02120 memset( Buffer , 0 , 128 );
02121
02122 ftoa( Value , Buffer );
02123
02124 RetValue = Buffer;
02125
02126 return( RetValue );
02127 }
02128 catch( nitro::exception e )
02129 {
02130 throw( nitro::exception( std::string( "Converters::ftoa( double Value , str_type & RetValue )::" ) + e.what() , e.code() ) );
02131 }
02132 catch( ... )
02133 {
02134 throw( nitro::exception( std::string( "Converters::ftoa( double Value , str_type & RetValue )::An error occured" ) , 0 ) );
02135 }
02136 }
02137 }
02138
02139 #endif