1 #include <cpPlugins/Interface/Interface.h>
2 #include <cpPlugins/Interface/ProcessObjectProvider.h>
7 #include <Pluma/Pluma.hpp>
15 bool operator()( char c ) const
18 return( c == '\\' || c == '/' );
29 // -------------------------------------------------------------------------
30 std::string cpPlugins_Interface_CanonicalPath( const std::string& path )
33 TCHAR buffer[ 4096 ] = TEXT( "" );
34 TCHAR** lppPart = { NULL };
35 GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
36 return( std::string( buffer ) );
38 return( std::string( realpath( path.c_str( ), NULL ) ) );
42 // -------------------------------------------------------------------------
43 cpPlugins::Interface::Interface::
46 this->m_Pluma = new pluma::Pluma( );
47 this->m_Pluma->acceptProviderType< ProcessObjectProvider >( );
50 // -------------------------------------------------------------------------
51 cpPlugins::Interface::Interface::
58 // -------------------------------------------------------------------------
59 bool cpPlugins::Interface::Interface::
60 LoadDefaultConfiguration( const std::string& path )
62 std::ifstream file( PLUGIN_CONFIG_FILE );
66 while( file.getline( buffer, 1000 ) )
68 // std::string line( buffer );
69 std::istringstream line( buffer );
70 std::string name, folder;
71 std::getline( line, name, '@' );
72 std::getline( line, folder, '@' );
73 std::stringstream path;
74 path << folder << "/" << PLUGIN_PREFIX << name << PLUGIN_EXT;
75 this->Load( path.str( ) );
86 // -------------------------------------------------------------------------
87 bool cpPlugins::Interface::Interface::
88 SaveDefaultConfiguration( const std::string& path )
90 std::ofstream file( PLUGIN_CONFIG_FILE );
93 auto pIt = this->m_LoadedPlugins.begin( );
94 for( ; pIt != this->m_LoadedPlugins.end( ); ++pIt )
96 auto fIt = pIt->second.begin( );
97 for( ; fIt != pIt->second.end( ); ++fIt )
98 file << *fIt << "@" << pIt->first << std::endl;
108 // -------------------------------------------------------------------------
109 cpPlugins::Interface::Interface::
110 TClasses& cpPlugins::Interface::Interface::
113 return( this->m_Classes );
116 // -------------------------------------------------------------------------
117 const cpPlugins::Interface::Interface::
118 TClasses& cpPlugins::Interface::Interface::
121 return( this->m_Classes );
124 // -------------------------------------------------------------------------
125 cpPlugins::Interface::Interface::
126 TLoadedPlugins& cpPlugins::Interface::Interface::
129 return( this->m_LoadedPlugins );
132 // -------------------------------------------------------------------------
133 const cpPlugins::Interface::Interface::
134 TLoadedPlugins& cpPlugins::Interface::Interface::
135 GetLoadedPlugins( ) const
137 return( this->m_LoadedPlugins );
140 // -------------------------------------------------------------------------
141 cpPlugins::Interface::ProcessObject::Pointer
142 cpPlugins::Interface::Interface::
143 CreateObject( const std::string& name ) const
145 cpPlugins::Interface::ProcessObject::Pointer po = NULL;
146 auto catIt = this->m_Classes.begin( );
147 while( catIt != this->m_Classes.end( ) )
149 auto classIt = catIt->second.find( name );
150 if( classIt != catIt->second.end( ) )
152 ProcessObjectProvider* provider =
153 dynamic_cast< ProcessObjectProvider* >(
154 this->m_Providers[ classIt->second ]
156 if( provider != NULL )
157 po = provider->create( );
158 catIt = this->m_Classes.end( );
167 // -------------------------------------------------------------------------
168 bool cpPlugins::Interface::Interface::
169 Load( const std::string& path )
174 ret = this->m_Pluma->load( path );
177 std::string folder, name;
178 Self::_SepFName( path, folder, name );
179 this->m_LoadedPlugins[ folder ].push_back( name );
180 this->_LoadClasses( );
192 // -------------------------------------------------------------------------
193 bool cpPlugins::Interface::Interface::
194 Load( const std::string& folder, const std::string& name )
196 std::string real_folder = folder;
198 if( sep( folder[ folder.size( ) - 1 ] ) )
199 real_folder = folder.substr( 0, folder.size( ) - 1 );
200 real_folder = cpPlugins_Interface_CanonicalPath( real_folder );
205 ret = this->m_Pluma->load( real_folder, name );
208 // Update loaded plugins
209 std::string prefix( PLUGIN_PREFIX );
210 std::string ext( PLUGIN_EXT );
211 std::string real_name = name;
213 real_name.replace( real_name.find( prefix ), prefix.size( ), "" );
214 real_name.replace( real_name.find( ext ), ext.size( ), "" );
215 this->m_LoadedPlugins[ real_folder ].push_back( real_name );
216 this->_LoadClasses( );
228 // -------------------------------------------------------------------------
229 bool cpPlugins::Interface::Interface::
230 LoadFromFolder( const std::string& folder, bool r )
234 std::list< std::string > f = this->m_Pluma->loadFromFolder( folder, r );
237 // Update loaded plugins
238 for( auto i = f.begin( ); i != f.end( ); ++i )
240 std::string folder, name;
241 Self::_SepFName( *i, folder, name );
242 this->m_LoadedPlugins[ folder ].push_back( name );
247 this->_LoadClasses( );
260 // -------------------------------------------------------------------------
261 bool cpPlugins::Interface::Interface::
262 Unload( const std::string& name )
267 ret = this->m_Pluma->unload( name );
270 this->m_Providers.clear( );
271 this->m_Classes.clear( );
273 // TODO: this->m_LoadedPlugins
275 this->_LoadClasses( );
287 // -------------------------------------------------------------------------
288 void cpPlugins::Interface::Interface::
293 this->m_Pluma->unloadAll( );
300 this->m_Providers.clear( );
301 this->m_Classes.clear( );
302 this->m_LoadedPlugins.clear( );
305 // -------------------------------------------------------------------------
306 bool cpPlugins::Interface::Interface::
307 IsLoaded( const std::string& name ) const
309 return( this->m_Pluma->isLoaded( name ) );
312 // -------------------------------------------------------------------------
313 void cpPlugins::Interface::Interface::
316 this->m_Providers.clear( );
317 this->m_Classes.clear( );
318 this->m_Pluma->getProviders( this->m_Providers );
320 // Get reader provider
321 for( unsigned int i = 0; i < this->m_Providers.size( ); ++i )
323 ProcessObject::Pointer d = this->m_Providers[ i ]->create( );
324 this->m_Classes[ d->GetClassCategory( ) ][ d->GetClassName( ) ] = i;
329 // -------------------------------------------------------------------------
330 void cpPlugins::Interface::Interface::
331 _SepFName( const std::string& path, std::string& folder, std::string& name )
336 std::string real_path = cpPlugins_Interface_CanonicalPath( path );
340 std::find_if( real_path.rbegin( ), real_path.rend( ), sep ).base( ),
344 // Get containing folder
346 folder.replace( folder.find( name ), name.size( ), "" );
347 if( sep( folder[ folder.size( ) - 1 ] ) )
348 folder = folder.substr( 0, folder.size( ) - 1 );
350 // Erase prefix and extension from filename
351 std::string prefix( PLUGIN_PREFIX );
352 std::string ext( PLUGIN_EXT );
354 name.replace( name.find( prefix ), prefix.size( ), "" );
355 name.replace( name.find( ext ), ext.size( ), "" );