]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/Interface.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface / Interface.cxx
index 4d470b68c4e2d96ec6a4aa1f5ba81714110acc9c..34dbf2e6fc186218481d1ebbfabd448bb64d4681 100644 (file)
@@ -1,10 +1,42 @@
 #include <cpPlugins/Interface/Interface.h>
+#include <cstdlib>
+#include <fstream>
+#include <sstream>
+
+#ifdef _WIN32
+#  define PLUGIN_PREFIX ""
+#  define PLUGIN_EXT ".dll"
+#else // Linux
+#  define PLUGIN_PREFIX "lib"
+#  define PLUGIN_EXT ".so"
+#endif // _WIN32
+#define PLUGIN_CONFIG_FILE "plugins.cfg"
+
+namespace cpPlugins
+{
+  namespace Interface
+  {
+    struct PathSeparator
+    {
+      bool operator()( char c ) const
+        {
+#ifdef _WIN32
+          return( c == '\\' || c == '/' );
+#else // Linux like
+          return( c == '/' );
+#endif // _WIN32
+        }
+    };
+
+  } // ecapseman
+
+} // ecapseman
 
 // -------------------------------------------------------------------------
 cpPlugins::Interface::Interface::
 Interface( )
 {
-  this->m_Pluma.acceptProviderType< ObjectProvider >( );
+  this->m_Pluma.acceptProviderType< ProcessObjectProvider >( );
 }
 
 // -------------------------------------------------------------------------
@@ -14,6 +46,56 @@ cpPlugins::Interface::Interface::
   this->UnloadAll( );
 }
 
+// -------------------------------------------------------------------------
+bool cpPlugins::Interface::Interface::
+LoadDefaultConfiguration( const std::string& path )
+{
+  std::ifstream file( PLUGIN_CONFIG_FILE );
+  if( file )
+  {
+    char buffer[ 1000 ];
+    while( file.getline( buffer, 1000 ) )
+    {
+      // std::string line( buffer );
+      std::istringstream line( buffer );
+      std::string name, folder;
+      std::getline( line, name, '@' );
+      std::getline( line, folder, '@' );
+      std::stringstream path;
+      path << folder << "/" << PLUGIN_PREFIX << name << PLUGIN_EXT;
+      this->Load( path.str( ) );
+
+    } // elihw
+
+    file.close( );
+    return( true );
+  }
+  else
+    return( false );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Interface::Interface::
+SaveDefaultConfiguration( const std::string& path )
+{
+  std::ofstream file( PLUGIN_CONFIG_FILE );
+  if( file )
+  {
+    auto pIt = this->m_LoadedPlugins.begin( );
+    for( ; pIt != this->m_LoadedPlugins.end( ); ++pIt )
+    {
+      auto fIt = pIt->second.begin( );
+      for( ; fIt != pIt->second.end( ); ++fIt )
+        file << *fIt << "@" << pIt->first << std::endl;
+
+    } // rof
+    file.close( );
+    return( true );
+  }
+  else
+    return( false );
+}
+
 // -------------------------------------------------------------------------
 cpPlugins::Interface::Interface::
 TClasses& cpPlugins::Interface::Interface::
@@ -31,30 +113,51 @@ GetClasses( ) const
 }
 
 // -------------------------------------------------------------------------
-cpPlugins::Interface::Object::Pointer cpPlugins::Interface::Interface::
-CreateObject( const std::string& name ) const
+cpPlugins::Interface::Interface::
+TLoadedPlugins& cpPlugins::Interface::Interface::
+GetLoadedPlugins( )
 {
-  TClassesIterator cIt = this->m_Classes.find( name );
-  if( cIt != this->m_Classes.end( ) )
-  {
-    ObjectProvider* provider =
-      dynamic_cast< ObjectProvider* >( this->m_Providers[ cIt->second ] );
-    if( provider != NULL )
-      return( provider->create( ) );
+  return( this->m_LoadedPlugins );
+}
 
-  } // fi
-  return( NULL );
+// -------------------------------------------------------------------------
+const cpPlugins::Interface::Interface::
+TLoadedPlugins& cpPlugins::Interface::Interface::
+GetLoadedPlugins( ) const
+{
+  return( this->m_LoadedPlugins );
 }
 
 // -------------------------------------------------------------------------
 cpPlugins::Interface::ProcessObject::Pointer
 cpPlugins::Interface::Interface::
-CreateProcessObject( const std::string& name ) const
+CreateObject( const std::string& name ) const
 {
-  cpPlugins::Interface::Object::Pointer o = this->CreateObject( name );
-  cpPlugins::Interface::ProcessObject::Pointer p =
-    dynamic_cast< cpPlugins::Interface::ProcessObject* >( o.GetPointer( ) );
-  return( p );
+  cpPlugins::Interface::ProcessObject::Pointer po = NULL;
+  auto catIt = this->m_Classes.begin( );
+  while( catIt != this->m_Classes.end( ) )
+  {
+    auto classIt = catIt->second.find( name );
+    if( classIt != catIt->second.end( ) )
+    {
+      ProcessObjectProvider* provider =
+        dynamic_cast< ProcessObjectProvider* >(
+          this->m_Providers[ classIt->second ]
+          );
+      if( provider != NULL )
+      {
+        po = provider->create( );
+        po->SetName( name );
+
+      } // fi
+      catIt = this->m_Classes.end( );
+    }
+    else
+      catIt++;
+
+  } // elihw
+
+  return( po );
 }
 
 // -------------------------------------------------------------------------
