]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface.cxx
MPR objects updated
[cpPlugins.git] / lib / cpPlugins / Interface.cxx
index b8bffc65b6dc4e2f1f09f0c91ae3065876853b84..53b7b118174179807139737986a54f5e9a1ed93b 100644 (file)
@@ -5,6 +5,8 @@
 #else // cpPlugins_SYS_WINDOWS
 #  include <dlfcn.h>
 #endif // cpPlugins_SYS_WINDOWS
+#include <cpPlugins_dirent.h>
+#include <algorithm>
 
 // -------------------------------------------------------------------------
 cpPlugins::Interface::
@@ -27,6 +29,26 @@ GetFilters( )
   return( this->m_Filters );
 }
 
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::
+GuessAccesiblePlugins( )
+{
+  // Load environment configuration
+  char* path = std::getenv( "cpPlugins_PATHS" );
+  if( path != NULL )
+  {
+    std::vector< std::string > tokens;
+    cpPlugins::TokenizeString( tokens, path, "#" );
+    for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
+      try { this->LoadPluginDir( *tIt ); } catch( ... ) { }
+
+  } // fi
+
+  // Load local path
+  auto lpath = cpPlugins::CanonicalPath( "." );
+  try { this->LoadPluginDir( lpath ); } catch( ... ) { }
+}
+
 // -------------------------------------------------------------------------
 bool cpPlugins::Interface::
 LoadConfiguration( const std::string& filename )
@@ -38,18 +60,7 @@ LoadConfiguration( const std::string& filename )
   this->UnloadAll( );
   std::string line;
   while( std::getline( in, line ) )
-  {
-    try
-    {
-      this->LoadPluginFile( line );
-    }
-    catch( ... )
-    {
-      // Do nothing
-
-    } // yrt
-
-  } // elihw
+    try { this->LoadPluginFile( line ); } catch( ... ) { }
   return( true );
 }
 
@@ -79,6 +90,14 @@ LoadPluginFile( const std::string& filename )
       filename +
       std::string( "\" does not exist." )
       );
+
+  // Check if it was already loaded
+  if(
+    this->m_DynLibraries.find( canonical_fn ) != this->m_DynLibraries.end( )
+    )
+    return;
+
+  // Ok, try to load the library
   void* hnd = Self::_DLOpen( canonical_fn );
   if( hnd == NULL )
     throw std::runtime_error(
@@ -134,6 +153,39 @@ LoadPluginFile( const std::string& filename )
     Self::_DLClose( hnd );
 }
 
+// -------------------------------------------------------------------------
+unsigned int cpPlugins::Interface::
+LoadPluginDir( const std::string& dirname )
+{
+  DIR* dir;
+  struct dirent* ent;
+  unsigned int count = 0;
+  if( ( dir = opendir( dirname.c_str( ) ) ) != NULL )
+  {
+    while( ( ent = readdir( dir ) ) != NULL )
+    {
+      try
+      {
+        this->LoadPluginFile(
+          dirname +
+          std::string( "/" ) +
+          ent->d_name
+          );
+        count++;
+      }
+      catch( ... ) { }
+
+    } // elihw
+    closedir( dir );
+  }
+  else
+    throw std::runtime_error(
+      std::string( "cpPlugins::Interface: Could not load directory " ) +
+      std::string( "\"" ) +  dirname + std::string( "\"" )
+      );
+  return( count );
+}
+
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::
 UnloadAll( )