TEST_F(PathTest, root_parent) { const fs::path p("/"); { int i = 0; for (auto it = p.begin(); it != p.end(); ++it) { ++i; } EXPECT_EQ(1, i); } const fs::path q(p.parent_path()); { int i = 0; for (auto it = q.begin(); it != q.end(); ++it) { ++i; } EXPECT_EQ(0, i); } }
static boost::filesystem::path path_sub(const boost::filesystem::path& lft, const boost::filesystem::path& rht) { boost::filesystem::path::iterator lft_itr = lft.begin(); boost::filesystem::path::iterator lft_end = lft.end(); boost::filesystem::path::iterator rht_itr = rht.begin(); boost::filesystem::path::iterator rht_end = rht.end(); while ((lft_itr != lft_end) && (rht_itr != rht_end)) { if (*lft_itr != *rht_itr) { break; } ++lft_itr; ++rht_itr; } boost::filesystem::path result; for (; lft_itr != lft_end; ++lft_itr) { result /= *lft_itr; } return std::move(result); }
boost::filesystem::path naive_uncomplete(boost::filesystem::path const path, boost::filesystem::path const base) { if (path.has_root_path()){ if (path.root_path() != base.root_path()) { return path; } else { return naive_uncomplete(path.relative_path(), base.relative_path()); } } else { if (base.has_root_path()) { throw "cannot uncomplete a path relative path from a rooted base"; } else { typedef boost::filesystem::path::const_iterator path_iterator; path_iterator path_it = path.begin(); path_iterator base_it = base.begin(); while ( path_it != path.end() && base_it != base.end() ) { if (*path_it != *base_it) break; ++path_it; ++base_it; } boost::filesystem::path result; for (; base_it != base.end(); ++base_it) { result /= ".."; } for (; path_it != path.end(); ++path_it) { result /= *path_it; } return result; } } }
bool dir_contains_path(boost::filesystem::path dir, boost::filesystem::path path) { dir = normalized_path(fs::system_complete(dir)); path = normalized_path(fs::system_complete(path)); for (fs::path::const_iterator diri(dir.begin()), pathi(path.begin()); diri != dir.end(); ++diri, ++pathi) if (pathi == path.end() || *diri != *pathi) return false; return true; }
fs::path makeRelative(fs::path fromPath, fs::path toPath) { fs::path res; for(fs::path::iterator from = fromPath.begin(), to = toPath.begin(); to != toPath.end(); ++to) { if(*from != *to) res /= (*to); if(from != fromPath.end()) ++from; } return res; }
unsigned short int AddFileAndParents(boost::filesystem::path p, unsigned short int parent, std::string prefix, file_list& files) { if(!boost::filesystem::exists(p)) { std::cerr << "Could not open " << p.string() << std::endl; return parent; } boost::filesystem::path base; for(boost::filesystem::path::iterator it = p.begin(); it != p.end(); ++it) { if(base.empty()) { base = *it; } else { std::string name = base.string() + "/" + it->string(); base = boost::filesystem::path(name); } parent = AddFile(base, parent, prefix, files); } return parent; }
void ScanFile(std::vector<ImageEntryType>& List, boost::filesystem::path const& Path) { if (!is_directory(Path)) { AddFile(List, Path, Path.leaf()); return; } using namespace boost::filesystem; typedef boost::filesystem::recursive_directory_iterator Iterator; std::size_t BaseOffset=std::distance(Path.begin(), Path.end()); for (Iterator i(Path), ie; i!=ie; ++i) { if (is_symlink(*i)) i.no_push(); if (!is_regular_file(*i)) continue; path Absolute=*i; path Relative; auto j=Absolute.begin(); std::size_t c=0; for (; j!=Absolute.end(); ++j, ++c) if (c >= BaseOffset) Relative /= *j; if (!Relative.empty()) AddFile(List, Path/Relative, Relative); } }
boost::filesystem::path fcppt::filesystem::strip_prefix( boost::filesystem::path const &_prefix, boost::filesystem::path const &_path ) { boost::filesystem::path result; std::for_each( std::next( _path.begin(), fcppt::cast::to_signed( fcppt::filesystem::num_subpaths( _prefix ) ) ), _path.end(), [ &result ]( boost::filesystem::path const &_entry ) { result /= _entry; } ); return result; }
boost::filesystem::path MultiresImagePlugin::MakePathRelative(boost::filesystem::path path, boost::filesystem::path base) { // Borrowed from: https://svn.boost.org/trac/boost/ticket/1976#comment:2 if (path.has_root_path()) { if (path.root_path() != base.root_path()) { return path; } else { return MakePathRelative(path.relative_path(), base.relative_path()); } } else { if (base.has_root_path()) { ROS_WARN("Cannot uncomplete a path relative path from a rooted base."); return path; } else { typedef boost::filesystem::path::const_iterator path_iterator; path_iterator path_it = path.begin(); path_iterator base_it = base.begin(); while (path_it != path.end() && base_it != base.end()) { if (*path_it != *base_it) break; ++path_it; ++base_it; } boost::filesystem::path result; for (; base_it != base.end(); ++base_it) { result /= ".."; } for (; path_it != path.end(); ++path_it) { result /= *path_it; } return result; } } }
void chopOffFirstElement( boost::filesystem::path & fileName ) { boost::filesystem::path reducedPath; boost::filesystem::path::iterator pit = fileName.begin(); for ( ++pit ; pit != fileName.end() ; ++pit ) { reducedPath /= *pit; } fileName = reducedPath; }
fs::path make_relative(fs::path a_From, fs::path a_To) { a_From = fs::absolute(a_From); a_To = fs::absolute(a_To); fs::path ret; fs::path::const_iterator itrFrom(a_From.begin()), itrTo(a_To.begin()); // Find common base for (fs::path::const_iterator toEnd(a_To.end()), fromEnd(a_From.end()); itrFrom != fromEnd && itrTo != toEnd && *itrFrom == *itrTo; ++itrFrom, ++itrTo); // Navigate backwards in directory to reach previously found base for (fs::path::const_iterator fromEnd(a_From.end()); itrFrom != fromEnd; ++itrFrom) { if( (*itrFrom) != "." ) ret /= ".."; } // Now navigate down the directory branch ret.append( itrTo, a_To.end() ); return ret; }
// Return path when appended to from will resolve to same as to boost::filesystem::path makeRelativePath(boost::filesystem::path from, boost::filesystem::path to) { boost::filesystem::path ret; boost::filesystem::path::const_iterator itrFrom(from.begin()), itrTo(to.begin()); from = boost::filesystem::absolute(from); to = boost::filesystem::absolute(to); // Find common base for (boost::filesystem::path::const_iterator toEnd(to.end()), fromEnd(from.end()); itrFrom != fromEnd && itrTo != toEnd && *itrFrom == *itrTo; ++itrFrom, ++itrTo) ; // Navigate backwards in directory to reach previously found base for (boost::filesystem::path::const_iterator fromEnd(from.end()); itrFrom != fromEnd; ++itrFrom) { if ((*itrFrom) != ".") ret /= ".."; } // Now navigate down the directory branch ret.append(itrTo, to.end()); return ret; }
// StormLib needs paths with windows style \'s std::string getStormLibPath(const bfs::path& path) { std::string retval = ""; for(bfs::path::iterator it = path.begin(); it != path.end(); ++it) { retval += it->string() + "\\"; } retval = retval.substr(0, retval.size()-1); return retval; }
boost::filesystem::path normalized_path(const boost::filesystem::path& path) { if (path.empty()) return path; fs::path temp; fs::path::iterator start( path.begin() ); fs::path::iterator last( path.end() ); fs::path::iterator stop( last-- ); for ( fs::path::iterator itr( start ); itr != stop; ++itr ) { if (*itr == "." && itr != start && itr != last) continue; // ignore a name and following ".." if (!temp.empty() && *itr == "..") { string lf( temp.filename() ); if (!lf.empty() && lf != "." && lf != "/" # ifdef BOOST_WINDOWS_PATH && !(lf.size() == 2 && lf[1] == ':') # endif && lf != "..") { temp.remove_filename(); continue; } } temp /= *itr; if (itr != last && temp.has_root_path() && fs::is_symlink(temp)) { temp = normalized_path(readlink(temp)); if (temp.filename() == ".") temp = temp.parent_path(); } } if (temp.empty()) temp /= "."; return temp; }
// expands "./my/path.sfc" to "[relativeTo]/my/path.sfc" // if allowHome is true, also expands "~/my/path.sfc" to "/home/pi/my/path.sfc" fs::path resolvePath(const fs::path& path, const fs::path& relativeTo, bool allowHome) { // nothing here if(path.begin() == path.end()) return path; if(*path.begin() == ".") { fs::path ret = relativeTo; for(auto it = ++path.begin(); it != path.end(); ++it) ret /= *it; return ret; } if(allowHome && *path.begin() == "~") { fs::path ret = getHomePath(); for(auto it = ++path.begin(); it != path.end(); ++it) ret /= *it; return ret; } return path; }
/** * https://svn.boost.org/trac/boost/ticket/1976#comment:2 * * "The idea: uncomplete(/foo/new, /foo/bar) => ../new * The use case for this is any time you get a full path (from an open dialog, perhaps) * and want to store a relative path so that the group of files can be moved to a different * directory without breaking the paths. An IDE would be a simple example, so that the * project file could be safely checked out of subversion." * * ALGORITHM: * iterate path and base * compare all elements so far of path and base * whilst they are the same, no write to output * when they change, or one runs out: * write to output, ../ times the number of remaining elements in base * write to output, the remaining elements in path */ boost::filesystem::path naive_uncomplete(boost::filesystem::path const p, boost::filesystem::path const base) { using boost::filesystem::path; using boost::filesystem::dot; using boost::filesystem::slash; if (p == base) return "./"; /*!! this breaks stuff if path is a filename rather than a directory, which it most likely is... but then base shouldn't be a filename so... */ boost::filesystem::path from_path, from_base, output; boost::filesystem::path::iterator path_it = p.begin(), path_end = p.end(); boost::filesystem::path::iterator base_it = base.begin(), base_end = base.end(); // check for emptiness if ((path_it == path_end) || (base_it == base_end)) throw std::runtime_error("path or base was empty; couldn't generate relative path"); #ifdef WIN32 // drive letters are different; don't generate a relative path if (*path_it != *base_it) return p; // now advance past drive letters; relative paths should only go up // to the root of the drive and not past it ++path_it, ++base_it; #endif // Cache system-dependent dot, double-dot and slash strings const String _dot = String(1, dot<path>::value); const String _dots = String(2, dot<path>::value); const String _sep = String(1, slash<path>::value); // iterate over path and base while (true) { // compare all elements so far of path and base to find greatest common root; // when elements of path and base differ, or run out: if ((path_it == path_end) || (base_it == base_end) || (*path_it != *base_it)) { // write to output, ../ times the number of remaining elements in base; // this is how far we've had to come down the tree from base to get to the common root for (; base_it != base_end; ++base_it) { if (*base_it == _dot) continue; else if (*base_it == _sep) continue; output /= "../"; } // write to output, the remaining elements in path; // this is the path relative from the common root boost::filesystem::path::iterator path_it_start = path_it; for (; path_it != path_end; ++path_it) { if (path_it != path_it_start) output /= "/"; if (*path_it == _dot) continue; if (*path_it == _sep) continue; output /= *path_it; } break; } // add directory level to both paths and continue iteration from_path /= path(*path_it); from_base /= path(*base_it); ++path_it, ++base_it; } return output;