void checkBeginEndBasic() { using namespace fs; using It = path::iterator; { path const p; ASSERT_SAME_TYPE(It, decltype(p.begin())); ASSERT_SAME_TYPE(It, decltype(p.end())); assert(p.begin() == p.end()); } { path const p("foo"); It default_constructed; default_constructed = p.begin(); assert(default_constructed == p.begin()); assert(default_constructed != p.end()); default_constructed = p.end(); assert(default_constructed == p.end()); assert(default_constructed != p.begin()); } { path p("//root_name//first_dir////second_dir"); const path expect[] = {"/", "root_name", "first_dir", "second_dir"}; assert(checkCollectionsEqual(p.begin(), p.end(), std::begin(expect), std::end(expect))); assert(checkCollectionsEqualBackwards(p.begin(), p.end(), std::begin(expect), std::end(expect))); } { path p("////foo/bar/baz///"); const path expect[] = {"/", "foo", "bar", "baz", ""}; assert(checkCollectionsEqual(p.begin(), p.end(), std::begin(expect), std::end(expect))); assert(checkCollectionsEqualBackwards(p.begin(), p.end(), std::begin(expect), std::end(expect))); } }
path Attributes::resolve(path name) { Attribute *a = _search_attr(attrs, name.generic_string()); if (a && a->link().size() == 0) return name; path partial; for(auto it=name.begin();it!=name.end();++it) { partial /= *it; a = _search_attr(attrs, partial.generic_string()); if (a) { if (a->link().size()) { path rest(a->link()); ++it; //everything matched - this is a link! if (it == name.end()) return a->link(); for(;it!=name.end();++it) rest /= *it; return resolve(rest); } else //name is no valid path! return name; } } return name; }
void checkIteratorConcepts() { using namespace fs; using It = path::iterator; using Traits = std::iterator_traits<It>; ASSERT_SAME_TYPE(Traits::iterator_category, std::bidirectional_iterator_tag); ASSERT_SAME_TYPE(Traits::value_type, path); ASSERT_SAME_TYPE(Traits::pointer, path const*); ASSERT_SAME_TYPE(Traits::reference, path const&); { It it; ASSERT_SAME_TYPE(It&, decltype(++it)); ASSERT_SAME_TYPE(It, decltype(it++)); ASSERT_SAME_TYPE(It&, decltype(--it)); ASSERT_SAME_TYPE(It, decltype(it--)); ASSERT_SAME_TYPE(Traits::reference, decltype(*it)); ASSERT_SAME_TYPE(Traits::pointer, decltype(it.operator->())); ASSERT_SAME_TYPE(std::string const&, decltype(it->native())); ASSERT_SAME_TYPE(bool, decltype(it == it)); ASSERT_SAME_TYPE(bool, decltype(it != it)); } { path const p; ASSERT_SAME_TYPE(It, decltype(p.begin())); ASSERT_SAME_TYPE(It, decltype(p.end())); assert(p.begin() == p.end()); } }
void remove_dir(path& p) { assert(p.begin() != p.end()); path tmp; for (path::iterator i = boost::next(p.begin()); i != p.end(); ++i) tmp /= *i; p = tmp; }
path path::lexically_relative(const path& base) const { std::pair<path::iterator, path::iterator> mm = detail::mismatch(begin(), end(), base.begin(), base.end()); if (mm.first == begin() && mm.second == base.begin()) return path(); if (mm.first == end() && mm.second == base.end()) return detail::dot_path(); path tmp; for (; mm.second != base.end(); ++mm.second) tmp /= detail::dot_dot_path(); for (; mm.first != end(); ++mm.first) tmp /= *mm.first; return tmp; }
F findPathFlow(const path& p) const { F minFlow = resCap(*p.begin(), *(p.begin() + 1)); for (typename path::const_iterator v = p.begin(); v != p.end() - 1; ++v) { minFlow = std::min(minFlow, resCap(*v, *(v + 1))); } return minFlow; }
path decapitate(const path& p) { path ret; for (auto it = p.begin(); it != p.end(); ++it) if (it != p.begin()) ret /= *it; return ret; }
path::iterator Relative(const path &testPath, const path &subPath) { path::iterator tIter = testPath.begin(); path::iterator sIter = subPath.begin(); for (;;) { if (sIter == subPath.end()) { return tIter; } else if (tIter == testPath.end()) { // testpath is shorter return testPath.end(); } else if (*tIter != *sIter) { return testPath.end(); } ++tIter; ++sIter; } }
path NaiveUncomplete ( const path file, const path base ) { if ( file.has_root_path() ) { if ( file.root_path() != base.root_path() ) { return file; } else { return NaiveUncomplete ( file.relative_path(), base.relative_path() ); } } else { if ( file.has_root_path() ) { throw "cannot uncomplete a path relative path from a rooted base"; } else { typedef path::const_iterator path_iterator; path_iterator path_it = file.begin(); path_iterator base_it = base.begin(); while ( path_it != file.end() && base_it != base.end() ) { if ( *path_it != *base_it ) { break; } ++path_it; ++base_it; } path result; for ( ; base_it != base.end(); ++base_it ) { result /= ".."; } for ( ; path_it != file.end(); ++path_it ) { result /= *path_it; } return result; } } } // NaiveUncomplete
inline path normalize (path const& p) { path result; for (path::iterator i (p.begin ()), e (p.end ()); i != e; ++i) { if (*i != ".") result /= path (*i, native); } return result; }
// Return path when appended to parent will resolve to same as child path relativize(path parent, path child){ parent = canonical(parent); child = canonical(child); // Find common base path::const_iterator parent_iter(parent.begin()), child_iter(child.begin()); path::const_iterator child_end(child.end()), parent_end(parent.end()); while(parent_iter != parent_end && child_iter != child_end && *parent_iter == *child_iter) ++parent_iter, ++child_iter; // Navigate backwards in directory to reach previously found base path ret; while(parent_iter != parent_end) { if( (*parent_iter) != "." ) ret /= ".."; ++parent_iter; } // Now navigate down the directory branch ret.append(child_iter, child.end()); return ret; }
path remove_prefix(const path& pth, const path& prefix) { path::const_iterator it; if (!skipPrefix(pth, prefix, it)) { throw filesystem_error( "Path does not start with prefix", pth, prefix, bsys::errc::make_error_code(bsys::errc::invalid_argument)); } path p; for (; it != pth.end(); ++it) { p /= *it; } return p; }
path CUtils::expandvars(path p) { path rp; path::iterator it = p.begin(); while (it != p.end()) { cerr << *it << endl; string elem(it->string()); if(elem.compare("~") == 0) { elem = string(getenv("HOME")); } if(elem.find_first_of('$') == 0) { const char* envvar = getenv( elem.substr(1).c_str() ); if(envvar != NULL) { elem = envvar; } } rp /= elem; cerr << " " << rp << endl; ++it; } return rp; }
void pushFlowThrowPath(const path& p, const F& pFlow) { for (typename path::const_iterator v = p.begin(); v != p.end() - 1; ++v) { flows.at(*v).at(*(v + 1)) += pFlow; flows.at(*(v + 1)).at(*v) -= pFlow; } }
path lexically_normal(const path& p) { if (p.empty()) return p; path temp; path::iterator start(p.begin()); path::iterator last(p.end()); path::iterator stop(last--); for (path::iterator itr(start); itr != stop; ++itr) { // ignore "." except at start and last if (itr->native().size() == 1 && (itr->native())[0] == dot && itr != start && itr != last) continue; // ignore a name and following ".." if (!temp.empty() && itr->native().size() == 2 && (itr->native())[0] == dot && (itr->native())[1] == dot) // dot dot { string_type lf(temp.filename().native()); if (lf.size() > 0 && (lf.size() != 1 || (lf[0] != dot && lf[0] != separator)) && (lf.size() != 2 || (lf[0] != dot && lf[1] != dot # ifdef BOOST_WINDOWS_API && lf[1] != colon # endif ) ) ) { temp.remove_filename(); //// if not root directory, must also remove "/" if any //if (temp.native().size() > 0 // && temp.native()[temp.native().size()-1] // == separator) //{ // string_type::size_type rds( // root_directory_start(temp.native(), temp.native().size())); // if (rds == string_type::npos // || rds != temp.native().size()-1) // { // temp.m_pathname.erase(temp.native().size()-1); // } //} path::iterator next(itr); if (temp.empty() && ++next != stop && next == last && *last == detail::dot_path()) { temp /= detail::dot_path(); } continue; } } temp /= *itr; }; if (temp.empty()) temp /= detail::dot_path(); return temp; }