@@ -66,7 +169,13 @@ Load( const std::string& path )
   {
     ret = this->m_Pluma.load( path );
     if( ret )
+    {
+      std::string folder, name;
+      Self::_SepFName( path, folder, name );
+      this->m_LoadedPlugins[ folder ].push_back( name );
       this->_LoadClasses( );
+
+    } // fi
   }
   catch( ... )
   {
@@ -80,12 +189,28 @@ Load( const std::string& path )
 bool cpPlugins::Interface::Interface::
 Load( const std::string& folder, const std::string& name )
 {
+  std::string real_folder = folder;
+  PathSeparator sep;
+  if( sep( folder[ folder.size( ) - 1 ] ) )
+    real_folder = folder.substr( 0, folder.size( ) - 1 );
+  real_folder = std::string( realpath( real_folder.c_str( ), NULL ) );
   bool ret = true;
   try
   {
-    ret = this->m_Pluma.load( folder, name );
+    ret = this->m_Pluma.load( real_folder, name );
     if( ret )
+    {
+      // Update loaded plugins
+      std::string prefix( PLUGIN_PREFIX );
+      std::string ext( PLUGIN_EXT );
+      std::string real_name = name;
+      if( prefix != "" )
+        real_name.replace( real_name.find( prefix ), prefix.size( ), "" );
+      real_name.replace( real_name.find( ext ), ext.size( ), "" );
+      this->m_LoadedPlugins[ real_folder ].push_back( real_name );
       this->_LoadClasses( );
+
+    } // fi
   }
   catch( ... )
   {
@@ -96,22 +221,35 @@ Load( const std::string& folder, const std::string& name )
 }
 
 // -------------------------------------------------------------------------
-int cpPlugins::Interface::Interface::
+bool cpPlugins::Interface::Interface::
 LoadFromFolder( const std::string& folder, bool r )
 {
-  bool ret = true;
   try
   {
-    ret = this->m_Pluma.loadFromFolder( folder, r );
-    if( ret )
+    std::list< std::string > f = this->m_Pluma.loadFromFolder( folder, r );
+    if( f.size( ) > 0 )
+    {
+      // Update loaded plugins
+      for( auto i = f.begin( ); i != f.end( ); ++i )
+      {
+        std::string folder, name;
+        Self::_SepFName( *i, folder, name );
+        this->m_LoadedPlugins[ folder ].push_back( name );
+
+      } // rof
+
+      // Load classes
       this->_LoadClasses( );
+      return( true );
+    }
+    else
+      return( false );
   }
   catch( ... )
   {
-    ret = false;
+    return( false );
 
   } // yrt
-  return( ret );
 }
 
 // -------------------------------------------------------------------------
@@ -126,6 +264,9 @@ Unload( const std::string& name )
     {
       this->m_Providers.clear( );
       this->m_Classes.clear( );
+
+      // TODO: this->m_LoadedPlugins
+
       this->_LoadClasses( );
 
     } // fi
@@ -153,13 +294,7 @@ UnloadAll( )
   } // yrt
   this->m_Providers.clear( );
   this->m_Classes.clear( );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Interface::
-GetLoadedPlugins( std::vector< const std::string* >& names ) const
-{
-  this->m_Pluma.getLoadedPlugins( names );
+  this->m_LoadedPlugins.clear( );
 }
 
 // -------------------------------------------------------------------------
@@ -180,10 +315,39 @@ _LoadClasses( )
   // Get reader provider
   for( unsigned int i = 0; i < this->m_Providers.size( ); ++i )
   {
-    Object::Pointer dummy = this->m_Providers[ i ]->create( );
-    this->m_Classes[ dummy->GetClassName( ) ] = i;
+    ProcessObject::Pointer d = this->m_Providers[ i ]->create( );
+    this->m_Classes[ d->GetClassCategory( ) ][ d->GetClassName( ) ] = i;
 
   } // rof
 }
 
+// -------------------------------------------------------------------------
+void cpPlugins::Interface::Interface::
+_SepFName( const std::string& path, std::string& folder, std::string& name )
+{
+  PathSeparator sep;
+
+  // Get absolute path
+  std::string real_path = std::string( realpath( path.c_str( ), NULL ) );
+
+  // Get name
+  name = std::string(
+    std::find_if( real_path.rbegin( ), real_path.rend( ), sep ).base( ),
+    real_path.end( )
+    );
+
+  // Get containing folder
+  folder = real_path;
+  folder.replace( folder.find( name ), name.size( ), "" );
+  if( sep( folder[ folder.size( ) - 1 ] ) )
+    folder = folder.substr( 0, folder.size( ) - 1 );
+
+  // Erase prefix and extension from filename
+  std::string prefix( PLUGIN_PREFIX );
+  std::string ext( PLUGIN_EXT );
+  if( prefix != "" )
+    name.replace( name.find( prefix ), prefix.size( ), "" );
+  name.replace( name.find( ext ), ext.size( ), "" );
+}
+
 // eof - $RCSfile$