]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Plugins.cxx
debug
[cpPlugins.git] / lib / cpPlugins / Interface / Plugins.cxx
1 #include <cpPlugins/Interface/Plugins.h>
2 #include <cpPlugins/OS/DLLManager.h>
3 #include <cpPlugins/OS/DirContents.h>
4 #include <cpPlugins/Utility.h>
5
6 // -------------------------------------------------------------------------
7 cpPlugins::Interface::Plugins::Pointer
8 cpPlugins::Interface::Plugins::m_Singleton = NULL;
9
10 // -------------------------------------------------------------------------
11 cpPlugins::Interface::Plugins::
12 Pointer cpPlugins::Interface::Plugins::
13 New( )
14 {
15   if( Self::m_Singleton.IsNull( ) )
16     Self::m_Singleton = new Self( );
17   return( Self::m_Singleton );
18 }
19
20 // -------------------------------------------------------------------------
21 itk::LightObject::Pointer cpPlugins::Interface::Plugins::
22 CreateAnother( ) const
23 {
24   itk::LightObject::Pointer smartPtr;
25   smartPtr = Self::m_Singleton;
26   return( smartPtr );
27 }
28
29 // -------------------------------------------------------------------------
30 cpPlugins::Interface::Plugins::
31 Pointer cpPlugins::Interface::Plugins::
32 Clone( ) const
33 {
34   return( Self::m_Singleton );
35 }
36
37 // -------------------------------------------------------------------------
38 const cpPlugins::Interface::Plugins::
39 TFilters& cpPlugins::Interface::Plugins::
40 GetFilters( ) const
41 {
42   return( this->m_Filters );
43 }
44
45 // -------------------------------------------------------------------------
46 void cpPlugins::Interface::Plugins::
47 LoadPluginsFile( const std::string& libname )
48 {
49   std::map< std::string, std::set< std::string > > filters;
50   cpPlugins::OS::DLLManager::GetPluginsLibraryContents( filters, libname );
51   THandlers zero( NULL, NULL );
52   for( auto i : filters )
53     for( auto j : i.second )
54       this->m_Filters[ i.first ][ j ] = TLibData( libname, zero );
55 }
56
57 // -------------------------------------------------------------------------
58 void cpPlugins::Interface::Plugins::
59 LoadPluginsDirectory( const std::string& dir )
60 {
61   // Create a globbing pattern
62   std::stringstream glob;
63   glob << cpPlugins_LIB_PREFIX << "*" << cpPlugins_LIB_EXT;
64
65   // Get possible shared libraries
66   std::set< std::string > files =
67     cpPlugins::OS::LoadDirContents( dir, false, glob.str( ) );
68   this->m_PluginsPaths.insert( dir );
69   for( auto f : files )
70     try { this->LoadPluginsFile( f ); } catch( ... ) { }
71 }
72
73 // -------------------------------------------------------------------------
74 void cpPlugins::Interface::Plugins::
75 GuessPlugins( )
76 {
77   // Create a globbing pattern
78   std::stringstream glob;
79   glob << cpPlugins_LIB_PREFIX << "*" << cpPlugins_LIB_EXT;
80
81   // Update paths and get possible shared libraries
82   this->_ReadPluginsPathsVariable( );
83   for( auto dir : this->m_PluginsPaths )
84   {
85     std::set< std::string > files =
86       cpPlugins::OS::LoadDirContents( dir, false, glob.str( ) );
87     for( auto f : files )
88       try { this->LoadPluginsFile( f ); } catch( ... ) { }
89
90   } // rof
91 }
92
93 // -------------------------------------------------------------------------
94 void cpPlugins::Interface::Plugins::
95 GuessEnvironment( const std::string& dir )
96 {
97   std::stringstream fname;
98   fname << dir << cpPlugins_PATH_SEPARATOR << cpPlugins_PATHS;
99   std::string buffer;
100   if( cpPlugins::Read( buffer, fname.str( ) ) )
101   {
102     std::istringstream input( buffer );
103     for( std::string line; std::getline( input, line ); )
104       this->m_PluginsPaths.insert( cpPlugins::CanonicalPath( line ) );
105
106   } // fi
107 }
108
109 // -------------------------------------------------------------------------
110 bool cpPlugins::Interface::Plugins::
111 SaveEnvironment( const std::string& dir )
112 {
113   std::stringstream buffer;
114   for( auto p : this->m_PluginsPaths )
115     buffer << p << std::endl;
116   std::stringstream fname;
117   fname << dir << cpPlugins_PATH_SEPARATOR << cpPlugins_PATHS;
118   return( cpPlugins::Write( buffer.str( ), fname.str( ) ) );
119 }
120
121 // -------------------------------------------------------------------------
122 cpPlugins::BaseObjects::ProcessObject::Pointer
123 cpPlugins::Interface::Plugins::
124 CreateFilter( const std::string& category, const std::string& name )
125 {
126   std::cout << "1" << std::endl;
127   typedef void* ( *_TCreator )( );
128   std::cout << "2" << std::endl;
129   typedef cpPlugins::BaseObjects::ProcessObject::Pointer _TPtr;
130   std::cout << "3" << std::endl;
131   _TPtr o = NULL;
132   std::cout << "4" << std::endl;
133   auto cat = this->m_Filters.find( category );
134   std::cout << "5" << std::endl;
135   if( cat != this->m_Filters.end( ) )
136   {
137   std::cout << "6" << std::endl;
138     auto nam = cat->second.find( name );
139   std::cout << "7" << std::endl;
140     if( nam != cat->second.end( ) )
141     {
142   std::cout << "8" << std::endl;
143       void* l_hnd = nam->second.second.first;
144   std::cout << "9" << std::endl;
145       void* f_hnd = nam->second.second.second;
146   std::cout << "10" << std::endl;
147       if( l_hnd == NULL )
148       {
149   std::cout << "11" << std::endl;
150         l_hnd = cpPlugins::OS::DLLManager::LoadPlugins( nam->second.first );
151   std::cout << "12" << std::endl;
152         nam->second.second.first = l_hnd;
153   std::cout << "13" << std::endl;
154
155       } // fi
156       if( f_hnd == NULL )
157       {
158   std::cout << "14" << std::endl;
159         f_hnd =
160           cpPlugins::OS::DLLManager::LoadCreator( l_hnd, category, name );
161   std::cout << "15" << std::endl;
162         nam->second.second.second = f_hnd;
163   std::cout << "16" << std::endl;
164
165       } // fi
166   std::cout << "17" << std::endl;
167       _TCreator creator = reinterpret_cast< _TCreator >( f_hnd );
168   std::cout << "18" << std::endl;
169       if( creator != NULL )
170       {
171         std::cout << "19 " << creator << " " << f_hnd << std::endl;
172         void* a = creator( );
173   std::cout << "20" << std::endl;
174         std::cout << "20 " << creator << " " << a << std::endl;
175   std::cout << "21" << std::endl;
176         o = reinterpret_cast< _TPtr* >( a )->GetPointer( );
177   std::cout << "22" << std::endl;
178         o->SetName( name );
179   std::cout << "23" << std::endl;
180         o->SetPluginName( nam->second.first );
181   std::cout << "24" << std::endl;
182
183       } // fi
184
185     } // fi
186
187   } // fi
188   std::cout << "25" << std::endl;
189   if( o.IsNull( ) )
190     throw std::runtime_error(
191       std::string( "Could not create a valid ProcessObject of type \"" ) +
192       category + std::string( ":" ) +
193       name + std::string( "\"" )
194       );
195   std::cout << "26" << std::endl;
196   return( o );
197 }
198
199 // -------------------------------------------------------------------------
200 cpPlugins::Interface::Plugins::
201 Plugins( )
202   : Superclass( )
203 {
204   cpPlugins::OS::DLLManager::TeaseLoadedLibraries( );
205 }
206
207 // -------------------------------------------------------------------------
208 cpPlugins::Interface::Plugins::
209 ~Plugins( )
210 {
211 }
212
213 // -------------------------------------------------------------------------
214 void cpPlugins::Interface::Plugins::
215 PrintSelf( std::ostream& os, itk::Indent indent ) const
216 {
217   for( auto i : this->m_Filters )
218   {
219     os << indent << "+ " << i.first << std::endl;
220     for( auto j : i.second )
221       os << indent << "|----> " << j.first << std::endl;
222
223   } // rof
224 }
225
226 // -------------------------------------------------------------------------
227 void cpPlugins::Interface::Plugins::
228 _ReadPluginsPathsVariable( )
229 {
230 #ifdef cpPlugins_OS_Windows
231   char* p;
232   size_t size;
233   _dupenv_s( &p, &size, cpPlugins_PATHS );
234 #else // cpPlugins_OS_Windows
235   char* p = std::getenv( cpPlugins_PATHS );
236 #endif // cpPlugins_OS_Windows
237   std::stringstream str;
238   if( p != NULL )
239     str << p << cpPlugins_ENV_SEPARATOR;
240   str << ".";
241   std::vector< std::string > tokens;
242   cpPlugins::Tokenize( tokens, str.str( ), cpPlugins_ENV_SEPARATOR );
243   for( auto dir : tokens )
244     this->m_PluginsPaths.insert( cpPlugins::CanonicalPath( dir ) );
245 }
246
247 // eof - $RCSfile$