]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Library.cxx
Moved to version 1.0
[cpPlugins.git] / lib / cpPlugins / Library.cxx
1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
4
5 #include <cpPlugins/Library.h>
6
7 #include <functional>
8 #include <fstream>
9 #ifdef cpPlugins_OS_Windows
10 #else // cpPlugins_OS_Windows
11 #  include <dlfcn.h>
12 #endif // cpPlugins_OS_Windows
13 #include <boost/filesystem/operations.hpp>
14 #include <cpPlugins/ProcessObject.h>
15
16 // -------------------------------------------------------------------------
17 cpPlugins::Library::
18 Library( const std::string& fname )
19   : m_Path( "" ),
20     m_Name( "" ),
21     m_Handle( NULL )
22 {
23   namespace bf = boost::filesystem;
24
25   // Set and check (via exceptions) related files
26   bf::path cname = bf::canonical( fname ).string( );
27   std::string name = cname.filename( ).string( );
28   std::stringstream n;
29   n
30     << cname.parent_path( ).string( )
31     << bf::path::preferred_separator
32     << cpPlugins_LIB_PREFIX
33     << name.substr( 0, name.size( ) - cname.extension( ).string( ).size( ) )
34     << cpPlugins_LIB_EXT;
35   this->m_Path = bf::canonical( n.str( ) ).string( );
36   this->m_Name = cname.string( );
37
38   // Load library contents
39   std::ifstream in( this->m_Name.c_str( ) );
40   if( !in )
41     cpPluginsErrorMacro(
42       << "Could not load plugins file \"" << this->m_Name << "\""
43       );
44   typedef std::istreambuf_iterator< char > _TDIt;
45   std::istringstream str( std::string( ( _TDIt( in ) ), _TDIt( ) ) );
46   std::string buffer = str.str( );
47   in.close( );
48   str = std::istringstream( buffer );
49   std::string line;
50   while( std::getline( str, line ) )
51     if( line != "" )
52       this->m_Contents.insert( line );
53 }
54
55 // -------------------------------------------------------------------------
56 cpPlugins::Library::
57 ~Library( )
58 {
59 }
60
61 // -------------------------------------------------------------------------
62 bool cpPlugins::Library::
63 Provides( const std::string& fname ) const
64 {
65   return( this->m_Contents.find( fname ) != this->m_Contents.end( ) );
66 }
67
68 // -------------------------------------------------------------------------
69 const cpPlugins::Library::
70 TStringSet& cpPlugins::Library::
71 GetContents( ) const
72 {
73   return( this->m_Contents );
74 }
75
76 // -------------------------------------------------------------------------
77 std::shared_ptr< cpPlugins::ProcessObject > cpPlugins::Library::
78 Create( const std::string& name )
79 {
80   typedef std::shared_ptr< cpPlugins::ProcessObject > _TPtr;
81
82   if( this->m_Handle == NULL )
83     this->_LoadLibrary( );
84   _TPtr* ptr = reinterpret_cast< _TPtr* >( this->_CreateObject( name ) );
85   return( *ptr );
86 }
87
88 // -------------------------------------------------------------------------
89 const std::string& cpPlugins::Library::
90 GetPath( ) const
91 {
92   return( this->m_Path );
93 }
94
95 // -------------------------------------------------------------------------
96 void cpPlugins::Library::
97 _LoadLibrary( )
98 {
99 #ifdef cpPlugins_OS_Windows
100 #else // cpPlugins_OS_Windows
101   this->m_Handle = dlopen( this->m_Path.c_str( ), RTLD_LAZY );
102   if( this->m_Handle == NULL )
103     cpPluginsErrorMacro( << dlerror( ) );
104 #endif // cpPlugins_OS_Windows
105 }
106
107 // -------------------------------------------------------------------------
108 void* cpPlugins::Library::
109 _CreateObject( const std::string& name )
110 {
111   if( this->m_Handle != NULL )
112   {
113 #ifdef cpPlugins_OS_Windows
114 #else // cpPlugins_OS_Windows
115     void* hnd = dlsym( this->m_Handle, "cpPlugins_Create" );
116     if( hnd == NULL )
117       cpPluginsErrorMacro( << dlerror( ) );
118     typedef void* (_TFunc)( const char* );
119     return( ( reinterpret_cast< _TFunc* >( hnd ) )( name.c_str( ) ) );
120 #endif // cpPlugins_OS_Windows
121   }
122   else
123     return( NULL );
124 }
125
126 // eof - $RCSfile$