]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/Plugins.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface / Plugins.cxx
index 8887f89f25f673d98f79ef0244367f7c847a2b2e..eabd53ef905e45048dd4530530b360b014a2bb43 100644 (file)
@@ -1,6 +1,6 @@
 #include <cpPlugins/Interface/Plugins.h>
 #include <cpPlugins/OS/DLLManager.h>
-#include <cpPlugins/Interface/Dirent.h>
+#include <cpPlugins/OS/DirContents.h>
 #include <cpExtensions/Utility.h>
 
 // -------------------------------------------------------------------------
@@ -82,67 +82,40 @@ GetFilters( const std::string& category ) const
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Plugins::
-AddEnvironments( const std::string& new_environment )
+AddEnvironments( const std::string& env )
 {
-  std::vector< std::string > tokens;
-  cpExtensions::Tokenize( tokens, new_environment, cpPlugins_ENV_SEPARATOR );
-  for( auto i = tokens.begin( ); i != tokens.end( ); ++i )
-  {
-    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
-
-  } // rof
+  std::vector< std::string > directories;
+  cpExtensions::Tokenize( directories, env, cpPlugins_ENV_SEPARATOR );
+  for( auto dir = directories.begin( ); dir != directories.end( ); ++dir )
+    this->m_Paths.insert( cpExtensions::CanonicalPath( *dir ) );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Plugins::
 LoadEnvironments( )
 {
-  std::stringstream all_errors;
+  std::set< std::string > libs;
   for( auto d = this->m_Paths.begin( ); d != this->m_Paths.end( ); ++d )
   {
-    std::stringstream name;
-    name << *d << cpPlugins_CONFIG;
+    std::stringstream fname;
+    fname << *d << cpPlugins_PATH_SEPARATOR << cpPlugins_CONFIG;
     std::string buffer;
-    if( cpExtensions::Read( buffer, name.str( ) ) )
+    if( cpExtensions::Read( buffer, fname.str( ) ) )
     {
       std::istringstream input( buffer );
       for( std::string line; std::getline( input, line ); )
       {
-        std::vector< std::string > tokens;
-        cpExtensions::Tokenize( tokens, line, "@" );
-        std::string library_file = "";
-        if( tokens[ 0 ] == "local" )
-          library_file =
-            cpExtensions::CanonicalPath(
-              *d + std::string( cpPlugins_LIB_PREFIX ) +
-              tokens[ 1 ] + std::string( cpPlugins_LIB_EXT )
-              );
-        else if( tokens[ 0 ] == "global" )
-          library_file = tokens[ 1 ];
-
-        if( library_file != "" )
+        auto pos = line.find( "*" );
+        if( pos != std::string::npos )
         {
-          std::string error = "";
-          void* hnd = cpPlugins::OS::DLLManager::Load( library_file, error );
-          if( hnd != NULL )
-            this->m_Libraries[ library_file ] = hnd;
-          else
-            all_errors << " ; " << error;
-
-        } // fi
+          std::string dname, fname;
+          cpPlugins::OS::SplitPath( dname, fname, line );
+          auto files = cpPlugins::OS::LoadDirContents( dname, false, fname );
+          for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
+            libs.insert( *fIt );
+        }
+        else
+          libs.insert( line );
 
       } // rof
 
@@ -150,57 +123,61 @@ LoadEnvironments( )
 
   } // rof
 
-  // Throw errors
-  if( all_errors.str( ) != "" )
-    throw std::runtime_error(
-      std::string( "Loading environment libraries errors: " ) +
-      all_errors.str( )
-      );
+  for( auto l = libs.begin( ); l != libs.end( ); ++l )
+  {
+    std::string lib = cpExtensions::CanonicalPath( *l );
+    if( lib != "" )
+    {
+      if( this->m_Libraries.find( lib ) == this->m_Libraries.end( ) )
+      {
+        std::string error = "";
+        void* hnd = cpPlugins::OS::DLLManager::Load( lib, error );
+        if( hnd != NULL )
+          this->m_Libraries[ lib ] = hnd;
+
+      } // fi
+
+    } // fi
+
+  } // rof
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Plugins::
-SaveEnvironments( const std::string& dir ) const
+LoadPaths( const std::string& dir )
 {
-  if( this->m_Paths.size( ) > 0 )
+  std::stringstream fname, envs;
+  fname << dir;
+  if( !cpExtensions::IsPathSeparator( dir.back( ) ) )
+    fname << cpExtensions_PATH_SEPARATOR;
+  fname << cpPlugins_PATHS;
+  std::string buffer;
+  if( cpExtensions::Read( buffer, fname.str( ) ) )
   {
-    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::istringstream input( buffer );
+    for( std::string line; std::getline( input, line ); )
+      envs << line << cpPlugins_ENV_SEPARATOR;
 
-    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." );
+  } // fi
+  if( envs.str( ).size( ) > 0 )
+    this->AddEnvironments( envs.str( ) );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Plugins::
-OpenEnvironments( const std::string& dir )
+SavePaths( const std::string& dir ) const
 {
+  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;
-  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
-    throw std::runtime_error( "Error opening environment file." );
+  if( !cpExtensions::Write( buffer.str( ), fname.str( ) ) )
+    throw std::runtime_error( "Error writing environment file." );
 }
 
 // -------------------------------------------------------------------------
@@ -289,7 +266,9 @@ LoadFile( const std::string& fname )
 
     // Get filter creator
     TCreator creator = ( TCreator )(
-      cpPlugins::OS::DLLManager::GetFunctionHandle( hnd, category + "_" + name )
+      cpPlugins::OS::DLLManager::GetFunctionHandle(
+        hnd, category + "_" + name
+        )
       );
     if( creator == NULL )
     {
@@ -356,28 +335,18 @@ LoadPlugin( const std::string& pname )
 void cpPlugins::Interface::Plugins::
 LoadDirectory( const std::string& dirname )
 {
-  DIR* dir;
-  struct dirent* ent;
-  if( ( dir = opendir( dirname.c_str( ) ) ) != NULL )
+  std::stringstream pat;
+  pat << "*" << cpPlugins_LIB_EXT;
+  auto libs = cpPlugins::OS::LoadDirContents( dirname, false, pat.str( ) );
+  for( auto lIt = libs.begin( ); lIt != libs.end( ); ++lIt )
   {
-    while( ( ent = readdir( dir ) ) != NULL )
+    try
     {
-      try
-      {
-        std::stringstream fname;
-        fname << dirname << cpExtensions_PATH_SEPARATOR << ent->d_name;
-        this->LoadFile( fname.str( ) );
-      }
-      catch( ... ) { }
-
-    } // elihw
-    closedir( dir );
-  }
-  else
-    throw std::runtime_error(
-      std::string( "Could not load directory " ) +
-      std::string( "\"" ) +  dirname + std::string( "\"" )
-      );
+      this->LoadFile( *lIt );
+    }
+    catch( ... ) { }
+
+  } // rof
 }
 
 // -------------------------------------------------------------------------
@@ -420,7 +389,13 @@ 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;