Newer
Older
Gigg, Martyn Anthony
committed
#include "Poco/Path.h"
#include "MantidKernel/DllOpen.h"
#include "MantidKernel/LibraryManager.h"
#include "MantidKernel/Logger.h"
Gigg, Martyn Anthony
committed
#include "Poco/File.h"
#include "Poco/DirectoryIterator.h"
#include "boost/algorithm/string.hpp"
Russell Taylor
committed
{
Gigg, Martyn Anthony
committed
namespace Kernel
{
Gigg, Martyn Anthony
committed
LibraryManagerImpl::LibraryManagerImpl() :
g_log(Logger::get("LibraryManager"))
{
g_log.debug() << "LibraryManager created." << std::endl;
}
Gigg, Martyn Anthony
committed
LibraryManagerImpl::~LibraryManagerImpl()
{
//std::cerr << "LibraryManager destroyed." << std::endl;
}
/** Opens all suitable DLLs on a given path.
Janik Zikovsky
committed
* @param filePath :: The filepath to the directory where the libraries are.
* @param isRecursive :: Whether to search subdirectories.
* @return The number of libraries opened.
Gigg, Martyn Anthony
committed
int LibraryManagerImpl::OpenAllLibraries(const std::string& filePath,
bool isRecursive)
{
int libCount = 0;
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
//validate inputs
Poco::File libPath(filePath);
if ( libPath.exists() && libPath.isDirectory() )
{
Roman Tolchenov
committed
DllOpen::addSearchDirectory(filePath);
Gigg, Martyn Anthony
committed
//iteratate over the available files
Poco::DirectoryIterator end_itr;
for (Poco::DirectoryIterator itr(libPath); itr != end_itr; ++itr)
{
if ( Poco::Path(itr->path()).isDirectory() )
{
if (isRecursive)
{
libCount += OpenAllLibraries(itr->path());
}
}
else
{
//if they are libraries
std::string libName = DllOpen::ConvertToLibName(Poco::Path(itr->path()).getFileName());
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
if (libName != "")
{
//load them
boost::shared_ptr<LibraryWrapper> dlwrap(new LibraryWrapper);
Gigg, Martyn Anthony
committed
//use lower case library name for the map key
std::string libNameLower = boost::algorithm::to_lower_copy(libName);
Gigg, Martyn Anthony
committed
//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 (dlwrap->OpenLibrary(libName, filePath))
{
//Successfully opened, so add to map
g_log.debug("Opened library: " + libName + ".\n");
OpenLibs.insert(std::pair< std::string, boost::shared_ptr<LibraryWrapper> >(libName, dlwrap) );
++libCount;
Gigg, Martyn Anthony
committed
}
}
}
Gigg, Martyn Anthony
committed
}
else
{
g_log.error("In OpenAllLibraries: " + filePath + " must be a directory.");
}
Gigg, Martyn Anthony
committed
return libCount;
}
Russell Taylor
committed
Gigg, Martyn Anthony
committed
} // namespace Kernel
Russell Taylor
committed
} // namespace Mantid