+++ /dev/null
-#include <cpPlugins/Interface/Plugins.h>
-#include <cpPlugins/OS/DLLManager.h>
-#include <cpPlugins/OS/DirContents.h>
-#include <cpPlugins/Utility.h>
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::Pointer
-cpPlugins::Interface::Plugins::m_Singleton = NULL;
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-Pointer cpPlugins::Interface::Plugins::
-New( )
-{
- if( Self::m_Singleton.IsNull( ) )
- Self::m_Singleton = new Self( );
- return( Self::m_Singleton );
-}
-
-// -------------------------------------------------------------------------
-itk::LightObject::Pointer cpPlugins::Interface::Plugins::
-CreateAnother( ) const
-{
- itk::LightObject::Pointer smartPtr;
- smartPtr = Self::m_Singleton;
- return( smartPtr );
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-Pointer cpPlugins::Interface::Plugins::
-Clone( ) const
-{
- return( Self::m_Singleton );
-}
-
-// -------------------------------------------------------------------------
-const cpPlugins::Interface::Plugins::
-TFilters& cpPlugins::Interface::Plugins::
-GetFilters( ) const
-{
- return( this->m_Filters );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-LoadPluginsFile( const std::string& libname )
-{
- std::map< std::string, std::set< std::string > > filters;
- cpPlugins::OS::DLLManager::GetPluginsLibraryContents( filters, libname );
- THandlers zero( NULL, NULL );
- for( auto i : filters )
- for( auto j : i.second )
- this->m_Filters[ i.first ][ j ] = TLibData( libname, zero );
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-LoadPluginsDirectory( const std::string& dir )
-{
- // Create a globbing pattern
- std::stringstream glob;
- glob << cpPlugins_LIB_PREFIX << "*" << cpPlugins_LIB_EXT;
-
- // Get possible shared libraries
- std::set< std::string > files =
- cpPlugins::OS::LoadDirContents( dir, false, glob.str( ) );
- this->m_PluginsPaths.insert( dir );
- for( auto f : files )
- try { this->LoadPluginsFile( f ); } catch( ... ) { }
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-GuessPlugins( )
-{
- // Create a globbing pattern
- std::stringstream glob;
- glob << cpPlugins_LIB_PREFIX << "*" << cpPlugins_LIB_EXT;
-
- // Update paths and get possible shared libraries
- this->_ReadPluginsPathsVariable( );
- for( auto dir : this->m_PluginsPaths )
- {
- std::set< std::string > files =
- cpPlugins::OS::LoadDirContents( dir, false, glob.str( ) );
- for( auto f : files )
- try { this->LoadPluginsFile( f ); } catch( ... ) { }
-
- } // rof
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-GuessEnvironment( const std::string& dir )
-{
- std::stringstream fname;
- fname << dir << cpPlugins_PATH_SEPARATOR << cpPlugins_PATHS;
- std::string buffer;
- if( cpPlugins::Read( buffer, fname.str( ) ) )
- {
- std::istringstream input( buffer );
- for( std::string line; std::getline( input, line ); )
- this->m_PluginsPaths.insert( cpPlugins::CanonicalPath( line ) );
-
- } // fi
-}
-
-// -------------------------------------------------------------------------
-bool cpPlugins::Interface::Plugins::
-SaveEnvironment( const std::string& dir )
-{
- std::stringstream buffer;
- for( auto p : this->m_PluginsPaths )
- buffer << p << std::endl;
- std::stringstream fname;
- fname << dir << cpPlugins_PATH_SEPARATOR << cpPlugins_PATHS;
- return( cpPlugins::Write( buffer.str( ), fname.str( ) ) );
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::BaseObjects::ProcessObject::Pointer
-cpPlugins::Interface::Plugins::
-CreateFilter( const std::string& category, const std::string& name )
-{
- typedef void* ( *_TCreator )( );
- typedef cpPlugins::BaseObjects::ProcessObject::Pointer _TPtr;
- _TPtr o = NULL;
- auto cat = this->m_Filters.find( category );
- if( cat != this->m_Filters.end( ) )
- {
- auto nam = cat->second.find( name );
- if( nam != cat->second.end( ) )
- {
- void* l_hnd = nam->second.second.first;
- void* f_hnd = nam->second.second.second;
- if( l_hnd == NULL )
- {
- l_hnd = cpPlugins::OS::DLLManager::LoadPlugins( nam->second.first );
- nam->second.second.first = l_hnd;
-
- } // fi
- if( f_hnd == NULL )
- {
- f_hnd =
- cpPlugins::OS::DLLManager::LoadCreator( l_hnd, category, name );
- nam->second.second.second = f_hnd;
-
- } // fi
- _TCreator creator = reinterpret_cast< _TCreator >( f_hnd );
- if( creator != NULL )
- {
- void* a = creator( );
- o = reinterpret_cast< _TPtr* >( a )->GetPointer( );
- o->SetName( name );
- o->SetPluginName( nam->second.first );
-
- } // fi
-
- } // fi
-
- } // fi
- if( o.IsNull( ) )
- throw std::runtime_error(
- std::string( "Could not create a valid ProcessObject of type \"" ) +
- category + std::string( ":" ) +
- name + std::string( "\"" )
- );
- return( o );
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-Plugins( )
- : Superclass( )
-{
- cpPlugins::OS::DLLManager::TeaseLoadedLibraries( );
-}
-
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Plugins::
-~Plugins( )
-{
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-PrintSelf( std::ostream& os, itk::Indent indent ) const
-{
- for( auto i : this->m_Filters )
- {
- os << indent << "+ " << i.first << std::endl;
- for( auto j : i.second )
- os << indent << "|----> " << j.first << std::endl;
-
- } // rof
-}
-
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Plugins::
-_ReadPluginsPathsVariable( )
-{
-#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 << ".";
- std::vector< std::string > tokens;
- cpPlugins::Tokenize( tokens, str.str( ), cpPlugins_ENV_SEPARATOR );
- for( auto dir : tokens )
- this->m_PluginsPaths.insert( cpPlugins::CanonicalPath( dir ) );
-}
-
-// eof - $RCSfile$