]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Utilities.h
b919c202adbc4f15fc38696a0161a800fdd2d82e
[cpPlugins.git] / lib / cpPlugins / Utilities.h
1 #ifndef __CPPLUGINS__UTILITY__H__
2 #define __CPPLUGINS__UTILITY__H__
3
4 #include <cpPlugins/Config.h>
5 #include <chrono>
6 #include <cstring>
7 #include <fstream>
8 #include <iostream>
9
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
16
17 // -------------------------------------------------------------------------
18 #define cpPlugins_CHRONO                                                \
19   std::chrono::duration_cast< std::chrono::milliseconds >(              \
20     std::chrono::system_clock::now( ).time_since_epoch( )               \
21     ).count( )
22
23 // -------------------------------------------------------------------------
24 #define cpPlugins_Id_Macro( N, C )                                      \
25   public:                                                               \
26   virtual const char* GetClassName( ) const ITK_OVERRIDE                \
27   { return( #N ); }                                                     \
28   virtual const char* GetClassCategory( ) const ITK_OVERRIDE            \
29   { return( #C ); }
30
31
32 // -------------------------------------------------------------------------
33 namespace cpPlugins
34 {
35   // -----------------------------------------------------------------------
36   inline bool IsPathSeparator( char c )
37   {
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
43   }
44
45   // -----------------------------------------------------------------------
46   inline bool IsBlank( const char& v )
47   {
48     return( v == ' ' || v == '\t' || v == '\n' || v == '\r' );
49   }
50
51   // -----------------------------------------------------------------------
52   template< class _TTokens >
53   inline void TokenizeString(
54     _TTokens& tokens, const std::string& str, const std::string& delims
55     )
56   {
57     tokens.clear( );
58     if( str.size( ) > 0 )
59     {
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';
65       char* next;
66       char* it = cpPlugins_STRTOK( buffer, delims.c_str( ), &next );
67       while( it != NULL )
68       {
69         tokens.push_back( std::string( it ) );
70         it = cpPlugins_STRTOK( NULL, delims.c_str( ), &next );
71
72       } // elihw
73       delete [] buffer;
74
75     } // fi
76   }
77
78   // -----------------------------------------------------------------------
79   inline std::string ReplaceString(
80     const std::string& str, const std::string& sub, const std::string& nsub
81     )
82   {
83     std::string res = str;
84     size_t index;
85     while( ( index = res.find( sub ) ) != std::string::npos )
86       res.replace( index, sub.size( ), nsub );
87     return( res );
88   }
89
90   // -----------------------------------------------------------------------
91   inline bool ReadFileIntoBuffer(
92     std::string& buffer, const std::string& fname
93     )
94   {
95     buffer = "";
96     std::ifstream file_stream( fname.c_str( ) );
97     if( !file_stream )
98       return( false );
99     file_stream.seekg( 0, std::ios::end );
100     buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
101     file_stream.seekg( 0, std::ios::beg );
102     buffer.assign(
103       ( std::istreambuf_iterator< char >( file_stream ) ),
104       std::istreambuf_iterator< char >( )
105       );
106     file_stream.close( );
107     return( true );
108   }
109
110   // -----------------------------------------------------------------------
111   inline std::string CanonicalPath( const std::string& path )
112   {
113     std::string ret = "";
114 #ifdef cpPlugins_SYS_WINDOWS
115     TCHAR  buffer[ 4096 ] = TEXT( "" );
116     TCHAR** lppPart = { NULL };
117     GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
118     ret = std::string( buffer );
119 #else // cpPlugins_SYS_WINDOWS
120     char* canonical_path = realpath( path.c_str( ), NULL );
121     if( canonical_path != NULL )
122     {
123       ret = canonical_path;
124       free( canonical_path );
125
126     } // fi
127 #endif // cpPlugins_SYS_WINDOWS
128     return( ret );
129   }
130
131 } // ecapseman
132
133 #endif // __CPPLUGINS__UTILITY__H__
134
135 // eof - $RCSfile$