1 #ifndef __cpPlugins__bash__Utility__h__
2 #define __cpPlugins__bash__Utility__h__
8 // -------------------------------------------------------------------------
9 #define cpPlugins_bash_OS_@CMAKE_SYSTEM_NAME@
10 #ifdef cpPlugins_bash_OS_Windows
11 # define cpPlugins_bash_STRTOK( A, B, N ) strtok_s( A, B, N )
12 # define cpPlugins_bash_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O );
13 #else // cpPlugins_bash_OS_Windows
14 # define cpPlugins_bash_STRTOK( A, B, N ) std::strtok( A, B )
15 # define cpPlugins_bash_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
16 #endif // cpPlugins_bash_OS_Windows
18 // -------------------------------------------------------------------------
19 namespace cpPlugins_bash
21 // -----------------------------------------------------------------------
22 template< class _TTokens >
24 _TTokens& tokens, const std::string& str, const std::string& delims
30 auto ssize = str.size( );
31 char* buffer = new char[ ssize + 1 ];
32 for( unsigned long i = 0; i < ssize; ++i )
33 buffer[ i ] = str[ i ];
34 buffer[ ssize ] = '\0';
36 char* it = cpPlugins_bash_STRTOK( buffer, delims.c_str( ), &next );
39 tokens.push_back( std::string( it ) );
40 it = cpPlugins_bash_STRTOK( NULL, delims.c_str( ), &next );
48 // -----------------------------------------------------------------------
49 inline std::string Replace(
50 const std::string& str, const std::string& sub, const std::string& nsub
53 std::string res = str;
55 while( ( index = res.find( sub ) ) != std::string::npos )
56 res.replace( index, sub.size( ), nsub );
60 // -----------------------------------------------------------------------
61 inline bool Read( std::string& buffer, const std::string& fname )
64 std::ifstream file_stream( fname.c_str( ) );
67 file_stream.seekg( 0, std::ios::end );
68 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
69 file_stream.seekg( 0, std::ios::beg );
71 ( std::istreambuf_iterator< char >( file_stream ) ),
72 std::istreambuf_iterator< char >( )
78 // -----------------------------------------------------------------------
79 inline bool Write( const std::string& buffer, const std::string& fname )
81 std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
84 file_stream.write( buffer.c_str( ), buffer.size( ) );
90 #endif // __cpPlugins__bash__Utility__h__