|
From: Lasse Kärkkäi. <tr...@us...> - 2009-11-11 19:23:26
|
Module: performous
Branch: master
Commit: 33cbfd782fa162eca8bbb723484524417d747fb7
Author: Lasse Karkkainen <tro...@tr...>
Date: Wed Nov 11 21:23:20 2009 +0200
Plugin++ loader using execname to find libs
---
libs/plugin++/CMakeLists.txt | 2 +-
libs/plugin++/include/plugin++/loader.hpp | 40 +----------------
libs/plugin++/src/loader.cc | 66 +++++++++++++++++++++++++++++
3 files changed, 70 insertions(+), 38 deletions(-)
diff --git a/libs/plugin++/CMakeLists.txt b/libs/plugin++/CMakeLists.txt
index 5ade017..79fc033 100644
--- a/libs/plugin++/CMakeLists.txt
+++ b/libs/plugin++/CMakeLists.txt
@@ -8,7 +8,7 @@ if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98 -Wall -Wextra")
endif(CMAKE_COMPILER_IS_GNUCXX)
-add_library(plugin++ SHARED src/dll.cc src/execname.cc)
+add_library(plugin++ SHARED src/dll.cc src/execname.cc src/loader.cc)
if(NOT WIN32)
target_link_libraries(plugin++ dl)
diff --git a/libs/plugin++/include/plugin++/loader.hpp b/libs/plugin++/include/plugin++/loader.hpp
index 89519e6..5c551a9 100644
--- a/libs/plugin++/include/plugin++/loader.hpp
+++ b/libs/plugin++/include/plugin++/loader.hpp
@@ -1,15 +1,10 @@
#ifndef LOADER_HPP_INCLUDED
#define LOADER_HPP_INCLUDED
-#pragma warning(disable: 4996) // Disable stupid Microsoft warnings about stdlib functions
-
#include "dll.hpp"
-#include <iostream>
#include <set>
-#include <sstream>
#include <boost/filesystem.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
-#include <stdexcept>
namespace plugin {
namespace fs = boost::filesystem;
@@ -17,39 +12,10 @@ namespace plugin {
/** @short A helper for loading all matching libraries in a folder. **/
class loader {
boost::ptr_vector<dll> dlls;
- void parse(std::set<fs::path>& paths, char const* var, fs::path const& folder = fs::path()) {
- if (!var) return;
- std::istringstream iss(var);
- std::string elem;
- while (std::getline(iss, elem, ':')) {
- fs::path p = elem;
- if (!folder.empty()) p /= folder;
- if (fs::is_directory(p)) paths.insert(p);
- }
- }
- /** Try to load all libraries in the given folder. **/
- void load(fs::path const& path) {
- for (fs::directory_iterator it(path), end; it != end; ++it) {
- try {
- dlls.push_back(new dll(it->string()));
- } catch (std::runtime_error const& e) {
- std::cerr << e.what() << std::endl;
- } catch (...) {
- std::cerr << "WTF?" << std::endl;
- }
- }
- }
+ void parse(std::set<fs::path>& paths, char const* var, fs::path const& folder = fs::path());
+ void load(fs::path const& path);
public:
- loader(fs::path const& folder) {
- std::set<fs::path> paths;
- parse(paths, std::getenv("PLUGIN_PATH"));
- if (!folder.empty()) {
- parse(paths, std::getenv("LD_LIBRARY_PATH"), folder);
- parse(paths, "/usr/lib:/usr/local/lib", folder);
- }
- for (std::set<fs::path>::const_iterator it = paths.begin(); it != paths.end(); ++it) load(*it);
- if (dlls.empty()) std::cerr << "No plugins found. Try setting PLUGIN_PATH or LD_LIBRARY_PATH." << std::endl;
- }
+ loader(fs::path const& folder);
};
}
diff --git a/libs/plugin++/src/loader.cc b/libs/plugin++/src/loader.cc
new file mode 100644
index 0000000..199698c
--- /dev/null
+++ b/libs/plugin++/src/loader.cc
@@ -0,0 +1,66 @@
+#include <plugin++/loader.hpp>
+#include <plugin++/execname.hpp>
+#include <iostream>
+#include <sstream>
+#include <stdexcept>
+
+#ifdef _MSC_VER
+#pragma warning(disable: 4996) // Disable stupid Microsoft warnings about stdlib functions
+#endif
+
+using namespace plugin;
+namespace fs = boost::filesystem;
+
+#ifdef _WIN32
+#define PATHSEP ';'
+#else
+#define PATHSEP ':'
+#endif
+
+/// Break var (a delimited path string) into paths (appends folder if supplied).
+void loader::parse(std::set<fs::path>& paths, char const* var, fs::path const& folder) {
+ if (!var) return;
+ std::istringstream iss(var);
+ std::string elem;
+ while (std::getline(iss, elem, PATHSEP)) {
+ fs::path p = elem;
+ if (!folder.empty()) p /= folder;
+ if (fs::is_directory(p)) paths.insert(p);
+ }
+}
+
+/// Try to load all libraries in the given folder.
+void loader::load(fs::path const& path) {
+ for (fs::directory_iterator it(path), end; it != end; ++it) {
+ try {
+ dlls.push_back(new dll(it->string()));
+ } catch (std::runtime_error const& e) {
+ std::cerr << e.what() << std::endl;
+ }
+ }
+}
+
+loader::loader(fs::path const& folder) {
+ std::set<fs::path> paths;
+ // Try PLUGIN_PATH environment setting
+ parse(paths, std::getenv("PLUGIN_PATH"));
+ // Try other options only if folder was given
+ if (!folder.empty()) {
+ // Try relative path from executable
+ fs::path p = execname();
+ p = p.parent_path().parent_path();
+ if (!p.empty()) {
+ p /= "lib" / folder;
+ if (fs::is_directory(p)) paths.insert(p);
+ }
+#ifndef _WIN32
+ // Try UNIX library paths
+ parse(paths, std::getenv("LD_LIBRARY_PATH"), folder);
+ parse(paths, "/usr/lib:/usr/local/lib", folder);
+#endif
+ }
+ // Load the contents of each folder
+ for (std::set<fs::path>::const_iterator it = paths.begin(); it != paths.end(); ++it) load(*it);
+ if (dlls.empty()) std::cerr << "No plugins found. Try setting PLUGIN_PATH to the folder with the plugins." << std::endl;
+}
+
|