Skip to content

Commit

Permalink
Replace Poco with std::filesystem in places
Browse files Browse the repository at this point in the history
Use std::filesystem methods and classes instead of Poco in the code
loading dynamic libraries. Also threw in replacing boost string
methods with std equivalents in DllOpen.cpp.
  • Loading branch information
jclarkeSTFC authored and peterfpeterson committed Dec 5, 2024
1 parent 2e2ab40 commit 8234ac6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
10 changes: 3 additions & 7 deletions Framework/Kernel/inc/MantidKernel/LibraryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include <filesystem>
#include <string>
#include <unordered_map>
#include <vector>
Expand All @@ -17,11 +18,6 @@
#include "MantidKernel/LibraryWrapper.h"
#include "MantidKernel/SingletonHolder.h"

namespace Poco {
class File;
class Path;
} // namespace Poco

namespace Mantid {
namespace Kernel {
/**
Expand All @@ -48,7 +44,7 @@ class MANTID_KERNEL_DLL LibraryManagerImpl {

/// Load libraries from the given Poco::File path
/// Private so Poco::File doesn't leak to the public interface
int openLibraries(const Poco::File &libpath, LoadLibraries loadingBehaviour,
int openLibraries(const std::filesystem::path &libpath, LoadLibraries loadingBehaviour,
const std::vector<std::string> &excludes);
/// Check if the library should be loaded
bool shouldBeLoaded(const std::string &filename, const std::vector<std::string> &excludes) const;
Expand All @@ -57,7 +53,7 @@ class MANTID_KERNEL_DLL LibraryManagerImpl {
/// Returns true if the library has been requested to be excluded
bool isExcluded(const std::string &filename, const std::vector<std::string> &excludes) const;
/// Load a given library
int openLibrary(const Poco::File &filepath, const std::string &cacheKey);
int openLibrary(const std::filesystem::path &filepath, const std::string &cacheKey);

/// Storage for the LibraryWrappers.
std::unordered_map<std::string, LibraryWrapper> m_openedLibs;
Expand Down
6 changes: 2 additions & 4 deletions Framework/Kernel/src/DllOpen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
#include <dlfcn.h>
#endif

#include <boost/algorithm/string/predicate.hpp>

#include <string>

namespace Mantid::Kernel {
Expand All @@ -36,7 +34,7 @@ const std::string LIB_SUFFIX = ".dll";
* @param filename The file name of the library
* @return True if it matches the expected format, false otherwise
*/
bool DllOpen::isValidFilename(const std::string &filename) { return boost::ends_with(filename, LIB_SUFFIX); }
bool DllOpen::isValidFilename(const std::string &filename) { return filename.ends_with(LIB_SUFFIX); }

/* Opens the Windows .dll file.
* @param filePath :: Filepath of the library.
Expand Down Expand Up @@ -86,7 +84,7 @@ const std::string LIB_SUFFIX = ".dylib";
* @return True if it matches the expected format, false otherwise
*/
bool DllOpen::isValidFilename(const std::string &filename) {
return boost::starts_with(filename, LIB_PREFIX) && boost::ends_with(filename, LIB_SUFFIX);
return filename.starts_with(LIB_PREFIX) && filename.ends_with(LIB_SUFFIX);
}

/* Opens the Linux .so file
Expand Down
37 changes: 17 additions & 20 deletions Framework/Kernel/src/LibraryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
#include "MantidKernel/LibraryWrapper.h"
#include "MantidKernel/Logger.h"

#include <Poco/DirectoryIterator.h>
#include <Poco/File.h>
#include <Poco/Path.h>
#include <boost/algorithm/string.hpp>
#include <filesystem>

namespace Mantid::Kernel {
namespace {
Expand All @@ -37,7 +34,7 @@ int LibraryManagerImpl::openLibraries(const std::string &filepath, LoadLibraries
const std::vector<std::string> &excludes) {
g_log.debug("Opening all libraries in " + filepath + "\n");
try {
return openLibraries(Poco::File(filepath), loadingBehaviour, excludes);
return openLibraries(std::filesystem::path(filepath), loadingBehaviour, excludes);
} catch (std::exception &exc) {
g_log.debug() << "Error occurred while opening libraries: " << exc.what() << "\n";
return 0;
Expand All @@ -52,34 +49,34 @@ int LibraryManagerImpl::openLibraries(const std::string &filepath, LoadLibraries
//-------------------------------------------------------------------------
/**
* Opens suitable DLLs on a given path.
* @param libpath A Poco::File object pointing to a directory where the
* @param libpath An std::filesystem::path object pointing to a directory where the
* libraries are.
* @param loadingBehaviour Control how libraries are searched for
* @param excludes If not empty then each string is considered as a substring
* to search within each library to be opened. If the substring is found then
* the library is not opened.
* @return The number of libraries opened.
*/
int LibraryManagerImpl::openLibraries(const Poco::File &libpath, LibraryManagerImpl::LoadLibraries loadingBehaviour,
int LibraryManagerImpl::openLibraries(const std::filesystem::path &libpath,
LibraryManagerImpl::LoadLibraries loadingBehaviour,
const std::vector<std::string> &excludes) {
int libCount(0);
if (libpath.exists() && libpath.isDirectory()) {
if (std::filesystem::exists(libpath) && std::filesystem::is_directory(libpath)) {
// Iterate over the available files
Poco::DirectoryIterator end_itr;
for (Poco::DirectoryIterator itr(libpath); itr != end_itr; ++itr) {
const Poco::File &item = *itr;
if (item.isFile()) {
if (shouldBeLoaded(itr.path().getFileName(), excludes))
libCount += openLibrary(itr.path(), itr.path().getFileName());
for (const auto &file : std::filesystem::directory_iterator(libpath)) {
const auto &path = file.path();
if (std::filesystem::is_regular_file(path)) {
if (shouldBeLoaded(path.filename().string(), excludes))
libCount += openLibrary(path.string(), path.filename().string());
else
continue;
} else if (loadingBehaviour == LoadLibraries::Recursive) {
// it must be a directory
libCount += openLibraries(item, LoadLibraries::Recursive, excludes);
libCount += openLibraries(path, LoadLibraries::Recursive, excludes);
}
}
} else {
g_log.error("In OpenAllLibraries: " + libpath.path() + " must be a directory.");
g_log.error("In OpenAllLibraries: " + libpath.string() + " must be a directory.");
}
return libCount;
}
Expand Down Expand Up @@ -122,18 +119,18 @@ bool LibraryManagerImpl::isExcluded(const std::string &filename, const std::vect

/**
* Load a library
* @param filepath :: A Poco::File The full path to a library as a string
* @param filepath :: An std::filesystem::path The full path to a library as a string
* @param cacheKey :: An identifier for the cache if loading is successful
* @return 1 if the file loaded successfully, 0 otherwise
*/
int LibraryManagerImpl::openLibrary(const Poco::File &filepath, const std::string &cacheKey) {
int LibraryManagerImpl::openLibrary(const std::filesystem::path &filepath, const std::string &cacheKey) {
// Try to open the library. The wrapper will unload the library when it
// is deleted
LibraryWrapper dlwrap;
if (dlwrap.openLibrary(filepath.path())) {
if (dlwrap.openLibrary(filepath.string())) {
// Successfully opened, so add to map
if (g_log.is(Poco::Message::PRIO_DEBUG)) {
g_log.debug("Opened library: " + filepath.path() + ".\n");
g_log.debug("Opened library: " + filepath.string() + ".\n");
}
m_openedLibs.emplace(cacheKey, std::move(dlwrap));
return 1;
Expand Down

0 comments on commit 8234ac6

Please sign in to comment.