]> Creatis software - cpPlugins.git/blob - lib/cpPlugins_Config.h
...
[cpPlugins.git] / lib / cpPlugins_Config.h
1 #ifndef __CPPLUGINS_CONFIG__H__
2 #define __CPPLUGINS_CONFIG__H__
3
4 /*
5  * =========================================================================
6  * ITK related macros
7  * =========================================================================
8  */
9 #include <itkMacro.h>
10 #define ITK_MANUAL_INSTANTIATION
11 #ifndef ITK_DELETE_FUNCTION
12 #  define ITK_DELETE_FUNCTION
13 #endif // ITK_DELETE_FUNCTION
14 #ifndef ITK_OVERRIDE
15 #  define ITK_OVERRIDE
16 #endif // ITK_OVERRIDE
17
18 /*
19  * =========================================================================
20  * VTK related macros
21  * =========================================================================
22  */
23 #include <vtkConfigure.h>
24 #ifndef VTK_OVERRIDE
25 #  define VTK_OVERRIDE
26 #endif // VTK_OVERRIDE
27
28 /*
29  * =========================================================================
30  * Identify OS
31  * =========================================================================
32  */
33 #if defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ )
34 #  define cpPlugins_SYS_WINDOWS
35 #  define cpPlugins_LIB_PREFIX ""
36 #  define cpPlugins_LIB_EXT "dll"
37 #  define cpPlugins_SEPARATOR ";"
38 #  ifndef WIN32_LEAN_AND_MEAN
39 #    define WIN32_LEAN_AND_MEAN
40 #  endif
41 #  define NOMINMAX
42 #  include <windows.h>
43 #  include <tchar.h>
44 #elif defined( linux ) || defined( __linux )
45 #  define cpPlugins_SYS_LINUX
46 #  define cpPlugins_LIB_PREFIX "lib"
47 #  define cpPlugins_LIB_EXT "so"
48 #  define cpPlugins_SEPARATOR ":"
49 #elif defined( __APPLE__ ) || defined( MACOSX ) || defined( macintosh ) || defined( Macintosh )
50 #  define cpPlugins_SYS_MACOS
51 #  define cpPlugins_LIB_PREFIX "lib"
52 #  define cpPlugins_LIB_EXT "dylib"
53 #  define cpPlugins_SEPARATOR ":"
54 #elif defined( __FreeBSD__ ) || defined( __FreeBSD_kernel__ )
55 #  define cpPlugins_SYS_FREEBSD
56 #  define cpPlugins_LIB_PREFIX "lib"
57 #  define cpPlugins_LIB_EXT "so"
58 #  define cpPlugins_SEPARATOR ":"
59 #else
60 #  error "This operating system is not supported by cpPlugins"
61 #endif
62
63 /*
64  * =========================================================================
65  * Some helper functions
66  * =========================================================================
67  */
68
69 #include <chrono>
70 #include <cstring>
71 #include <fstream>
72 #include <iostream>
73 #include <string>
74
75 // -------------------------------------------------------------------------
76 #ifdef cpPlugins_SYS_WINDOWS
77 #  define cpPlugins_STRTOK( A, B, N ) strtok_s( A, B, N )
78 #else // cpPlugins_SYS_WINDOWS
79 #  define cpPlugins_STRTOK( A, B, N ) std::strtok( A, B )
80 #endif // cpPlugins_SYS_WINDOWS
81
82 // -------------------------------------------------------------------------
83 #define cpPlugins_CHRONO                                                \
84   std::chrono::duration_cast< std::chrono::milliseconds >(              \
85     std::chrono::system_clock::now( ).time_since_epoch( )               \
86     ).count( )
87
88 namespace cpPlugins
89 {
90   struct IsSeparator
91   {
92     // ---------------------------------------------------------------------
93     inline bool operator()( char c ) const
94       {
95 #ifdef cpPlugins_SYS_WINDOWS
96         return( c == '\\' || c == '/' );
97 #else // cpPlugins_SYS_WINDOWS
98         return( c == '/' );
99 #endif // cpPlugins_SYS_WINDOWS
100       }
101   };
102
103   // -----------------------------------------------------------------------
104   inline bool IsBlank( const char& value )
105   {
106     return( value == ' ' || value == '\t' || value == '\n' || value == '\r' );
107   }
108
109   // -----------------------------------------------------------------------
110   template< class _TTokens >
111   inline void TokenizeString(
112     _TTokens& tokens, const std::string& str, const std::string& delims
113     )
114   {
115     tokens.clear( );
116     if( str.size( ) > 0 )
117     {
118       auto ssize = str.size( );
119       char* buffer = new char[ ssize + 1 ];
120       for( unsigned long i = 0; i < ssize; ++i )
121         buffer[ i ] = str[ i ];
122       buffer[ ssize ] = '\0';
123       char* next;
124       char* it = cpPlugins_STRTOK( buffer, delims.c_str( ), &next );
125       while( it != NULL )
126       {
127         tokens.push_back( std::string( it ) );
128         it = cpPlugins_STRTOK( NULL, delims.c_str( ), &next );
129
130       } // elihw
131       delete [] buffer;
132
133     } // fi
134   }
135
136   // -----------------------------------------------------------------------
137   inline std::string ReplaceString(
138     const std::string& str, const std::string& sub, const std::string& nsub
139     )
140   {
141     std::string res = str;
142     size_t index;
143     while( ( index = res.find( sub ) ) != std::string::npos )
144       res.replace( index, sub.size( ), nsub );
145     return( res );
146   }
147
148   // -----------------------------------------------------------------------
149   inline bool ReadFileIntoString( std::string& buffer, const std::string& fname )
150   {
151     buffer = "";
152     std::ifstream file_stream( fname.c_str( ) );
153     if( !file_stream )
154       return( false );
155     file_stream.seekg( 0, std::ios::end );
156     buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
157     file_stream.seekg( 0, std::ios::beg );
158     buffer.assign(
159       ( std::istreambuf_iterator< char >( file_stream ) ),
160       std::istreambuf_iterator< char >( )
161       );
162     file_stream.close( );
163     return( true );
164   }
165
166   // -----------------------------------------------------------------------
167   inline std::string CanonicalPath( const std::string& path )
168   {
169     std::string ret = "";
170 #ifdef cpPlugins_SYS_WINDOWS
171     TCHAR  buffer[ 4096 ] = TEXT( "" );
172     TCHAR** lppPart = { NULL };
173     GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
174     ret = std::string( buffer );
175 #else // cpPlugins_SYS_WINDOWS
176     char* canonical_path = realpath( path.c_str( ), NULL );
177     if( canonical_path != NULL )
178     {
179       ret = canonical_path;
180       free( canonical_path );
181
182     } // fi
183 #endif // cpPlugins_SYS_WINDOWS
184     return( ret );
185   }
186
187 } // ecapseman
188
189 #endif // __CPPLUGINS_CONFIG__H__
190
191 // eof - $RCSfile$