Field3D
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()

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

Definition at line 105 of file PluginLoader.cpp.

106{
107 std::string delimiters = ".";
108 std::vector <std::string> items;
109
110 tokenize(name, delimiters, items);
111
112 if (items.size() == 0) {
113 return 0;
114 }
115
116 if (items[items.size() -1] == suffix) {
117 return 1;
118 }
119
120 return 0;
121}

Referenced by getDirSos().

◆ getDirSos()

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

Definition at line 125 of file PluginLoader.cpp.

126{
127#ifdef WIN32
128 const char *ds = dir.c_str();
129 HANDLE dirh;
130 WIN32_FIND_DATAA fd;
131
132 dirh = FindFirstFileA(ds, &fd);
133 while (dirh != INVALID_HANDLE_VALUE)
134 {
135 std::string name = fd.cFileName;
136 std::string name_lower;
137
138 std::transform(name.begin(), name.end(), name_lower.begin(), ::tolower);
139
140 if (filter(name_lower, "so")) {
141 name = dir + "/" + name;
142 sos.push_back(name);
143 }
144
145 if (!FindNextFileA(dirh, &fd))
146 {
147 ::FindClose(dirh);
148 break;
149 }
150 }
151#else
152 struct dirent *dirent;
153
154 const char *ds = dir.c_str();
155 DIR *dirfd = opendir(ds);
156 if (!dirfd) {
157 std::string er =
158 "Field3D_plugin loader: could not open directory " + dir + "\n";
159 //perror(er.c_str());
160 return false;
161 }
162
163 dirent = readdir(dirfd);
164 while (dirent != NULL) {
165
166 std::string name = dirent->d_name;
167
168 if (filter(name, "so")) {
169 name = dir + "/" + name;
170 sos.push_back(name);
171 }
172
173 dirent = readdir(dirfd);
174 }
175
176 closedir(dirfd);
177#endif
178 return true;
179}
static int filter(std::string &name, const char *suffix)

References filter().

Referenced by PluginLoader::loadPlugins().

◆ findRegistrationFunc()

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

Definition at line 181 of file PluginLoader.cpp.

182{
183#ifdef WIN32
184 HMODULE handle = ::LoadLibraryA(sofile.c_str());
185#else
186 void *handle = dlopen(sofile.c_str(), RTLD_GLOBAL|RTLD_NOW);
187#endif
188 // Attempt to load .so file
189 if (!handle) {
190 std::string errmsg;
191#ifdef WIN32
192 char *errstr;
193//----------------------------------------------------------------------------//
194 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
195 NULL, GetLastError(),
196 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&errstr,
197 0, NULL);
198 if (errstr) {
199 errmsg = errstr;
200 ::LocalFree(errstr);
201 } else {
202 errmsg = "Unknown error";
203 }
204#else
205 errmsg = dlerror();
206#endif
207
208 std::cout <<
209 "Field3D Plugin loader: failed to load plugin: " << errmsg << "\n";
210 return 0;
211 }
212
213 // Determine plugin type by looking for one of:
214 // registerField3DPlugin()
215
216 RegistrationFunc fptr;
217
218#ifdef WIN32
219 fptr = (RegistrationFunc)GetProcAddress(handle,"registerField3DPlugin");
220#else
221 fptr = (RegistrationFunc)dlsym(handle,"registerField3DPlugin");
222#endif
223 if (!fptr) {
224 char *debugEnvVar = getenv("FIELD3D_DEBUG");
225 if (debugEnvVar) {
226 // debug env var exist, so print warning
228 "Field3D plugin loader: failed to load "
229 "the symbol registerField3DPlugin");
230 }
231 }
232 return fptr;
233}
FIELD3D_NAMESPACE_OPEN typedef int(* RegistrationFunc)(ClassFactory &)
@ SevWarning
Definition Log.h:68
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().