PluginLoader.cpp File Reference

Contains implementations of plugin loading functions. More...

`#include <dlfcn.h>`
`#include <dirent.h>`
`#include <sys/types.h>`
`#include <stdlib.h>`
`#include <string>`
`#include <vector>`
`#include <stdio.h>`
`#include <errno.h>`
`#include <string.h>`
`#include <boost/tokenizer.hpp>`
`#include "ClassFactory.h"`
`#include "PluginLoader.h"`

Go to the source code of this file.

## Functions

static int | filter (std::string &name, const char *suffix)
static RegistrationFunc | findRegistrationFunc (const std::string &sofile)
bool | getDirSos (std::vector< std::string > &sos, std::string &dir)

## Variables

FIELD3D_NAMESPACE_OPEN typedef int(* | RegistrationFunc )(ClassFactory &)

## Detailed Description

Contains implementations of plugin loading functions.

Definition in file PluginLoader.cpp.

## Function Documentation

## filter()

int filter | ( | std::string & | name,
|  | const char * | suffix )

static

Definition at line 105 of file PluginLoader.cpp.

{
std::string delimiters = ".";
std::vector <std::string> items;
tokenize(name, delimiters, items);
if (items.size() == 0) {
return 0;
}
if (items[items.size() -1] == suffix) {
return 1;
}
return 0;
}

Referenced by getDirSos().

## getDirSos()

bool getDirSos | ( | std::vector< std::string > & | sos,
|  | std::string & | dir )

Definition at line 125 of file PluginLoader.cpp.

{
#ifdef WIN32
const char *ds = dir.c_str();
HANDLE dirh;
WIN32_FIND_DATAA fd;
dirh = FindFirstFileA(ds, &fd);
while (dirh != INVALID_HANDLE_VALUE)
{
std::string name = fd.cFileName;
std::string name_lower;
std::transform(name.begin(), name.end(), name_lower.begin(), ::tolower);
if (filter(name_lower, "so")) {
name = dir + "/" + name;
sos.push_back(name);
}
if (!FindNextFileA(dirh, &fd))
{
::FindClose(dirh);
break;
}
}
#else
struct dirent *dirent;
const char *ds = dir.c_str();
DIR *dirfd = opendir(ds);
if (!dirfd) {
std::string er =
"Field3D_plugin loader: could not open directory " + dir + "\n";
//perror(er.c_str());
return false;
}
dirent = readdir(dirfd);
while (dirent != NULL) {
std::string name = dirent->d_name;
if (filter(name, "so")) {
name = dir + "/" + name;
sos.push_back(name);
}
dirent = readdir(dirfd);
}
closedir(dirfd);
#endif
return true;
}
filter
static int filter(std::string &name, const char *suffix)
Definition PluginLoader.cpp:105

References filter().

Referenced by PluginLoader::loadPlugins().

## findRegistrationFunc()

RegistrationFunc findRegistrationFunc | ( | const std::string & | sofile | ) |

static

Definition at line 181 of file PluginLoader.cpp.

{
#ifdef WIN32
HMODULE handle = ::LoadLibraryA(sofile.c_str());
#else
void *handle = dlopen(sofile.c_str(), RTLD_GLOBAL|RTLD_NOW);
#endif
// Attempt to load .so file
if (!handle) {
std::string errmsg;
#ifdef WIN32
char *errstr;
//----------------------------------------------------------------------------//
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&errstr,
0, NULL);
if (errstr) {
errmsg = errstr;
::LocalFree(errstr);
} else {
errmsg = "Unknown error";
}
#else
errmsg = dlerror();
#endif
std::cout <<
"Field3D Plugin loader: failed to load plugin: " << errmsg << "\n";
return 0;
}
// Determine plugin type by looking for one of:
// registerField3DPlugin()
RegistrationFunc fptr;
#ifdef WIN32
fptr = (RegistrationFunc)GetProcAddress(handle,"registerField3DPlugin");
#else
fptr = (RegistrationFunc)dlsym(handle,"registerField3DPlugin");
#endif
if (!fptr) {
char *debugEnvVar = getenv("FIELD3D_DEBUG");
if (debugEnvVar) {
// debug env var exist, so print warning
Msg::print(Msg::SevWarning,
"Field3D plugin loader: failed to load "
"the symbol registerField3DPlugin");
}
}
return fptr;
}
RegistrationFunc
FIELD3D_NAMESPACE_OPEN typedef int(* RegistrationFunc)(ClassFactory &)
Definition PluginLoader.cpp:92
Msg::SevWarning
@ SevWarning
Definition Log.h:68
Msg::print
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity.
Definition Log.cpp:70

References Msg::print(), RegistrationFunc, and Msg::SevWarning.

Referenced by PluginLoader::loadPlugins().

## Variable Documentation

## RegistrationFunc

FIELD3D_NAMESPACE_OPEN typedef int(* RegistrationFunc) (ClassFactory &) | ( | ClassFactory & |  | ) |

Definition at line 92 of file PluginLoader.cpp.

Referenced by findRegistrationFunc(), and PluginLoader::loadPlugins().