]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins_Config.h
More macos issues...
[cpPlugins.git] / lib / cpPlugins_Config.h
index 84314e886c2e40469e28e789235d3b943cae1055..7a228b10b10ee6de452118e7badb7e00330d685c 100644 (file)
 #ifndef __CPPLUGINS_CONFIG__H__
 #define __CPPLUGINS_CONFIG__H__
 
+/*
+ * =========================================================================
+ * ITK related macros
+ * =========================================================================
+ */
 #include <itkMacro.h>
-
 #define ITK_MANUAL_INSTANTIATION
-
 #ifndef ITK_DELETE_FUNCTION
 #  define ITK_DELETE_FUNCTION
 #endif // ITK_DELETE_FUNCTION
-
 #ifndef ITK_OVERRIDE
 #  define ITK_OVERRIDE
 #endif // ITK_OVERRIDE
 
+/*
+ * =========================================================================
+ * Identify OS
+ * =========================================================================
+ */
+#if defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ )
+#  define cpPlugins_SYS_WINDOWS
+#  define cpPlugins_LIB_PREFIX ""
+#  define cpPlugins_LIB_EXT "dll"
+#  ifndef WIN32_LEAN_AND_MEAN
+#    define WIN32_LEAN_AND_MEAN
+#  endif
+#  define NOMINMAX
+#  include <windows.h>
+#  include <tchar.h>
+#elif defined( linux ) || defined( __linux )
+#  define cpPlugins_SYS_LINUX
+#  define cpPlugins_LIB_PREFIX "lib"
+#  define cpPlugins_LIB_EXT "so"
+#elif defined( __APPLE__ ) || defined( MACOSX ) || defined( macintosh ) || defined( Macintosh )
+#  define cpPlugins_SYS_MACOS
+#  define cpPlugins_LIB_PREFIX "lib"
+#  define cpPlugins_LIB_EXT "dylib"
+#elif defined( __FreeBSD__ ) || defined( __FreeBSD_kernel__ )
+#  define cpPlugins_SYS_FREEBSD
+#  define cpPlugins_LIB_PREFIX "lib"
+#  define cpPlugins_LIB_EXT "so"
+#else
+#  error "This operating system is not supported by cpPlugins"
+#endif
+
+/*
+ * =========================================================================
+ * Some helper functions
+ * =========================================================================
+ */
+
+#include <cstring>
+#include <fstream>
+#include <iostream>
+#include <string>
+
+#ifdef cpPlugins_SYS_WINDOWS
+#  define cpPlugins_STRTOK( A, B, N ) strtok_s( A, B, N )
+#else // cpPlugins_SYS_WINDOWS
+#  define cpPlugins_STRTOK( A, B, N ) std::strtok( A, B )
+#endif // cpPlugins_SYS_WINDOWS
+
+namespace cpPlugins
+{
+  struct IsSeparator
+  {
+    // ---------------------------------------------------------------------
+    inline bool operator()( char c ) const
+      {
+#ifdef cpPlugins_SYS_WINDOWS
+        return( c == '\\' || c == '/' );
+#else // cpPlugins_SYS_WINDOWS
+        return( c == '/' );
+#endif // cpPlugins_SYS_WINDOWS
+      }
+  };
+
+  // -----------------------------------------------------------------------
+  inline bool IsBlank( const char& value )
+  {
+    return( value == ' ' || value == '\t' || value == '\n' || value == '\r' );
+  }
+
+  // -----------------------------------------------------------------------
+  template< class _TTokens >
+  inline void TokenizeString(
+    _TTokens& tokens, const std::string& str, const std::string& delims
+    )
+  {
+    tokens.clear( );
+    if( str.size( ) > 0 )
+    {
+      auto ssize = str.size( );
+      char* buffer = new char[ ssize + 1 ];
+      for( unsigned long i = 0; i < ssize; ++i )
+        buffer[ i ] = str[ i ];
+      buffer[ ssize ] = '\0';
+      char* next;
+      char* it = cpPlugins_STRTOK( buffer, delims.c_str( ), &next );
+      while( it != NULL )
+      {
+        tokens.push_back( std::string( it ) );
+        it = cpPlugins_STRTOK( NULL, delims.c_str( ), &next );
+
+      } // elihw
+      delete [] buffer;
+
+    } // fi
+  }
+
+  // -----------------------------------------------------------------------
+  inline std::string ReplaceString(
+    const std::string& str, const std::string& sub, const std::string& nsub
+    )
+  {
+    std::string res = str;
+    size_t index;
+    while( ( index = res.find( sub ) ) != std::string::npos )
+      res.replace( index, sub.size( ), nsub );
+    return( res );
+  }
+
+  // -----------------------------------------------------------------------
+  inline bool ReadFileIntoString( std::string& buffer, const std::string& fname )
+  {
+    buffer = "";
+    std::ifstream file_stream( fname.c_str( ) );
+    if( !file_stream )
+      return( false );
+    file_stream.seekg( 0, std::ios::end );
+    buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
+    file_stream.seekg( 0, std::ios::beg );
+    buffer.assign(
+      ( std::istreambuf_iterator< char >( file_stream ) ),
+      std::istreambuf_iterator< char >( )
+      );
+    file_stream.close( );
+    return( true );
+  }
+
+  // -----------------------------------------------------------------------
+  inline std::string CanonicalPath( const std::string& path )
+  {
+    std::string ret = "";
+#ifdef cpPlugins_SYS_WINDOWS
+    TCHAR  buffer[ 4096 ] = TEXT( "" );
+    TCHAR** lppPart = { NULL };
+    GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
+    ret = std::string( buffer );
+#else // cpPlugins_SYS_WINDOWS
+    char* canonical_path = realpath( path.c_str( ), NULL );
+    if( canonical_path != NULL )
+    {
+      ret = canonical_path;
+      free( canonical_path );
+
+    } // fi
+#endif // cpPlugins_SYS_WINDOWS
+    return( ret );
+  }
+
+} // ecapseman
+
 #endif // __CPPLUGINS_CONFIG__H__
 
 // eof - $RCSfile$