Skip to content
Snippets Groups Projects
LibraryManager.cpp 2.87 KiB
Newer Older
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include <iostream>

#include "MantidKernel/DllOpen.h"
Nick Draper's avatar
Nick Draper committed
#include "MantidKernel/LibraryManager.h"
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
Nick Draper's avatar
Nick Draper committed
  namespace fs = boost::filesystem;  // to help clarify which bits are boost in code below

  Logger& LibraryManager::g_log = Logger::get("LibraryManager");

	LibraryManager::LibraryManager() : module(0)
Matt Clarke's avatar
Matt Clarke committed
	{}
	LibraryManager::~LibraryManager()
	{
		//Close lib
		if (module)
		{
			DllOpen::CloseDll(module);
			module = 0;
		}
	/** Opens a DLL
	 *  @param libName The name of the file to open (not including the lib/so/dll)
	 *  @return True if DLL is opened or already open
	 */
	bool LibraryManager::OpenLibrary(const std::string& libName)
			//Load dynamically loaded library
			module = DllOpen::OpenDll(libName);
				return false;
			}
		}
	
		return true;
	}
	
	/** Opens a DLL
	 *  @param libName The name of the file to open (not including the lib/so/dll)
	 *  @param filePath The filepath to the directory where the library lives
	 *  @return True if DLL is opened or already open
	 */
	bool LibraryManager::OpenLibrary(const std::string& libName, const std::string& filePath)
	{
		if (!module)
		{		
			//Load dynamically loaded library
			module = DllOpen::OpenDll(libName, filePath);
			if (!module) 
			{
Nick Draper's avatar
Nick Draper committed
  /** Opens all suitable DLLs on a given path
	 *  @param filePath The filepath to the directory where the libraries live
	 *  @param isRecursive whether to search subdirectories
	 *  @return The number of libraries opened
	 */
	int LibraryManager::OpenAllLibraries(const std::string& filePath, bool isRecursive)
  {
    int libCount = 0;

    //validate inputs
    if ( fs::exists( filePath ) )
    {
      //iteratate over the available files
      fs::directory_iterator end_itr; // default construction yields past-the-end
      for ( fs::directory_iterator itr( filePath );
          itr != end_itr;
          ++itr )
      {
        if ( fs::is_directory(itr->status()) )
        {
          if (isRecursive)
          {
            libCount += OpenAllLibraries(itr->path().string());
          }
        }
        else
        {
          //if they are libraries
Nick Draper's avatar
Nick Draper committed
          std::string libName = DllOpen::ConvertToLibName(itr->path().leaf());
Nick Draper's avatar
Nick Draper committed
          if (libName != "")
          {
            //load them
            if (OpenLibrary(libName,filePath)) ++libCount;
          }
        }
      }
    }
    else
    {
      g_log.error("In OpenAllLibraries: " + filePath + " must be a directory."); 
    }


   
    return libCount;
  }