Opened 11 years ago
Closed 7 years ago
#5897 closed Feature Requests (fixed)
Make path relative function.
Reported by: | Owned by: | Beman Dawes | |
---|---|---|---|
Milestone: | Boost 1.60.0 | Component: | filesystem |
Version: | Boost 1.47.0 | Severity: | Problem |
Keywords: | Cc: |
Description
It is often useful to take a path that is absolute and make it relative to some other path. For example, if you're copying from directory A to directory B using a recursive_directory_iterator, it is often the case that you will do the following:
for_each(bfs::recursive_directory_iterator(directoryA), bfs::recursive_directory_iterator(), [=](const bfs::directory_entry &entry) { if(bfs::is_file(entry.status())) { bfs::path dstPath = directoryB / make_relative(directoryA, entry.path()); bfs::copy_file(entry.path(), dstPath); } });
The make_relative
function would iterate through the path components of the two arguments, removing entries until it found different ones. Then, it would add ".." to the beginning of the second path argument, until it ran out of directories in the first path. What you are left with is the second path relative to the first one.
Obviously, this would not work if both paths are absolute. But this is such a common operation that there has to be some way to make it work.
Change History (13)
comment:1 by , 11 years ago
comment:2 by , 11 years ago
I've added an example for 1.46.1 ( probably works for trunk as well, haven't tried ). For details take a look at https://svn.boost.org/trac/boost/ticket/6249 .
comment:3 by , 9 years ago
Please consider this feature, it is required rather often - http://stackoverflow.com/questions/10167382/boostfilesystem-get-relative-path
comment:4 by , 9 years ago
This would be really helpful, I'd consider it one of the basic functions you want to do with paths.
comment:11 by , 7 years ago
Could a contributor at least comment on this? This at least seems like a "must-have" feature for any utility file system API. 4 years is a pretty long time for a simple feature request to not be acknowledged.
comment:12 by , 7 years ago
Have you not seen the other 2692 active tickets they're equally busy ignoring? ;)
comment:13 by , 7 years ago
Milestone: | To Be Determined → Boost 1.60.0 |
---|---|
Resolution: | → fixed |
Status: | new → closed |
Boost 1.60.0 will add functions lexically_normal, lexically_relative, relative, and weakly_canonical. These are designed to provide the functionality requested by this ticket.
See http://boostorg.github.io/filesystem/relative_proposal.html for more information. It proposes additional lexical and operational proximate functions, but I have chosen not to include those now. They will be reconsidered once users get some field experience with the core functionality for computing relative paths.
These function are available now on the https://github.com/boostorg/filesystem develop branch, and will be merged to master as soon as sufficient regression tests have cycled. They will also be proposed for inclusion in the next version of the C++ committee's TS 18822, File System Technical Specification.
Many thanks to Jamie Allsop for his help and perseverance, and to everyone else who made suggestions and comments. Jamie's paper is available at https://github.com/ja11sop/std-filesystem-relative
Thanks,
--Beman
Great idea! I was looking for that exact function just today.