1 #ifndef __cpPlugins__bash__Utility__h__
2 #define __cpPlugins__bash__Utility__h__
8 // -------------------------------------------------------------------------
9 #ifdef @prj_NAME@_Windows
10 # define cpExtensions_STRTOK( A, B, N ) strtok_s( A, B, N )
11 # define cpExtensions_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O );
13 #else // @prj_NAME@_Windows
14 # define cpExtensions_STRTOK( A, B, N ) std::strtok( A, B )
15 # define cpExtensions_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
16 #endif // @prj_NAME@_Windows
19 // -------------------------------------------------------------------------
20 namespace cpPlugins_bash
22 // -----------------------------------------------------------------------
23 template< class _TTokens >
25 _TTokens& tokens, const std::string& str, const std::string& delims
31 auto ssize = str.size( );
32 char* buffer = new char[ ssize + 1 ];
33 for( unsigned long i = 0; i < ssize; ++i )
34 buffer[ i ] = str[ i ];
35 buffer[ ssize ] = '\0';
37 char* it = cpExtensions_STRTOK( buffer, delims.c_str( ), &next );
40 tokens.push_back( std::string( it ) );
41 it = cpExtensions_STRTOK( NULL, delims.c_str( ), &next );
49 // -----------------------------------------------------------------------
50 inline std::string Replace(
51 const std::string& str, const std::string& sub, const std::string& nsub
54 std::string res = str;
56 while( ( index = res.find( sub ) ) != std::string::npos )
57 res.replace( index, sub.size( ), nsub );
61 // -----------------------------------------------------------------------
62 inline bool Read( std::string& buffer, const std::string& fname )
65 std::ifstream file_stream( fname.c_str( ) );
68 file_stream.seekg( 0, std::ios::end );
69 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
70 file_stream.seekg( 0, std::ios::beg );
72 ( std::istreambuf_iterator< char >( file_stream ) ),
73 std::istreambuf_iterator< char >( )
79 // -----------------------------------------------------------------------
80 inline bool Write( const std::string& buffer, const std::string& fname )
82 std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
85 file_stream.write( buffer.c_str( ), buffer.size( ) );
91 #endif // __cpPlugins__bash__Utility__h__