std::vector<std::string> default_search_paths() { std::vector<std::string> paths; #ifdef CHAISCRIPT_WINDOWS // force no unicode CHAR path[4096]; int size = GetModuleFileNameA(0, path, sizeof(path) - 1); std::string exepath(path, size); size_t lastslash = exepath.rfind('\\'); size_t secondtolastslash = exepath.rfind('\\', lastslash - 1); if (lastslash != std::string::npos) { paths.push_back(exepath.substr(0, lastslash)); } if (secondtolastslash != std::string::npos) { return{ exepath.substr(0, secondtolastslash) + "\\lib\\chaiscript\\" }; } #else std::string exepath; std::vector<char> buf(2048); ssize_t size = -1; if ((size = readlink("/proc/self/exe", &buf.front(), buf.size())) > 0) { exepath = std::string(&buf.front(), static_cast<size_t>(size)); } if (exepath.empty()) { if ((size = readlink("/proc/curproc/file", &buf.front(), buf.size())) > 0) { exepath = std::string(&buf.front(), static_cast<size_t>(size)); } } if (exepath.empty()) { if ((size = readlink("/proc/self/path/a.out", &buf.front(), buf.size())) > 0) { exepath = std::string(&buf.front(), static_cast<size_t>(size)); } } if (exepath.empty()) { Dl_info rInfo; memset(&rInfo, 0, sizeof(rInfo)); if (!dladdr(cast_module_symbol(&default_search_paths), &rInfo) || !rInfo.dli_fname) { return paths; } exepath = std::string(rInfo.dli_fname); } size_t lastslash = exepath.rfind('/'); size_t secondtolastslash = exepath.rfind('/', lastslash - 1); if (lastslash != std::string::npos) { paths.push_back(exepath.substr(0, lastslash)); } if (secondtolastslash != std::string::npos) { paths.push_back(exepath.substr(0, secondtolastslash) + "/lib/chaiscript/"); } #endif return paths; }
std::string default_search_path() { #ifdef CHAISCRIPT_WINDOWS TCHAR path[2048]; int size = GetModuleFileName(0, path, sizeof(path)-1); std::string exepath(path, size); size_t secondtolastslash = exepath.rfind('\\', exepath.rfind('\\') - 1); if (secondtolastslash != std::string::npos) { return exepath.substr(0, secondtolastslash) + "\\lib\\chaiscript\\"; } else { return ""; } #else std::string exepath; std::vector<char> buf(2048); ssize_t size = -1; if ((size = readlink("/proc/self/exe", &buf.front(), buf.size())) != -1) { exepath = std::string(&buf.front(), size); } if (exepath.empty()) { if ((size = readlink("/proc/curproc/file", &buf.front(), buf.size())) != -1) { exepath = std::string(&buf.front(), size); } } if (exepath.empty()) { if ((size = readlink("/proc/self/path/a.out", &buf.front(), buf.size())) != -1) { exepath = std::string(&buf.front(), size); } } if (exepath.empty()) { Dl_info rInfo; memset( &rInfo, 0, sizeof(rInfo) ); if ( !dladdr(cast_module_symbol(&default_search_path), &rInfo) || !rInfo.dli_fname ) { return ""; } exepath = std::string(rInfo.dli_fname); } size_t secondtolastslash = exepath.rfind('/', exepath.rfind('/') - 1); if (secondtolastslash != std::string::npos) { return exepath.substr(0, secondtolastslash) + "/lib/chaiscript/"; } else { return ""; } #endif }