bool findRelativePath(const filesystem::path& from_, const filesystem::path& to, filesystem::path& out_relativePath) { if(from_.is_complete() && to.is_complete()){ filesystem::path from; makePathCompact(from_, from); filesystem::path::const_iterator p = from.begin(); filesystem::path::const_iterator q = to.begin(); while(p != from.end() && q != to.end()){ if(!comparePathIterator(p, q)){ break; } ++p; ++q; } out_relativePath.clear(); while(p != from.end()){ out_relativePath /= ".."; ++p; } while(q != to.end()){ out_relativePath /= *q++; } return true; } return false; }
int findSubDirectory(const filesystem::path& directory, const filesystem::path& path, filesystem::path& out_subdirectory) { int numMatchedDepth = 0; if(directory.is_absolute() && path.is_absolute()){ filesystem::path compactPath; makePathCompact(path, compactPath); filesystem::path::const_iterator p = directory.begin(); filesystem::path::const_iterator q = compactPath.begin(); while(p != directory.end() && q != compactPath.end()){ if(!comparePathIterator(p, q)){ break; } ++numMatchedDepth; ++p; ++q; } if(p == directory.end()){ out_subdirectory.clear(); while(q != compactPath.end()){ out_subdirectory /= *q++; } return numMatchedDepth; } } return 0; }
void findExecutablePath() { #ifdef _WIN32 static const int BUFSIZE = 1024; TCHAR execFilePath[BUFSIZE]; if(GetModuleFileName(NULL, execFilePath, BUFSIZE)){ #ifndef UNICODE executablePath_ = execFilePath; #else int codepage = _getmbcp(); const int newSize = WideCharToMultiByte(codepage, 0, execFilePath, -1, NULL, 0, NULL, NULL); if(newSize > 0){ vector<filesystem::path::String> execFilePathMB(newSize + 1); newSize = WideCharToMultiByte(codepage, 0, execFilePath, -1, &execFilePathMB[0], newSize + 1, NULL, NULL); executablePath_ = execFilePathUtf8; ; } #endif // UNICODE } #endif #ifdef __linux__ utsname info; if(uname(&info) == 0){ if(strncmp(info.sysname, "Linux", 6) == 0){ static const int BUFSIZE = 1024; char buf[BUFSIZE]; int n = readlink("/proc/self/exe", buf, BUFSIZE - 1); buf[n] = 0; executablePath_ = buf; } } #endif #ifdef MACOSX char buf[1024]; uint32_t n = sizeof(buf); if(_NSGetExecutablePath(buf, &n) == 0){ executablePath_ = buf; } filesystem::path path; // remove dot from a path like bin/./choreonoid makePathCompact(filesystem::path(executablePath_), path); //filesystem::path path = filesystem::canonical(filesystem::path(executablePath_)); #else filesystem::path path(executablePath_); #endif filesystem::path topPath = path.parent_path().parent_path(); executableTopDirectory_ = topPath.string(); filesystem::path sharePath = topPath / CNOID_SHARE_SUBDIR; if(filesystem::is_directory(sharePath)){ shareDirectory_ = getNativePathString(sharePath); } else if(filesystem::is_directory(sharePath.parent_path())){ shareDirectory_ = getNativePathString(sharePath.parent_path()); } else if(topPath.has_parent_path()){ // case of a sub build directory sharePath = topPath.parent_path() / "share"; if(filesystem::is_directory(sharePath)){ shareDirectory_ = getNativePathString(sharePath); } } #ifdef _WIN32 if(path.extension() == ".exe"){ executableBasename_ = getBasename(path); } else { executableBasename_ = getFilename(path); } #else executableBasename_ = getFilename(path); #endif }