X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2FcpPlugins%2FInterface%2FInterface.cxx;h=34dbf2e6fc186218481d1ebbfabd448bb64d4681;hb=de874ea850042e77a99a456188f423c8df2e374f;hp=bb5b35c92ac32a7f85cfaefbd9432ed01bc3b884;hpb=ad814fa27df55cd06a071e474e56efc27f9f8fa5;p=cpPlugins.git diff --git a/lib/cpPlugins/Interface/Interface.cxx b/lib/cpPlugins/Interface/Interface.cxx index bb5b35c..34dbf2e 100644 --- a/lib/cpPlugins/Interface/Interface.cxx +++ b/lib/cpPlugins/Interface/Interface.cxx @@ -1,4 +1,36 @@ #include +#include +#include +#include + +#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:: @@ -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:: @@ -30,23 +112,52 @@ GetClasses( ) const return( this->m_Classes ); } +// ------------------------------------------------------------------------- +cpPlugins::Interface::Interface:: +TLoadedPlugins& cpPlugins::Interface::Interface:: +GetLoadedPlugins( ) +{ + return( this->m_LoadedPlugins ); +} + +// ------------------------------------------------------------------------- +const cpPlugins::Interface::Interface:: +TLoadedPlugins& cpPlugins::Interface::Interface:: +GetLoadedPlugins( ) const +{ + return( this->m_LoadedPlugins ); +} + // ------------------------------------------------------------------------- cpPlugins::Interface::ProcessObject::Pointer cpPlugins::Interface::Interface:: CreateObject( const std::string& name ) const { - TClassesIterator cIt = this->m_Classes.find( name ); - if( cIt != this->m_Classes.end( ) ) + cpPlugins::Interface::ProcessObject::Pointer po = NULL; + auto catIt = this->m_Classes.begin( ); + while( catIt != this->m_Classes.end( ) ) { - ProcessObjectProvider* provider = - dynamic_cast< ProcessObjectProvider* >( - this->m_Providers[ cIt->second ] - ); - if( provider != NULL ) - return( provider->create( ) ); + 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++; - } // fi - return( NULL ); + } // elihw + + return( po ); } // ------------------------------------------------------------------------- @@ -58,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( ... ) { @@ -72,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( ... ) { @@ -88,20 +221,35 @@ Load( const std::string& folder, const std::string& name ) } // ------------------------------------------------------------------------- -std::list< std::string > cpPlugins::Interface::Interface:: +bool cpPlugins::Interface::Interface:: LoadFromFolder( const std::string& folder, bool r ) { - std::list< std::string > files; try { - files = this->m_Pluma.loadFromFolder( folder, r ); - if( files.size( ) > 0 ) + 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( ... ) { + return( false ); + } // yrt - return( files ); } // ------------------------------------------------------------------------- @@ -116,6 +264,9 @@ Unload( const std::string& name ) { this->m_Providers.clear( ); this->m_Classes.clear( ); + + // TODO: this->m_LoadedPlugins + this->_LoadClasses( ); } // fi @@ -143,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( ); } // ------------------------------------------------------------------------- @@ -170,10 +315,39 @@ _LoadClasses( ) // Get reader provider for( unsigned int i = 0; i < this->m_Providers.size( ); ++i ) { - ProcessObject::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$