45 #include <sys/types.h>
54 #include <boost/tokenizer.hpp>
69 void tokenize(
const std::string &str,
const std::string &delimiters,
70 std::vector<std::string> &retItems)
72 typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
73 boost::char_separator<char> sep(delimiters.c_str());
74 Tokenizer tok(str, sep);
75 for (Tokenizer::iterator i = tok.begin(); i != tok.end(); ++i) {
76 retItems.push_back(*i);
98 std::string delimiters =
".";
99 std::vector <std::string> items;
101 tokenize(name, delimiters, items);
103 if (items.size() == 0) {
107 if (items[items.size() -1] ==
"so") {
116 bool getDirSos(std::vector<std::string> &sos, std::string &dir)
118 struct dirent *dirent;
120 const char *ds = dir.c_str();
121 DIR *dirfd = opendir(ds);
124 "Field3D_plugin loader: could not open directory " + dir +
"\n";
129 dirent = readdir(dirfd);
130 while (dirent != NULL) {
132 std::string name = dirent->d_name;
135 name = dir +
"/" + name;
139 dirent = readdir(dirfd);
166 char *cptr = getenv(
"FIELD3D_DSO_PATH");
170 std::string path = cptr;
173 std::vector<std::string> paths;
174 const std::string delimiters =
":";
176 tokenize(path, delimiters, paths);
179 for (
unsigned int i = 0; i < paths.size(); i++) {
182 std::vector<std::string> sos;
188 for (
unsigned int j = 0; j < sos.size(); j++) {
189 std::string sofile = sos[j];
192 const std::string pathDelimiter =
"/";
193 std::vector<std::string> pluginName;
194 tokenize(sofile, pathDelimiter, pluginName);
196 bool pluginAlreadyLoaded =
false;
198 for (
unsigned int i = 0; i < ms_pluginsLoaded.size(); i++) {
199 if (pluginName.size() > 0) {
200 if (ms_pluginsLoaded[i] == pluginName[pluginName.size() - 1]) {
203 pluginAlreadyLoaded =
true;
209 if (pluginAlreadyLoaded) {
213 if (pluginName.size() > 0) {
214 std::string lastName = pluginName[pluginName.size() -1];
215 ms_pluginsLoaded.push_back(lastName);
220 void *handle = dlopen(sofile.c_str(), RTLD_GLOBAL|RTLD_NOW);
223 "Field3D Plugin loader: failed to load plugin: " << dlerror() <<
"\n";
232 dlsym(handle,
"registerField3DPlugin");
233 std::string msg =
"Initialized Field3D Plugin " + sofile;
235 char *dlsymError = dlerror();
242 "failed to init Field3D plugin " + sofile);
248 char *debugEnvVar = getenv(
"FIELD3D_DEBUG");
252 "Field3D plugin loader: failed to load "
253 "the symbol registerField3DPlugin");
264 bool PluginLoader::getDso(
char *cptr,
const char *dso,
265 std::string &dsoPath)
268 std::string path = cptr;
271 std::vector<std::string> paths;
272 const std::string delimiters=
":";
274 Tokenize(path, paths, delimiters);
277 for (
unsigned int i=0; i < paths.size(); i++) {
278 struct dirent *dirent;
280 std::string dir = paths[i];
281 const char *ds = dir.c_str();
282 DIR *dirfd = opendir(ds);
287 dirent = readdir(dirfd);
288 while (dirent != NULL) {
290 std::string name = dirent->d_name;
293 dsoPath = dir +
"/" + name;
298 dirent = readdir(dirfd);
310 bool PluginLoader::resolveGlobalsForPlugins(
const char *dso) {
313 char *cptr = getenv(
"HOUDINI_DSO_PATH");
318 if (!getDso(cptr,dso,sofile)) {
319 std::string dsostring = dso;
320 Msg::print(dsostring +
" is not in HOUDINI_DSO_PATH");
324 void *handle = dlopen(sofile.c_str(), RTLD_GLOBAL|RTLD_NOW);
327 std::cout <<
"Field3D Plugin loader: failed to load Houdini plugin: "
328 << sofile <<
" " << dlerror() <<
"\n";
333 Msg::print(
"---------------------------------------------------------");
335 Msg::print(
"---------------------------------------------------------");
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
static void loadPlugins()
Checks all paths in $FIELD3D_DSO_PATH and loads the plugins it finds.
static ClassFactory & singleton()
}
void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
int filter(std::string &name)
static std::vector< std::string > ms_pluginsLoaded
List of plugins loaded.
PluginLoader()
Default constructor.
Contains the PluginLoader class.
bool getDirSos(std::vector< std::string > &sos, std::string &dir)
Contains the ClassFactory class for registering Field3D classes.
~PluginLoader()
Destructor.
#define FIELD3D_NAMESPACE_OPEN