23 #include "common_priv.h"
34 void Loader(
const std::string &ldfile, std::map<std::string, std::string> &data);
42 if (lt_dlinit() > 0) {
43 throw std::runtime_error(lt_dlerror());
50 for (LibMap::iterator itr =
libmap.begin(); itr !=
libmap.end(); ++itr) {
51 lt_dlclose(itr->second);
63 for (std::vector<std::string>::const_iterator i = list.begin(), e = list.end(); i != e; ++i) {
73 for (std::vector<std::string>::const_iterator i = list.begin(), e = list.end(); i != e; ++i) {
79 std::string ext =
".nodelist";
81 if (dir.IsRegularFile()) {
82 std::string fname = dir.BaseName();
83 if (fname[0] ==
'.') {
continue; }
84 if (fname.size() < ext.size()
85 || fname.substr(fname.size() - ext.size(), ext.size()) != ext) {
94 for (std::vector<std::string>::const_iterator i = dirnames.begin(), e = dirnames.end(); i != e; ++i) {
101 FactoryMap::iterator entry =
factorymap.find(nodetype);
107 return entry->second.get();
112 std::string name = factory->GetName();
113 factorymap.insert(std::make_pair(name, factory));
120 if (lt_dladvise_init(&advise) > 0) {
121 throw std::runtime_error(lt_dlerror());
125 lt_dladvise_destroy(&advise);
128 if (lt_dladvise_global(&advise) > 0) {
129 throw std::runtime_error(lt_dlerror());
135 lt_dlhandle handle = lt_dlopenadvise(lib.c_str(), advise.advise);
137 throw std::runtime_error(lt_dlerror());
139 libmap.insert(std::make_pair(lib, handle));
147 lt_dlhandle handle = 0;
157 handle = lt_dlopen(0);
159 throw std::runtime_error(lt_dlerror());
163 init.vptr = lt_dlsym(handle, sym.c_str());
164 const char *error = lt_dlerror();
166 NodeLibMap::iterator itr =
nodelibmap.find(sym);
168 throw std::runtime_error(
"Unable to find node type " + name);
172 init.vptr = lt_dlsym(handle, sym.c_str());
173 error = lt_dlerror();
175 throw std::runtime_error(error);
178 shared_ptr<NodeFactory> factory = init.fn();
179 factorymap.insert(std::make_pair(factory->GetName(), factory));
190 using std::stringstream;
195 void ProcessLine(vector<string> &stack,
const string &curdir,
196 map<string, string> &data,
unsigned rep);
198 void Loader(
const string &ldfile, map<string, string> &data,
unsigned rep) {
199 ASSERT(rep < 100,
"Loader include overflow");
201 f.open(ldfile.c_str());
202 ASSERT(f.is_open(),
"Failed to open %s", ldfile.c_str());
204 vector<char> token_stack;
205 vector<string> stack;
206 bool in_comment =
false;
208 bool in_single_quote =
false;
209 bool in_double_quote =
false;
216 if (c ==
'\n' || c ==
'\r') {
222 if (in_single_quote) {
224 in_single_quote =
false;
226 token_stack.push_back(c);
229 token_stack.push_back(c);
231 }
else if (in_double_quote) {
233 in_double_quote =
false;
235 token_stack.push_back(c);
243 in_single_quote =
true;
246 in_double_quote =
true;
252 if (!token_stack.empty()) {
253 stack.push_back(
string(&token_stack[0], token_stack.size()));
266 if (!token_stack.empty()) {
268 stack.push_back(
string(&token_stack[0], token_stack.size()));
273 token_stack.push_back(c);
282 map<string, string> &data,
unsigned rep) {
283 if (stack.empty())
return;
284 vector<string>::iterator itr = stack.begin();
287 if (itr != stack.end()) {
288 string libname = *itr;
292 if (!libname.empty()) {
294 while (itr != stack.end()) {
295 data[*itr] = libname;
300 }
else if (*itr ==
"include") {
302 while (itr != stack.end()) {
307 Loader(inc, data, rep + 1);
314 void Loader(
const string &ldfile, map<string, string> &data) {
shared_ptr< NodeFactory >(* CPNInitPrototype)(void)
This is the prototype of the function that is called by the dynamic library loading facility...
void LoadSharedLib(const std::string &libname)
#define CPN_DEFAULT_INIT_SYMBOL_STR
void SearchDirectory(const std::string &dirname)
void RegisterFactory(shared_ptr< NodeFactory > factory)
NodeFactory * GetFactory(const std::string &nodename)
void Loader(const std::string &ldfile, std::map< std::string, std::string > &data)
void InternalLoadLib(const std::string &lib)
void InternalLoad(const std::string &sym)
void ProcessLine(vector< string > &stack, const string &curdir, map< string, string > &data, unsigned rep)
void LoadNodeList(const std::string &filename)
std::string RealPath(const std::string &path)
std::string DirName(const std::string &path)
std::string PathConcat(const std::string &dir, const std::string &file)
bool IsAbsPath(const std::string &path)
The node factory provides a method for the kernel to create arbitrary user defined Nodes...
Definition of the NodeFactory.