X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2FcpPlugins%2FUtilities.h;fp=lib%2FcpPlugins%2FUtilities.h;h=b919c202adbc4f15fc38696a0161a800fdd2d82e;hb=201c5026430f9bcc33f9db6a39f5d03db096c860;hp=0000000000000000000000000000000000000000;hpb=aa77db36cc1d87ba014c982a235239c3c87224f3;p=cpPlugins.git diff --git a/lib/cpPlugins/Utilities.h b/lib/cpPlugins/Utilities.h new file mode 100644 index 0000000..b919c20 --- /dev/null +++ b/lib/cpPlugins/Utilities.h @@ -0,0 +1,135 @@ +#ifndef __CPPLUGINS__UTILITY__H__ +#define __CPPLUGINS__UTILITY__H__ + +#include +#include +#include +#include +#include + +// ------------------------------------------------------------------------- +#ifdef cpPlugins_SYS_WINDOWS +# define cpPlugins_STRTOK( A, B, N ) strtok_s( A, B, N ) +#else // cpPlugins_SYS_WINDOWS +# define cpPlugins_STRTOK( A, B, N ) std::strtok( A, B ) +#endif // cpPlugins_SYS_WINDOWS + +// ------------------------------------------------------------------------- +#define cpPlugins_CHRONO \ + std::chrono::duration_cast< std::chrono::milliseconds >( \ + std::chrono::system_clock::now( ).time_since_epoch( ) \ + ).count( ) + +// ------------------------------------------------------------------------- +#define cpPlugins_Id_Macro( N, C ) \ + public: \ + virtual const char* GetClassName( ) const ITK_OVERRIDE \ + { return( #N ); } \ + virtual const char* GetClassCategory( ) const ITK_OVERRIDE \ + { return( #C ); } + + +// ------------------------------------------------------------------------- +namespace cpPlugins +{ + // ----------------------------------------------------------------------- + inline bool IsPathSeparator( char c ) + { +#ifdef cpPlugins_SYS_WINDOWS + return( c == '\\' || c == cpPlugins_PATH_SEPARATOR ); +#else // cpPlugins_SYS_WINDOWS + return( c == cpPlugins_PATH_SEPARATOR ); +#endif // cpPlugins_SYS_WINDOWS + } + + // ----------------------------------------------------------------------- + inline bool IsBlank( const char& v ) + { + return( v == ' ' || v == '\t' || v == '\n' || v == '\r' ); + } + + // ----------------------------------------------------------------------- + template< class _TTokens > + inline void TokenizeString( + _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_STRTOK( buffer, delims.c_str( ), &next ); + while( it != NULL ) + { + tokens.push_back( std::string( it ) ); + it = cpPlugins_STRTOK( NULL, delims.c_str( ), &next ); + + } // elihw + delete [] buffer; + + } // fi + } + + // ----------------------------------------------------------------------- + inline std::string ReplaceString( + 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 ReadFileIntoBuffer( + 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 std::string CanonicalPath( const std::string& path ) + { + std::string ret = ""; +#ifdef cpPlugins_SYS_WINDOWS + TCHAR buffer[ 4096 ] = TEXT( "" ); + TCHAR** lppPart = { NULL }; + GetFullPathName( path.c_str( ), 4096, buffer, lppPart ); + ret = std::string( buffer ); +#else // cpPlugins_SYS_WINDOWS + char* canonical_path = realpath( path.c_str( ), NULL ); + if( canonical_path != NULL ) + { + ret = canonical_path; + free( canonical_path ); + + } // fi +#endif // cpPlugins_SYS_WINDOWS + return( ret ); + } + +} // ecapseman + +#endif // __CPPLUGINS__UTILITY__H__ + +// eof - $RCSfile$