Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "g2o_common.h"
00018
00019 #include "dl_wrapper.h"
00020 #include "g2o/stuff/string_tools.h"
00021 #include "g2o/stuff/filesys_tools.h"
00022
00023 #include <vector>
00024 #include <iostream>
00025 #include <cstdlib>
00026 using namespace ::std;
00027
00028
00029
00030
00031 #ifdef __APPLE__
00032 #define SO_EXT "dylib"
00033 #elif defined (WINDOWS) || defined (CYGWIN)
00034 #define SO_EXT "dll"
00035 #else // Linux
00036 #define SO_EXT "so"
00037 #endif
00038
00039
00040 #if defined (UNIX) || defined(CYGWIN)
00041 #include <dlfcn.h>
00042 static Dl_info info;
00043 #define PATH_SEPARATOR ":"
00044 #else // WINDOWS
00045 #define PATH_SEPARATOR ";"
00046
00047 static void fakeFunctionForWindows() {}
00048
00049 HMODULE getMyInstance()
00050 {
00051 MEMORY_BASIC_INFORMATION mbi;
00052 if (VirtualQuery((const void *)&fakeFunctionForWindows, &mbi, sizeof(mbi))) {
00053 return (HMODULE) mbi.AllocationBase;
00054 }
00055 return NULL;
00056 }
00057 #endif
00058
00059
00060
00061 #ifndef G2O_LIBRARY_POSTFIX
00062 #define G2O_LIBRARY_POSTFIX ""
00063 #endif
00064
00065 static const string TYPES_PATTERN=string("*_types_*")+string(G2O_LIBRARY_POSTFIX)+string(".")+string(SO_EXT);
00066 static const string SOLVERS_PATTERN=string("*_solver_*")+string(G2O_LIBRARY_POSTFIX)+string(".")+string(SO_EXT);
00067
00068 namespace g2o {
00069
00070 void findArguments(const std::string& option, vector<string>& args, int argc, char** argv)
00071 {
00072 args.clear();
00073 for (int i = 0; i < argc; ++i) {
00074 if (argv[i] == option && i + 1 < argc) {
00075 args.push_back(argv[i+1]);
00076 }
00077 }
00078 }
00079
00080 void loadStandardTypes(DlWrapper& dlTypesWrapper, int argc, char** argv)
00081 {
00082 char * envTypesPath = getenv("G2O_TYPES_DIR");
00083 string typesPath;
00084
00085 if (envTypesPath != NULL) {
00086 typesPath = envTypesPath;
00087 } else {
00088 typesPath = G2O_DEFAULT_TYPES_DIR_;
00089 #if defined (UNIX) || defined(CYGWIN)
00090 if (dladdr(&info, &info) != 0) {
00091 typesPath = getDirname(info.dli_fname);
00092 }
00093 #else // Windows
00094 char libFilename[MAX_PATH + 1];
00095 HMODULE instance = getMyInstance();
00096 if (instance && GetModuleFileName(instance, libFilename, MAX_PATH) > 0) {
00097 typesPath = getDirname(libFilename);
00098 }
00099 #endif
00100 }
00101
00102 vector<string> paths = strSplit(typesPath, PATH_SEPARATOR);
00103 for (vector<string>::const_iterator it = paths.begin(); it != paths.end(); ++it) {
00104 if (it->size() > 0)
00105 dlTypesWrapper.openLibraries(*it, TYPES_PATTERN);
00106 }
00107
00108 vector<string> libs;
00109 if (argc > 0 && argv != 0)
00110 findArguments("-typeslib", libs, argc, argv);
00111 for (vector<string>::const_iterator it = libs.begin(); it != libs.end(); ++it) {
00112 cerr << "Loading types " << *it << endl;
00113 dlTypesWrapper.openLibrary(*it);
00114 }
00115 }
00116
00117 void loadStandardSolver(DlWrapper& dlSolverWrapper, int argc, char** argv)
00118 {
00119 char * envSolversPath = getenv("G2O_SOLVERS_DIR");
00120 string solversPath;
00121
00122 if (envSolversPath != NULL) {
00123 solversPath = envSolversPath;
00124 } else {
00125 solversPath = G2O_DEFAULT_SOLVERS_DIR_;
00126 #if defined (UNIX) || defined(CYGWIN)
00127 if (dladdr(&info, &info) != 0) {
00128 solversPath = getDirname(info.dli_fname);
00129 }
00130 #else // Windows
00131 char libFilename[MAX_PATH + 1];
00132 HMODULE instance = getMyInstance();
00133 if (instance && GetModuleFileName(instance, libFilename, MAX_PATH) > 0) {
00134 solversPath = getDirname(libFilename);
00135 }
00136 #endif
00137 }
00138
00139 vector<string> paths = strSplit(solversPath, PATH_SEPARATOR);
00140 for (vector<string>::const_iterator it = paths.begin(); it != paths.end(); ++it) {
00141 if (it->size() > 0)
00142 dlSolverWrapper.openLibraries(*it, SOLVERS_PATTERN);
00143 }
00144
00145 vector<string> libs;
00146 if (argc > 0 && argv != 0)
00147 findArguments("-solverlib", libs, argc, argv);
00148 for (vector<string>::const_iterator it = libs.begin(); it != libs.end(); ++it) {
00149 cerr << "Loading solver " << *it << endl;
00150 dlSolverWrapper.openLibrary(*it);
00151 }
00152 }
00153
00154 }