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