Commit d63d3fc6 authored by Peter Eastman's avatar Peter Eastman
Browse files

Implemented Platform::loadPluginsFromDirectory()

parent 4c0c5649
...@@ -181,6 +181,16 @@ public: ...@@ -181,6 +181,16 @@ public:
* or relative to a set of standard locations. * or relative to a set of standard locations.
*/ */
static void loadPluginLibrary(std::string file); static void loadPluginLibrary(std::string file);
/**
* Load multiple dynamic libraries (DLLs) which contain OpenMM plugins from a single directory.
* This method loops over every file contained in the specified directory and calls loadPluginLibrary()
* for each one. If an error occurs while trying to load a particular file, that file is simply
* ignored.
*
* @param directory the path to the directory containing libraries to load
* @return the names of all files which were successfully loaded as libraries
*/
static std::vector<std::string> loadPluginsFromDirectory(std::string directory);
private: private:
// Retarded visual studio compiler complains about being unable to // Retarded visual studio compiler complains about being unable to
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include <sstream> #include <sstream>
#else #else
#include <dlfcn.h> #include <dlfcn.h>
#include <dirent.h>
#endif #endif
#include <set> #include <set>
...@@ -151,3 +152,43 @@ void Platform::loadPluginLibrary(string file) { ...@@ -151,3 +152,43 @@ void Platform::loadPluginLibrary(string file) {
throw OpenMMException("Error loading library "+file+": "+dlerror()); throw OpenMMException("Error loading library "+file+": "+dlerror());
#endif #endif
} }
vector<string> Platform::loadPluginsFromDirectory(string directory) {
vector<string> files;
char dirSeparator;
#ifdef _MSC_VER
dirSeparator = '\\';
WIN32_FIND_DATA fileInfo;
string filePattern(directory + dirSeparator + "*");
HANDLE findHandle = FindFirstFile(filePattern.c_str(), &fileInfo);
if (findHandle != INVALID_HANDLE_VALUE) {
do {
if (fileInfo.cFileName[0] != '.')
files.push_back(string(fileInfo.cFileName));
} while (FindNextFile(findHandle, &fileInfo));
FindClose(findHandle);
}
#else
dirSeparator = '/';
DIR* dir;
struct dirent *entry;
dir = opendir(directory.c_str());
if (dir != NULL) {
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] != '.')
files.push_back(string(entry->d_name));
}
closedir(dir);
}
#endif
vector<string> loadedLibraries;
for (unsigned int i = 0; i < files.size(); ++i) {
try {
Platform::loadPluginLibrary(directory+dirSeparator+files[i]);
loadedLibraries.push_back(files[i]);
} catch (OpenMMException ex) {
// Just ignore it.
}
}
return loadedLibraries;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment