]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/Plugins.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface / Plugins.cxx
index b5f9269809b4f5f86f3f4350073e47c914e0d332..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,57 +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( ) != "" )
-      this->m_Paths.insert( dir.str( ) );
-
-  } // 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
 
@@ -140,17 +123,49 @@ 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::
+LoadPaths( const std::string& dir )
+{
+  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::istringstream input( buffer );
+    for( std::string line; std::getline( input, line ); )
+      envs << line << cpPlugins_ENV_SEPARATOR;
+
+  } // fi
+  if( envs.str( ).size( ) > 0 )
+    this->AddEnvironments( envs.str( ) );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Plugins::
-SaveEnvironments( const std::string& dir ) const
+SavePaths( const std::string& dir ) const
 {
   std::stringstream buffer;
   for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i )
@@ -165,48 +180,6 @@ SaveEnvironments( const std::string& dir ) const
     throw std::runtime_error( "Error writing environment file." );
 }
 
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-OpenEnvironments( const std::string& dir )
-{
-  std::vector< std::string > tokens;
-  cpExtensions::Tokenize( tokens, dir, cpPlugins_ENV_SEPARATOR );
-  for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
-  {
-    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
-}
-
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::Plugins::
 LoadFile( const std::string& fname )
@@ -293,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 )
     {
@@ -360,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
 }
 
 // -------------------------------------------------------------------------
@@ -424,27 +389,18 @@ 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->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
-  */
+  this->AddEnvironments( str.str( ) );
 }
 
 // -------------------------------------------------------------------------