]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Library.cxx
Moved to version 1.0
[cpPlugins.git] / lib / cpPlugins / Library.cxx
diff --git a/lib/cpPlugins/Library.cxx b/lib/cpPlugins/Library.cxx
new file mode 100644 (file)
index 0000000..51af602
--- /dev/null
@@ -0,0 +1,126 @@
+// =========================================================================
+// @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
+// =========================================================================
+
+#include <cpPlugins/Library.h>
+
+#include <functional>
+#include <fstream>
+#ifdef cpPlugins_OS_Windows
+#else // cpPlugins_OS_Windows
+#  include <dlfcn.h>
+#endif // cpPlugins_OS_Windows
+#include <boost/filesystem/operations.hpp>
+#include <cpPlugins/ProcessObject.h>
+
+// -------------------------------------------------------------------------
+cpPlugins::Library::
+Library( const std::string& fname )
+  : m_Path( "" ),
+    m_Name( "" ),
+    m_Handle( NULL )
+{
+  namespace bf = boost::filesystem;
+
+  // Set and check (via exceptions) related files
+  bf::path cname = bf::canonical( fname ).string( );
+  std::string name = cname.filename( ).string( );
+  std::stringstream n;
+  n
+    << cname.parent_path( ).string( )
+    << bf::path::preferred_separator
+    << cpPlugins_LIB_PREFIX
+    << name.substr( 0, name.size( ) - cname.extension( ).string( ).size( ) )
+    << cpPlugins_LIB_EXT;
+  this->m_Path = bf::canonical( n.str( ) ).string( );
+  this->m_Name = cname.string( );
+
+  // Load library contents
+  std::ifstream in( this->m_Name.c_str( ) );
+  if( !in )
+    cpPluginsErrorMacro(
+      << "Could not load plugins file \"" << this->m_Name << "\""
+      );
+  typedef std::istreambuf_iterator< char > _TDIt;
+  std::istringstream str( std::string( ( _TDIt( in ) ), _TDIt( ) ) );
+  std::string buffer = str.str( );
+  in.close( );
+  str = std::istringstream( buffer );
+  std::string line;
+  while( std::getline( str, line ) )
+    if( line != "" )
+      this->m_Contents.insert( line );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Library::
+~Library( )
+{
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Library::
+Provides( const std::string& fname ) const
+{
+  return( this->m_Contents.find( fname ) != this->m_Contents.end( ) );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::Library::
+TStringSet& cpPlugins::Library::
+GetContents( ) const
+{
+  return( this->m_Contents );
+}
+
+// -------------------------------------------------------------------------
+std::shared_ptr< cpPlugins::ProcessObject > cpPlugins::Library::
+Create( const std::string& name )
+{
+  typedef std::shared_ptr< cpPlugins::ProcessObject > _TPtr;
+
+  if( this->m_Handle == NULL )
+    this->_LoadLibrary( );
+  _TPtr* ptr = reinterpret_cast< _TPtr* >( this->_CreateObject( name ) );
+  return( *ptr );
+}
+
+// -------------------------------------------------------------------------
+const std::string& cpPlugins::Library::
+GetPath( ) const
+{
+  return( this->m_Path );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Library::
+_LoadLibrary( )
+{
+#ifdef cpPlugins_OS_Windows
+#else // cpPlugins_OS_Windows
+  this->m_Handle = dlopen( this->m_Path.c_str( ), RTLD_LAZY );
+  if( this->m_Handle == NULL )
+    cpPluginsErrorMacro( << dlerror( ) );
+#endif // cpPlugins_OS_Windows
+}
+
+// -------------------------------------------------------------------------
+void* cpPlugins::Library::
+_CreateObject( const std::string& name )
+{
+  if( this->m_Handle != NULL )
+  {
+#ifdef cpPlugins_OS_Windows
+#else // cpPlugins_OS_Windows
+    void* hnd = dlsym( this->m_Handle, "cpPlugins_Create" );
+    if( hnd == NULL )
+      cpPluginsErrorMacro( << dlerror( ) );
+    typedef void* (_TFunc)( const char* );
+    return( ( reinterpret_cast< _TFunc* >( hnd ) )( name.c_str( ) ) );
+#endif // cpPlugins_OS_Windows
+  }
+  else
+    return( NULL );
+}
+
+// eof - $RCSfile$