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

#include "boost/filesystem/operations.hpp"
Nick Draper's avatar
Nick Draper committed
#include "boost/filesystem/path.hpp"
#include "boost/algorithm/string.hpp"
#include "MantidKernel/LibraryManager.h"
#include "MantidKernel/DllOpen.h"

	namespace fs = boost::filesystem;  // to help clarify which bits are boost in code below
Nick Draper's avatar
Nick Draper committed

	LibraryManagerImpl::LibraryManagerImpl() : g_log(Logger::get("LibraryManager"))
		std::cerr << "LibraryManager created." << std::endl;
		g_log.debug() << "LibraryManager created." << std::endl;
	}
	LibraryManagerImpl::~LibraryManagerImpl()
		std::cerr << "LibraryManager destroyed." << std::endl;
//		g_log.debug() << "LibraryManager destroyed." << std::endl;
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 LibraryManagerImpl::OpenAllLibraries(const std::string& filePath, bool isRecursive)
Nick Draper's avatar
Nick Draper committed
  {
    int libCount = 0;

    //validate inputs
    if ( fs::exists( filePath ) )
    {
Nick Draper's avatar
Nick Draper committed
      //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
        {
Nick Draper's avatar
Nick Draper committed
          //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 != "")
          {
		LibraryWrapper* tmp = new LibraryWrapper; 
		
		//use lower case library name for the map key
		std::string libNameLower = boost::algorithm::to_lower_copy(libName);
		//Check that a libray with this name has not already been loaded
		if (OpenLibs.find(libNameLower) == OpenLibs.end())
			g_log.debug("Trying to open library: " + libName + "...");
			//Try to open the library
			if (tmp->OpenLibrary(libName,filePath))
			{		
				//Successfully opened, so add to map
				g_log.debug("Opened library: " + libName + ".\n");
				boost::shared_ptr<LibraryWrapper> pLib(tmp); 
	 	                OpenLibs.insert ( std::pair< std::string, boost::shared_ptr<LibraryWrapper> >(libName, pLib) ); 
Nick Draper's avatar
Nick Draper committed
      g_log.error("In OpenAllLibraries: " + filePath + " must be a directory."); 
Nick Draper's avatar
Nick Draper committed
   
    return libCount;
  }