]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface.cxx
index f898e7b1e92d7c2237cc34d05159d1c4664c2b28..1f78e27976998f1dd6f53c3f48c1f59a3b7cb53b 100644 (file)
@@ -1,6 +1,6 @@
 #include <cpPlugins/Interface.h>
 #include <cpPlugins/Utilities.h>
-#include <cpPlugins/LoadDynamicLibrariesFunctions.h>
+#include <cpPlugins/DLLManager.h>
 #include <cpPlugins/dirent.h>
 #include <algorithm>
 
@@ -13,7 +13,7 @@ Interface( )
   if( p != NULL )
     str << p << cpPlugins_SEPARATOR;
   str << ".";
-  this->UpdateEnvironments( str.str( ) );
+  this->AddEnvironments( str.str( ) );
 }
 
 // -------------------------------------------------------------------------
@@ -23,17 +23,19 @@ cpPlugins::Interface::
 }
 
 // -------------------------------------------------------------------------
-const std::set< std::string >& cpPlugins::Interface::
+const cpPlugins::Interface::
+TStrings& cpPlugins::Interface::
 GetPaths( ) const
 {
   return( this->m_Paths );
 }
 
 // -------------------------------------------------------------------------
-std::set< std::string > cpPlugins::Interface::
+cpPlugins::Interface::
+TStrings cpPlugins::Interface::
 GetLibraries( ) const
 {
-  std::set< std::string > res;
+  TStrings res;
   for(
     auto i = this->m_Libraries.begin( ); i != this->m_Libraries.end( ); ++i
     )
@@ -42,71 +44,79 @@ GetLibraries( ) const
 }
 
 // -------------------------------------------------------------------------
-std::set< std::string > cpPlugins::Interface::
+cpPlugins::Interface::
+TStrings cpPlugins::Interface::
 GetPlugins( ) const
 {
-  std::set< std::string > res;
+  TStrings res;
   for( auto i = this->m_Plugins.begin( ); i != this->m_Plugins.end( ); ++i )
     res.insert( i->first );
   return( res );
 }
 
 // -------------------------------------------------------------------------
-std::set< std::string > cpPlugins::Interface::
+cpPlugins::Interface::
+TStrings cpPlugins::Interface::
 GetCategories( ) const
 {
-  std::set< std::string > res;
+  TStrings res;
   for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
     res.insert( i->first );
   return( res );
 }
 
 // -------------------------------------------------------------------------
-std::set< std::string > cpPlugins::Interface::
+cpPlugins::Interface::
+TStrings cpPlugins::Interface::
 GetFilters( const std::string& category ) const
 {
-  std::set< std::string > res;
+  TStrings res;
   auto cIt = this->m_Filters.find( category );
   if( cIt != this->m_Filters.end( ) )
-  {
     for( auto i = cIt->second.begin( ); i != cIt->second.end( ); ++i )
       res.insert( i->first );
-
-  } // rof
   return( res );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::
-UpdateEnvironments( const std::string& new_environment )
+AddEnvironments( const std::string& new_environment )
 {
   std::vector< std::string > tokens;
   cpPlugins::TokenizeString( tokens, new_environment, cpPlugins_SEPARATOR );
   for( auto i = tokens.begin( ); i != tokens.end( ); ++i )
   {
-    std::string dir = cpPlugins::CanonicalPath( *i );
-    if( dir != "" )
-      this->m_Paths.insert( dir );
+    std::stringstream dir;
+    dir << cpPlugins::CanonicalPath( *i );
+    if( dir.str( ) != "" )
+    {
+      if( !cpPlugins::IsPathSeparator( dir.str( ).back( ) ) )
+        dir << cpPlugins_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
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::Interface::
-LoadEnvironment( )
+LoadEnvironments( )
 {
   std::stringstream all_errors;
-  for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i )
+  for( auto d = this->m_Paths.begin( ); d != this->m_Paths.end( ); ++d )
   {
-    std::stringstream dir;
-    dir << *i;
-    if( !cpPlugins::IsPathSeparator( i->back( ) ) )
-      dir << cpPlugins_PATH_SEPARATOR;
-    std::string fname = dir.str( ) + std::string( cpPlugins_CONFIG );
-    std::string config_file;
-    if( cpPlugins::ReadFileIntoBuffer( config_file, fname ) )
+    std::stringstream name;
+    name << *d << cpPlugins_CONFIG;
+    std::string buffer;
+    if( cpPlugins::ReadFileIntoBuffer( buffer, name.str( ) ) )
     {
-      std::istringstream input( config_file );
+      std::istringstream input( buffer );
       for( std::string line; std::getline( input, line ); )
       {
         std::vector< std::string > tokens;
@@ -115,10 +125,8 @@ LoadEnvironment( )
         if( tokens[ 0 ] == "local" )
           library_file =
             cpPlugins::CanonicalPath(
-              dir.str( ) +
-              std::string( cpPlugins_LIB_PREFIX ) +
-              tokens[ 1 ] +
-              std::string( "." ) + 
+              *d + std::string( cpPlugins_LIB_PREFIX ) +
+              tokens[ 1 ] + std::string( "." ) + 
               std::string( cpPlugins_LIB_EXT )
               );
         else if( tokens[ 0 ] == "global" )
@@ -162,10 +170,10 @@ SaveEnvironments( const std::string& dir ) const
 
     std::stringstream fname;
     fname << dir;
-    if( cpPlugins::IsPathSeparator( dir.back( ) ) )
+    if( !cpPlugins::IsPathSeparator( dir.back( ) ) )
       fname << cpPlugins_PATH_SEPARATOR;
-    fname << cpPlugins_CONFIG;
-    if( !( cpPlugins::WriteBufferToFile( buffer.str( ), fname.str( ) ) ) )
+    fname << cpPlugins_PATHS;
+    if( !cpPlugins::WriteBufferToFile( buffer.str( ), fname.str( ) ) )
       throw std::runtime_error( "Error writing environment file." );
   }
   else
@@ -178,15 +186,17 @@ OpenEnvironments( const std::string& dir )
 {
   std::stringstream fname;
   fname << dir;
-  if( cpPlugins::IsPathSeparator( dir.back( ) ) )
+  if( !cpPlugins::IsPathSeparator( dir.back( ) ) )
     fname << cpPlugins_PATH_SEPARATOR;
-  fname << cpPlugins_CONFIG;
+  fname << cpPlugins_PATHS;
   std::string buffer;
   if( cpPlugins::ReadFileIntoBuffer( buffer, fname.str( ) ) )
   {
     std::istringstream input( buffer );
+    std::stringstream paths;
     for( std::string line; std::getline( input, line ); )
-      this->m_Paths.insert( cpPlugins::CanonicalPath( line ) );
+      paths << line << cpPlugins_SEPARATOR;
+    this->AddEnvironments( paths.str( ) );
   }
   else
     throw std::runtime_error( "Error opening environment file." );
@@ -375,13 +385,8 @@ GuessPlugins( )
 {
   for( auto i = this->m_Paths.begin( ); i != this->m_Paths.end( ); ++i )
   {
-    try
-    {
-      this->LoadDirectory( *i );
-    }
-    catch( ... )
-    {
-    } // yrt
+    try { this->LoadDirectory( *i ); }
+    catch( ... ) { }
 
   } // rof
 }