]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/Plugins.cxx
Windows compilation: 3/3
[cpPlugins.git] / lib / cpPlugins / Interface / Plugins.cxx
index 8887f89f25f673d98f79ef0244367f7c847a2b2e..42ab2cc77ded0fd88249a1c6a8bf9f6b8b1b70d5 100644 (file)
@@ -91,17 +91,7 @@ AddEnvironments( const std::string& new_environment )
     std::stringstream dir;
     dir << cpExtensions::CanonicalPath( *i );
     if( dir.str( ) != "" )
-    {
-      if( !cpExtensions::IsPathSeparator( dir.str( ).back( ) ) )
-        dir << cpExtensions_PATH_SEPARATOR;
-      std::stringstream name;
-      name << dir.str( ) << cpPlugins_CONFIG;
-      std::ifstream check( name.str( ).c_str( ), std::ifstream::binary );
-      if( check )
-        this->m_Paths.insert( dir.str( ) );
-      check.close( );
-
-    } // fi
+      this->m_Paths.insert( dir.str( ) );
 
   } // rof
 }
@@ -162,45 +152,59 @@ LoadEnvironments( )
 void cpPlugins::Interface::Plugins::
 SaveEnvironments( const std::string& dir ) const
 {
-  if( this->m_Paths.size( ) > 0 )
-  {
-    std::stringstream buffer;
-    auto i = this->m_Paths.begin( );
-    for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i )
-      buffer << *i << std::endl;
+  std::stringstream buffer;
+  for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i )
+    buffer << *i << std::endl;
 
-    std::stringstream fname;
-    fname << dir;
-    if( !cpExtensions::IsPathSeparator( dir.back( ) ) )
-      fname << cpExtensions_PATH_SEPARATOR;
-    fname << cpPlugins_PATHS;
-    if( !cpExtensions::Write( buffer.str( ), fname.str( ) ) )
-      throw std::runtime_error( "Error writing environment file." );
-  }
-  else
-    throw std::runtime_error( "No paths to save." );
+  std::stringstream fname;
+  fname << dir;
+  if( !cpExtensions::IsPathSeparator( dir.back( ) ) )
+    fname << cpExtensions_PATH_SEPARATOR;
+  fname << cpPlugins_PATHS;
+  if( !cpExtensions::Write( buffer.str( ), fname.str( ) ) )
+    throw std::runtime_error( "Error writing environment file." );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Plugins::
 OpenEnvironments( const std::string& dir )
 {
-  std::stringstream fname;
-  fname << dir;
-  if( !cpExtensions::IsPathSeparator( dir.back( ) ) )
-    fname << cpExtensions_PATH_SEPARATOR;
-  fname << cpPlugins_PATHS;
-  std::string buffer;
-  if( cpExtensions::Read( buffer, fname.str( ) ) )
+  std::vector< std::string > tokens;
+  cpExtensions::Tokenize( tokens, dir, cpPlugins_ENV_SEPARATOR );
+  for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
   {
-    std::istringstream input( buffer );
-    std::stringstream paths;
-    for( std::string line; std::getline( input, line ); )
-      paths << line << cpPlugins_ENV_SEPARATOR;
-    this->AddEnvironments( paths.str( ) );
-  }
-  else
-    throw std::runtime_error( "Error opening environment file." );
+    std::stringstream fname;
+    fname << *tIt;
+    if( !cpExtensions::IsPathSeparator( dir.back( ) ) )
+      fname << cpExtensions_PATH_SEPARATOR;
+    fname << cpPlugins_PATHS;
+    std::string buffer;
+    if( cpExtensions::Read( buffer, fname.str( ) ) )
+    {
+      std::istringstream input( buffer );
+      std::stringstream paths;
+      for( std::string line; std::getline( input, line ); )
+        paths << line << cpPlugins_ENV_SEPARATOR;
+      this->AddEnvironments( paths.str( ) );
+    }
+    else
+    {
+      bool success = true;
+      try
+      {
+        this->LoadDirectory( dir );
+      }
+      catch( ... )
+      {
+        success = false;
+
+      } // yrt
+      if( success )
+        this->AddEnvironments( dir );
+
+    } // fi
+
+  } // rof
 }
 
 // -------------------------------------------------------------------------
@@ -420,12 +424,33 @@ cpPlugins::Interface::Plugins::
 Plugins( )
   : Superclass( )
 {
+#ifdef cpPlugins_OS_Windows
+  char* p;
+  size_t size;
+  _dupenv_s( &p, &size, cpPlugins_PATHS );
+#else // cpPlugins_OS_Windows
   char* p = std::getenv( cpPlugins_PATHS );
+#endif // cpPlugins_OS_Windows
   std::stringstream str;
   if( p != NULL )
     str << p << cpPlugins_ENV_SEPARATOR;
   str << ".";
-  this->AddEnvironments( str.str( ) );
+  this->OpenEnvironments( str.str( ) );
+
+  // Try to read locally defined paths
+  /* TODO
+     std::vector< std::string > tokens;
+     cpExtensions::Tokenize( tokens, str.str( ), cpPlugins_ENV_SEPARATOR );
+     for( auto t = tokens.begin( ); t != tokens.end( ); ++t )
+     {
+     try
+     {
+     this->OpenEnvironments( *t );
+     }
+     catch( ... ) { }
+
+     } // rof
+  */
 }
 
 // -------------------------------------------------------------------------