]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/Plugins.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface / Plugins.cxx
index 260172e467c41e1b0d93ca5a1d0d6d419189aaa7..eabd53ef905e45048dd4530530b360b014a2bb43 100644 (file)
@@ -1,8 +1,7 @@
 #include <cpPlugins/Interface/Plugins.h>
 #include <cpPlugins/OS/DLLManager.h>
-#include <cpPlugins/Interface/Dirent.h>
+#include <cpPlugins/OS/DirContents.h>
 #include <cpExtensions/Utility.h>
-#include <regex>
 
 // -------------------------------------------------------------------------
 cpPlugins::Interface::Plugins::
@@ -106,76 +105,37 @@ LoadEnvironments( )
       std::istringstream input( buffer );
       for( std::string line; std::getline( input, line ); )
       {
-        std::vector< std::string > tokens;
-        cpExtensions::Tokenize( tokens, line, "|" );
-        if( tokens.size( ) == 2 )
+        auto pos = line.find( "*" );
+        if( pos != std::string::npos )
         {
-          std::string pth = tokens[ 0 ];
-          std::string lib = tokens[ 1 ];
-          if( lib.find( "*" ) != std::string::npos )
-          {
-            std::string ext( cpPlugins_LIB_EXT );
-            DIR* dir;
-            struct dirent* ent;
-            if( ( dir = opendir( pth.c_str( ) ) ) != NULL )
-            {
-              while( ( ent = readdir( dir ) ) != NULL )
-              {
-                std::string fname( ent->d_name );
-                long pos = long( fname.size( ) ) - long( ext.size( ) );
-                if( pos > 0 )
-                {
-                  if( fname.substr( pos ) == ext )
-                  {
-           std::regex re( lib );
-std::smatch match;
-if( std::regex_search( fname, match, re ) && match.size( ) >= 1 )
- {
-            std::stringstream str;
-            str
-              << pth << cpPlugins_PATH_SEPARATOR << fname;
-            libs.insert( str.str( ) );
-  } // fi
-
-                  } // fi
-
-                } // fi
-
-              } // elihw
-              closedir( dir );
-
-            } // fi
-          }
-          else
-          {
-            std::stringstream str;
-            str
-              << pth << cpPlugins_PATH_SEPARATOR << cpPlugins_LIB_PREFIX
-              << lib << cpPlugins_LIB_EXT;
-            libs.insert( str.str( ) );
-
-          } // fi
-
-        } // 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
 
     } // fi
 
   } // rof
+
   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;
+      {
+        std::string error = "";
+        void* hnd = cpPlugins::OS::DLLManager::Load( lib, error );
+        if( hnd != NULL )
+          this->m_Libraries[ lib ] = hnd;
 
-} // fi
+      } // fi
 
     } // fi
 
@@ -191,16 +151,16 @@ LoadPaths( const std::string& 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;
+  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( ) );
+  } // fi
+  if( envs.str( ).size( ) > 0 )
+    this->AddEnvironments( envs.str( ) );
 }
 
 // -------------------------------------------------------------------------
@@ -306,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 )
     {
@@ -373,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
 }
 
 // -------------------------------------------------------------------------