45#define WIN32_LEAN_AND_MEAN
60#include <boost/tokenizer.hpp>
75 void tokenize(
const std::string &str,
const std::string &delimiters,
76 std::vector<std::string> &retItems)
78 typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
79 boost::char_separator<char> sep(delimiters.c_str());
80 Tokenizer tok(str, sep);
81 for (Tokenizer::iterator i = tok.begin(); i != tok.end(); ++i) {
82 retItems.push_back(*i);
105static int filter(std::string &name,
const char *suffix)
107 std::string delimiters =
".";
108 std::vector <std::string> items;
110 tokenize(name, delimiters, items);
112 if (items.size() == 0) {
116 if (items[items.size() -1] == suffix) {
125bool getDirSos(std::vector<std::string> &sos, std::string &dir)
128 const char *ds = dir.c_str();
132 dirh = FindFirstFileA(ds, &fd);
133 while (dirh != INVALID_HANDLE_VALUE)
135 std::string name = fd.cFileName;
136 std::string name_lower;
138 std::transform(name.begin(), name.end(), name_lower.begin(), ::tolower);
140 if (
filter(name_lower,
"so")) {
141 name = dir +
"/" + name;
145 if (!FindNextFileA(dirh, &fd))
152 struct dirent *dirent;
154 const char *ds = dir.c_str();
155 DIR *dirfd = opendir(ds);
158 "Field3D_plugin loader: could not open directory " + dir +
"\n";
163 dirent = readdir(dirfd);
164 while (dirent != NULL) {
166 std::string name = dirent->d_name;
169 name = dir +
"/" + name;
173 dirent = readdir(dirfd);
184 HMODULE handle = ::LoadLibraryA(sofile.c_str());
186 void *handle = dlopen(sofile.c_str(), RTLD_GLOBAL|RTLD_NOW);
194 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
195 NULL, GetLastError(),
196 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&errstr,
202 errmsg =
"Unknown error";
209 "Field3D Plugin loader: failed to load plugin: " << errmsg <<
"\n";
224 char *debugEnvVar = getenv(
"FIELD3D_DEBUG");
228 "Field3D plugin loader: failed to load "
229 "the symbol registerField3DPlugin");
255 char *cptr = getenv(
"FIELD3D_DSO_PATH");
259 std::string path = cptr;
262 std::vector<std::string> paths;
263 const std::string delimiters =
":";
265 tokenize(path, delimiters, paths);
268 for (
unsigned int i = 0; i < paths.size(); i++) {
271 std::vector<std::string> sos;
277 for (
unsigned int j = 0; j < sos.size(); j++) {
278 std::string sofile = sos[j];
281 const std::string pathDelimiter =
"/";
282 std::vector<std::string> pluginName;
283 tokenize(sofile, pathDelimiter, pluginName);
285 bool pluginAlreadyLoaded =
false;
288 if (pluginName.size() > 0) {
292 pluginAlreadyLoaded =
true;
298 if (pluginAlreadyLoaded) {
302 if (pluginName.size() > 0) {
303 std::string lastName = pluginName[pluginName.size() -1];
315 "failed to init Field3D plugin " + sofile);
317 Msg::print(
"Initialized Field3D Plugin " + sofile);
328bool PluginLoader::getDso(
char *cptr,
const char *dso,
329 std::string &dsoPath)
332 std::string path = cptr;
335 std::vector<std::string> paths;
336 const std::string delimiters=
":";
338 Tokenize(path, paths, delimiters);
341 for (
unsigned int i=0; i < paths.size(); i++) {
342 struct dirent *dirent;
344 std::string dir = paths[i];
345 const char *ds = dir.c_str();
346 DIR *dirfd = opendir(ds);
351 dirent = readdir(dirfd);
352 while (dirent != NULL) {
354 std::string name = dirent->d_name;
357 dsoPath = dir +
"/" + name;
362 dirent = readdir(dirfd);
374bool PluginLoader::resolveGlobalsForPlugins(
const char *dso) {
377 char *cptr = getenv(
"HOUDINI_DSO_PATH");
382 if (!getDso(cptr,dso,sofile)) {
383 std::string dsostring = dso;
384 Msg::print(dsostring +
" is not in HOUDINI_DSO_PATH");
388 void *handle = dlopen(sofile.c_str(), RTLD_GLOBAL|RTLD_NOW);
391 std::cout <<
"Field3D Plugin loader: failed to load Houdini plugin: "
392 << sofile <<
" " << dlerror() <<
"\n";
397 Msg::print(
"---------------------------------------------------------");
399 Msg::print(
"---------------------------------------------------------");
Contains the ClassFactory class for registering Field3D classes.
bool getDirSos(std::vector< std::string > &sos, std::string &dir)
static RegistrationFunc findRegistrationFunc(const std::string &sofile)
static int filter(std::string &name, const char *suffix)
FIELD3D_NAMESPACE_OPEN typedef int(* RegistrationFunc)(ClassFactory &)
Contains the PluginLoader class.
bool getDirSos(std::vector< std::string > &sos, std::string &dir)
static ClassFactory & singleton()
}
PluginLoader()
Default constructor.
~PluginLoader()
Destructor.
static std::vector< std::string > ms_pluginsLoaded
List of plugins loaded.
static void loadPlugins()
Checks all paths in $FIELD3D_DSO_PATH and loads the plugins it finds.
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity.
#define FIELD3D_NAMESPACE_SOURCE_CLOSE