I have two absolute filesystem paths (A and B), and I want to generate a third filesystem path that represents "A relative from B".
Use case:
boost::filesystem
appears to have complete
to resolve relative ~ relative => absolute
, but nothing to do this in reverse (absolute ~ absolute => relative
).
I want to do it with Boost paths.
The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.
In simple words, an absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.
path. relpath(path) makes a absolute path to relative path. And if the path provided is itself a relative path then the function returns the same path.
With C++17 and its std::filesystem::relative
, which evolved from boost, this is a no-brainer:
#include <filesystem> #include <iostream> namespace fs = std::filesystem; int main() { const fs::path base("/is/the/speed/of/light/absolute"); const fs::path p("/is/the/speed/of/light/absolute/or/is/it/relative/to/the/observer"); const fs::path p2("/little/light/races/in/orbit/of/a/rogue/planet"); std::cout << "Base is base: " << fs::relative(p, base).generic_string() << '\n' << "Base is deeper: " << fs::relative(base, p).generic_string() << '\n' << "Base is orthogonal: " << fs::relative(p2, base).generic_string(); // Omitting exception handling/error code usage for simplicity. }
Output (second parameter is base)
Base is base: or/is/it/relative/to/the/observer Base is deeper: ../../../../../../.. Base is orthogonal: ../../../../../../little/light/races/in/orbit/of/a/rogue/planet
It uses std::filesystem::path::lexically_relative
for comparison. The difference to the pure lexical function is, that std::filesystem::relative
resolves symlinks and normalizes both paths using std::filesystem::weakly_canonical
(which was introduced for relative
) before comparison.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With