From 10f490b762198b1e0fe5d4473874643243880883 Mon Sep 17 00:00:00 2001
From: Leonardo Florez-Valencia <florez-l@javeriana.edu.co>
Date: Sun, 20 Mar 2016 16:40:38 -0500
Subject: [PATCH] ...

---
 lib/cpPlugins/Interface.cxx | 41 +++++++++++++++++++++++++++----------
 lib/cpPlugins/Interface.h   |  2 +-
 2 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/lib/cpPlugins/Interface.cxx b/lib/cpPlugins/Interface.cxx
index 6c6e0f8..58e468d 100644
--- a/lib/cpPlugins/Interface.cxx
+++ b/lib/cpPlugins/Interface.cxx
@@ -80,6 +80,12 @@ LoadPluginFile( const std::string& filename )
       filename +
       std::string( "\" does not exist." )
       );
+
+  // Check if it was already loaded
+  if( this->m_DynLibraries.find( canonical_fn ) != this->m_DynLibraries.end( ) )
+    return;
+
+  // Ok, try to load the library
   void* hnd = Self::_DLOpen( canonical_fn );
   if( hnd == NULL )
     throw std::runtime_error(
@@ -136,21 +142,34 @@ LoadPluginFile( const std::string& filename )
 }
 
 // -------------------------------------------------------------------------
-void cpPlugins::Interface::
+unsigned int cpPlugins::Interface::
 LoadPluginDir( const std::string& dirname )
 {
-#error ACA VOY
-  DIR *dir;
-  struct dirent *ent;
-  if( (dir = opendir ( dirname.c_str( ) ) ) != NULL)
+  DIR* dir;
+  struct dirent* ent;
+  unsigned int count = 0;
+  if( ( dir = opendir( dirname.c_str( ) ) ) != NULL )
   {
-    while ((ent = readdir (dir)) != NULL) {
-      printf ("%s\n", ent->d_name);
-    }
-    closedir (dir);
-  } else {
-    std::cerr << "error" << std::endl;
+    while( ( ent = readdir( dir ) ) != NULL )
+    {
+      try
+      {
+        this->LoadPluginFile( ent->d_name );
+        count++;
+      }
+      catch( ... )
+      {
+        // Ignore errors
+      } // yrt
+    } // elihw
+    closedir( dir );
   }
+  else
+    throw std::runtime_error(
+      std::string( "cpPlugins::Interface: Could not load directory " ) +
+      std::string( "\"" ) +  dirname + std::string( "\"" )
+      );
+  return( count );
 }
 
 // -------------------------------------------------------------------------
diff --git a/lib/cpPlugins/Interface.h b/lib/cpPlugins/Interface.h
index 7c9681a..451c0e0 100644
--- a/lib/cpPlugins/Interface.h
+++ b/lib/cpPlugins/Interface.h
@@ -32,7 +32,7 @@ namespace cpPlugins
     bool LoadConfiguration( const std::string& filename );
     bool SaveConfiguration( const std::string& filename ) const;
     void LoadPluginFile( const std::string& filename );
-    void LoadPluginDir( const std::string& dirname );
+    unsigned int LoadPluginDir( const std::string& dirname );
     void UnloadAll( );
 
     cpPlugins::ProcessObject::Pointer Create(
-- 
2.49.0