#ifndef __cpPlugins__bash__Utility__h__ #define __cpPlugins__bash__Utility__h__ #include #include #include // ------------------------------------------------------------------------- #define cpPlugins_bash_OS_@CMAKE_SYSTEM_NAME@ #ifdef cpPlugins_bash_OS_Windows # define cpPlugins_bash_STRTOK( A, B, N ) strtok_s( A, B, N ) # define cpPlugins_bash_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O ); #else // cpPlugins_bash_OS_Windows # define cpPlugins_bash_STRTOK( A, B, N ) std::strtok( A, B ) # define cpPlugins_bash_SPRINTF( B, S, O ) std::sprintf( B, "%s", O ); #endif // cpPlugins_bash_OS_Windows // ------------------------------------------------------------------------- namespace cpPlugins_bash { // ----------------------------------------------------------------------- template< class _TTokens > inline void Tokenize( _TTokens& tokens, const std::string& str, const std::string& delims ) { tokens.clear( ); if( str.size( ) > 0 ) { auto ssize = str.size( ); char* buffer = new char[ ssize + 1 ]; for( unsigned long i = 0; i < ssize; ++i ) buffer[ i ] = str[ i ]; buffer[ ssize ] = '\0'; char* next; char* it = cpPlugins_bash_STRTOK( buffer, delims.c_str( ), &next ); while( it != NULL ) { tokens.push_back( std::string( it ) ); it = cpPlugins_bash_STRTOK( NULL, delims.c_str( ), &next ); } // elihw delete [] buffer; } // fi } // ----------------------------------------------------------------------- inline std::string Replace( const std::string& str, const std::string& sub, const std::string& nsub ) { std::string res = str; size_t index; while( ( index = res.find( sub ) ) != std::string::npos ) res.replace( index, sub.size( ), nsub ); return( res ); } // ----------------------------------------------------------------------- inline bool Read( std::string& buffer, const std::string& fname ) { buffer = ""; std::ifstream file_stream( fname.c_str( ) ); if( !file_stream ) return( false ); file_stream.seekg( 0, std::ios::end ); buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) ); file_stream.seekg( 0, std::ios::beg ); buffer.assign( ( std::istreambuf_iterator< char >( file_stream ) ), std::istreambuf_iterator< char >( ) ); file_stream.close( ); return( true ); } // ----------------------------------------------------------------------- inline bool Write( const std::string& buffer, const std::string& fname ) { std::ofstream file_stream( fname.c_str( ), std::ofstream::binary ); if( !file_stream ) return( false ); file_stream.write( buffer.c_str( ), buffer.size( ) ); return( true ); } } // ecapseman #endif // __cpPlugins__bash__Utility__h__ // eof - $RCSfile$