1 #include <cpPlugins/Interface/Interface.h>
6 #include <Pluma/Pluma.hpp>
9 # define PLUGIN_PREFIX ""
10 # define PLUGIN_EXT ".dll"
12 # define PLUGIN_PREFIX "lib"
13 # define PLUGIN_EXT ".so"
15 #define PLUGIN_CONFIG_FILE "plugins.cfg"
23 bool operator()( char c ) const
26 return( c == '\\' || c == '/' );
37 // -------------------------------------------------------------------------
38 std::string cpPlugins_Interface_CanonicalPath( const std::string& path )
41 TCHAR buffer[ 4096 ] = TEXT( "" );
42 TCHAR** lppPart = { NULL };
43 GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
44 return( std::string( buffer ) );
46 return( std::string( realpath( path.c_str( ), NULL ) ) );
50 // -------------------------------------------------------------------------
51 cpPlugins::Interface::Interface::
54 this->m_Pluma = new pluma::Pluma( );
55 this->m_Pluma->acceptProviderType< ProcessObjectProvider >( );
58 // -------------------------------------------------------------------------
59 cpPlugins::Interface::Interface::
66 // -------------------------------------------------------------------------
67 bool cpPlugins::Interface::Interface::
68 LoadDefaultConfiguration( const std::string& path )
70 std::ifstream file( PLUGIN_CONFIG_FILE );
74 while( file.getline( buffer, 1000 ) )
76 // std::string line( buffer );
77 std::istringstream line( buffer );
78 std::string name, folder;
79 std::getline( line, name, '@' );
80 std::getline( line, folder, '@' );
81 std::stringstream path;
82 path << folder << "/" << PLUGIN_PREFIX << name << PLUGIN_EXT;
83 this->Load( path.str( ) );
94 // -------------------------------------------------------------------------
95 bool cpPlugins::Interface::Interface::
96 SaveDefaultConfiguration( const std::string& path )
98 std::ofstream file( PLUGIN_CONFIG_FILE );
101 auto pIt = this->m_LoadedPlugins.begin( );
102 for( ; pIt != this->m_LoadedPlugins.end( ); ++pIt )
104 auto fIt = pIt->second.begin( );
105 for( ; fIt != pIt->second.end( ); ++fIt )
106 file << *fIt << "@" << pIt->first << std::endl;
116 // -------------------------------------------------------------------------
117 cpPlugins::Interface::Interface::
118 TClasses& cpPlugins::Interface::Interface::
121 return( this->m_Classes );
124 // -------------------------------------------------------------------------
125 const cpPlugins::Interface::Interface::
126 TClasses& cpPlugins::Interface::Interface::
129 return( this->m_Classes );
132 // -------------------------------------------------------------------------
133 cpPlugins::Interface::Interface::
134 TLoadedPlugins& cpPlugins::Interface::Interface::
137 return( this->m_LoadedPlugins );
140 // -------------------------------------------------------------------------
141 const cpPlugins::Interface::Interface::
142 TLoadedPlugins& cpPlugins::Interface::Interface::
143 GetLoadedPlugins( ) const
145 return( this->m_LoadedPlugins );
148 // -------------------------------------------------------------------------
149 cpPlugins::Interface::ProcessObject::Pointer
150 cpPlugins::Interface::Interface::
151 CreateObject( const std::string& name ) const
153 cpPlugins::Interface::ProcessObject::Pointer po = NULL;
154 auto catIt = this->m_Classes.begin( );
155 while( catIt != this->m_Classes.end( ) )
157 auto classIt = catIt->second.find( name );
158 if( classIt != catIt->second.end( ) )
160 ProcessObjectProvider* provider =
161 dynamic_cast< ProcessObjectProvider* >(
162 this->m_Providers[ classIt->second ]
164 if( provider != NULL )
166 po = provider->create( );
170 catIt = this->m_Classes.end( );
180 // -------------------------------------------------------------------------
181 bool cpPlugins::Interface::Interface::
182 Load( const std::string& path )
187 ret = this->m_Pluma->load( path );
190 std::string folder, name;
191 Self::_SepFName( path, folder, name );
192 this->m_LoadedPlugins[ folder ].push_back( name );
193 this->_LoadClasses( );
205 // -------------------------------------------------------------------------
206 bool cpPlugins::Interface::Interface::
207 Load( const std::string& folder, const std::string& name )
209 std::string real_folder = folder;
211 if( sep( folder[ folder.size( ) - 1 ] ) )
212 real_folder = folder.substr( 0, folder.size( ) - 1 );
213 real_folder = cpPlugins_Interface_CanonicalPath( real_folder );
218 ret = this->m_Pluma->load( real_folder, name );
221 // Update loaded plugins
222 std::string prefix( PLUGIN_PREFIX );
223 std::string ext( PLUGIN_EXT );
224 std::string real_name = name;
226 real_name.replace( real_name.find( prefix ), prefix.size( ), "" );
227 real_name.replace( real_name.find( ext ), ext.size( ), "" );
228 this->m_LoadedPlugins[ real_folder ].push_back( real_name );
229 this->_LoadClasses( );
241 // -------------------------------------------------------------------------
242 bool cpPlugins::Interface::Interface::
243 LoadFromFolder( const std::string& folder, bool r )
247 std::list< std::string > f = this->m_Pluma->loadFromFolder( folder, r );
250 // Update loaded plugins
251 for( auto i = f.begin( ); i != f.end( ); ++i )
253 std::string folder, name;
254 Self::_SepFName( *i, folder, name );
255 this->m_LoadedPlugins[ folder ].push_back( name );
260 this->_LoadClasses( );
273 // -------------------------------------------------------------------------
274 bool cpPlugins::Interface::Interface::
275 Unload( const std::string& name )
280 ret = this->m_Pluma->unload( name );
283 this->m_Providers.clear( );
284 this->m_Classes.clear( );
286 // TODO: this->m_LoadedPlugins
288 this->_LoadClasses( );
300 // -------------------------------------------------------------------------
301 void cpPlugins::Interface::Interface::
306 this->m_Pluma->unloadAll( );
313 this->m_Providers.clear( );
314 this->m_Classes.clear( );
315 this->m_LoadedPlugins.clear( );
318 // -------------------------------------------------------------------------
319 bool cpPlugins::Interface::Interface::
320 IsLoaded( const std::string& name ) const
322 return( this->m_Pluma->isLoaded( name ) );
325 // -------------------------------------------------------------------------
326 void cpPlugins::Interface::Interface::
329 this->m_Providers.clear( );
330 this->m_Classes.clear( );
331 this->m_Pluma->getProviders( this->m_Providers );
333 // Get reader provider
334 for( unsigned int i = 0; i < this->m_Providers.size( ); ++i )
336 ProcessObject::Pointer d = this->m_Providers[ i ]->create( );
337 this->m_Classes[ d->GetClassCategory( ) ][ d->GetClassName( ) ] = i;
342 // -------------------------------------------------------------------------
343 void cpPlugins::Interface::Interface::
344 _SepFName( const std::string& path, std::string& folder, std::string& name )
349 std::string real_path = cpPlugins_Interface_CanonicalPath( path );
353 std::find_if( real_path.rbegin( ), real_path.rend( ), sep ).base( ),
357 // Get containing folder
359 folder.replace( folder.find( name ), name.size( ), "" );
360 if( sep( folder[ folder.size( ) - 1 ] ) )
361 folder = folder.substr( 0, folder.size( ) - 1 );
363 // Erase prefix and extension from filename
364 std::string prefix( PLUGIN_PREFIX );
365 std::string ext( PLUGIN_EXT );
367 name.replace( name.find( prefix ), prefix.size( ), "" );
368 name.replace( name.find( ext ), ext.size( ), "" );