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