]> Creatis software - cpPlugins.git/blob - lib/cpPlugins_Config.h
More macos issues...
[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 <cstring>
56 #include <fstream>
57 #include <iostream>
58 #include <string>
59
60 #ifdef cpPlugins_SYS_WINDOWS
61 #  define cpPlugins_STRTOK( A, B, N ) strtok_s( A, B, N )
62 #else // cpPlugins_SYS_WINDOWS
63 #  define cpPlugins_STRTOK( A, B, N ) std::strtok( A, B )
64 #endif // cpPlugins_SYS_WINDOWS
65
66 namespace cpPlugins
67 {
68   struct IsSeparator
69   {
70     // ---------------------------------------------------------------------
71     inline bool operator()( char c ) const
72       {
73 #ifdef cpPlugins_SYS_WINDOWS
74         return( c == '\\' || c == '/' );
75 #else // cpPlugins_SYS_WINDOWS
76         return( c == '/' );
77 #endif // cpPlugins_SYS_WINDOWS
78       }
79   };
80
81   // -----------------------------------------------------------------------
82   inline bool IsBlank( const char& value )
83   {
84     return( value == ' ' || value == '\t' || value == '\n' || value == '\r' );
85   }
86
87   // -----------------------------------------------------------------------
88   template< class _TTokens >
89   inline void TokenizeString(
90     _TTokens& tokens, const std::string& str, const std::string& delims
91     )
92   {
93     tokens.clear( );
94     if( str.size( ) > 0 )
95     {
96       auto ssize = str.size( );
97       char* buffer = new char[ ssize + 1 ];
98       for( unsigned long i = 0; i < ssize; ++i )
99         buffer[ i ] = str[ i ];
100       buffer[ ssize ] = '\0';
101       char* next;
102       char* it = cpPlugins_STRTOK( buffer, delims.c_str( ), &next );
103       while( it != NULL )
104       {
105         tokens.push_back( std::string( it ) );
106         it = cpPlugins_STRTOK( NULL, delims.c_str( ), &next );
107
108       } // elihw
109       delete [] buffer;
110
111     } // fi
112   }
113
114   // -----------------------------------------------------------------------
115   inline std::string ReplaceString(
116     const std::string& str, const std::string& sub, const std::string& nsub
117     )
118   {
119     std::string res = str;
120     size_t index;
121     while( ( index = res.find( sub ) ) != std::string::npos )
122       res.replace( index, sub.size( ), nsub );
123     return( res );
124   }
125
126   // -----------------------------------------------------------------------
127   inline bool ReadFileIntoString( std::string& buffer, const std::string& fname )
128   {
129     buffer = "";
130     std::ifstream file_stream( fname.c_str( ) );
131     if( !file_stream )
132       return( false );
133     file_stream.seekg( 0, std::ios::end );
134     buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
135     file_stream.seekg( 0, std::ios::beg );
136     buffer.assign(
137       ( std::istreambuf_iterator< char >( file_stream ) ),
138       std::istreambuf_iterator< char >( )
139       );
140     file_stream.close( );
141     return( true );
142   }
143
144   // -----------------------------------------------------------------------
145   inline std::string CanonicalPath( const std::string& path )
146   {
147     std::string ret = "";
148 #ifdef cpPlugins_SYS_WINDOWS
149     TCHAR  buffer[ 4096 ] = TEXT( "" );
150     TCHAR** lppPart = { NULL };
151     GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
152     ret = std::string( buffer );
153 #else // cpPlugins_SYS_WINDOWS
154     char* canonical_path = realpath( path.c_str( ), NULL );
155     if( canonical_path != NULL )
156     {
157       ret = canonical_path;
158       free( canonical_path );
159
160     } // fi
161 #endif // cpPlugins_SYS_WINDOWS
162     return( ret );
163   }
164
165 } // ecapseman
166
167 #endif // __CPPLUGINS_CONFIG__H__
168
169 // eof - $RCSfile$