1 #ifndef __CPPLUGINS__UTILITY__H__
2 #define __CPPLUGINS__UTILITY__H__
4 #include <cpPlugins/Config.h>
10 // -------------------------------------------------------------------------
11 #ifdef cpPlugins_SYS_WINDOWS
12 # define cpPlugins_STRTOK( A, B, N ) strtok_s( A, B, N )
13 #else // cpPlugins_SYS_WINDOWS
14 # define cpPlugins_STRTOK( A, B, N ) std::strtok( A, B )
15 #endif // cpPlugins_SYS_WINDOWS
17 // -------------------------------------------------------------------------
18 #define cpPlugins_CHRONO \
19 std::chrono::duration_cast< std::chrono::milliseconds >( \
20 std::chrono::system_clock::now( ).time_since_epoch( ) \
23 // -------------------------------------------------------------------------
24 #define cpPlugins_Id_Macro( N, C ) \
26 virtual const char* GetClassName( ) const ITK_OVERRIDE \
28 virtual const char* GetClassCategory( ) const ITK_OVERRIDE \
32 // -------------------------------------------------------------------------
35 // -----------------------------------------------------------------------
36 inline bool IsPathSeparator( char c )
38 #ifdef cpPlugins_SYS_WINDOWS
39 return( c == '\\' || c == cpPlugins_PATH_SEPARATOR );
40 #else // cpPlugins_SYS_WINDOWS
41 return( c == cpPlugins_PATH_SEPARATOR );
42 #endif // cpPlugins_SYS_WINDOWS
45 // -----------------------------------------------------------------------
46 inline bool IsBlank( const char& v )
48 return( v == ' ' || v == '\t' || v == '\n' || v == '\r' );
51 // -----------------------------------------------------------------------
52 template< class _TTokens >
53 inline void TokenizeString(
54 _TTokens& tokens, const std::string& str, const std::string& delims
60 auto ssize = str.size( );
61 char* buffer = new char[ ssize + 1 ];
62 for( unsigned long i = 0; i < ssize; ++i )
63 buffer[ i ] = str[ i ];
64 buffer[ ssize ] = '\0';
66 char* it = cpPlugins_STRTOK( buffer, delims.c_str( ), &next );
69 tokens.push_back( std::string( it ) );
70 it = cpPlugins_STRTOK( NULL, delims.c_str( ), &next );
78 // -----------------------------------------------------------------------
79 inline std::string ReplaceString(
80 const std::string& str, const std::string& sub, const std::string& nsub
83 std::string res = str;
85 while( ( index = res.find( sub ) ) != std::string::npos )
86 res.replace( index, sub.size( ), nsub );
90 // -----------------------------------------------------------------------
91 inline std::string CanonicalPath( const std::string& path )
94 #ifdef cpPlugins_SYS_WINDOWS
95 TCHAR buffer[ 4096 ] = TEXT( "" );
96 TCHAR** lppPart = { NULL };
97 GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
98 ret = std::string( buffer );
99 #else // cpPlugins_SYS_WINDOWS
100 char* canonical_path = realpath( path.c_str( ), NULL );
101 if( canonical_path != NULL )
103 ret = canonical_path;
104 free( canonical_path );
107 #endif // cpPlugins_SYS_WINDOWS
111 // -----------------------------------------------------------------------
112 inline bool WriteBufferToFile(
113 const std::string& buffer, const std::string& fname
116 std::ofstream file_stream(
117 CanonicalPath( fname ).c_str( ), std::ofstream::binary
121 file_stream.write( buffer.c_str( ), buffer.size( ) );
125 // -----------------------------------------------------------------------
126 inline bool ReadFileIntoBuffer(
127 std::string& buffer, const std::string& fname
131 std::ifstream file_stream(
132 CanonicalPath( fname ).c_str( ), std::ifstream::binary
136 file_stream.seekg( 0, std::ios::end );
137 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
138 file_stream.seekg( 0, std::ios::beg );
140 ( std::istreambuf_iterator< char >( file_stream ) ),
141 std::istreambuf_iterator< char >( )
143 file_stream.close( );
149 #endif // __CPPLUGINS__UTILITY__H__