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