void split_paths( const filesystem::path& p1, const filesystem::path& p2, filesystem::path& common, filesystem::path& r1, filesystem::path& r2) { assert(common.empty()); assert(r1.empty()); assert(r2.empty()); filesystem::path::const_iterator i1 = p1.begin(); filesystem::path::const_iterator i2 = p2.begin(); while (i1 != p1.end() && i2 != p2.end()) { if (*i1 != *i2) break; if ((p1.has_filename() && succ(i1) == p1.end()) || (p2.has_filename() && succ(i2) == p2.end())) break; common /= *i1; ++i1, ++i2; } while (i1 != p1.end()) r1 /= *i1++; while (i2 != p2.end()) r2 /= *i2++; }
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; }
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; }
void makePathCompact(const filesystem::path& path, filesystem::path& out_compact) { out_compact.clear(); for(filesystem::path::const_iterator p = path.begin(); p != path.end(); ++p){ if(*p == ".."){ out_compact = out_compact.parent_path(); } else if(*p != "."){ out_compact /= *p; } } }