从两个绝对路径获取相对路径
我有两个绝对文件系统路径(A 和 B),并且我想生成第三个文件系统路径来表示“A 相对于 B”。
用例:
- 管理播放列表的媒体播放器。
- 用户将文件添加到播放列表。
- 添加到播放列表的新文件路径相对于播放列表路径。
- 将来,整个音乐目录(包括播放列表)都会转移到其他地方。
- 所有路径仍然有效,因为它们是相对于播放列表的。
boost::filesystem
似乎已完整
来解析relative ~relative =>绝对
,但没有任何相反的操作(绝对〜绝对=>相对
)。
我想用Boost路径来做到这一点。
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:
- Media player managing a playlist.
- User adds file to playlist.
- New file path added to playlist relative to playlist path.
- In the future, entire music directory (including playlist) moved elsewhere.
- All paths still valid because they are relative to the playlist.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用 C++17 及其
std::filesystem::relative
,从boost演变而来,这是一个显而易见的:输出(第二个参数是base)
它使用
std::filesystem::path::lexically_relative
进行比较。与纯词法函数的区别在于,std::filesystem::relative 解析符号链接并使用
std::filesystem::weakly_canonical
(这是在比较之前引入relative
)。With C++17 and its
std::filesystem::relative
, which evolved from boost, this is a no-brainer:Output (second parameter is base)
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 usingstd::filesystem::weakly_canonical
(which was introduced forrelative
) before comparison.自版本 1.60.0 boost 起。文件系统确实支持这一点。您正在寻找成员函数
path lexically_relative(const path& p) const
。1.60.0 之前的原始答案如下。
Boost不支持这个;这是一个悬而未决的问题 - #1976(完整的逆函数) - 尽管如此似乎没有得到太多关注。
这是一个看似幼稚的解决方法,似乎可以解决问题(不确定是否可以改进):
As of version 1.60.0 boost.filesystem does support this. You're looking for the member function
path lexically_relative(const path& p) const
.Original, pre-1.60.0 answer below.
Boost doesn't support this; it's an open issue — #1976 (Inverse function for complete) — that nevertheless doesn't seem to be getting much traction.
Here's a vaguely naive workaround that seems to do the trick (not sure whether it can be improved):
我刚刚编写了可以将绝对路径转换为相对路径的代码。它适用于我的所有用例,但我不能保证它是完美的。
为了便于阅读,我将 boost::filesystem 缩写为“fs”。在函数定义中,您可以使用 fs::path::current_path() 作为“relative_to”的默认值。
I just wrote code that can translate an absolute path to a relative path. It works in all my use cases, but I can not guarantee it is flawless.
I have abreviated boost::filesystem to 'fs' for readability. In the function definition, you can use fs::path::current_path() as a default value for 'relative_to'.
我只是考虑使用
boost::filesystem
来完成相同的任务,但是 - 由于我的应用程序同时使用 Qt 和 Boost 库,我决定使用 Qt,它通过一种简单的方法来完成此任务 QString QDir::relativeFilePath( const QString & fileName ):它的工作原理就像魅力让我节省了几个小时的时间。
I was just thinking about using
boost::filesystem
for the same task, but - since my application uses both Qt and Boost libraries, I decided to use Qt which does this task with one simple method QString QDir::relativeFilePath( const QString & fileName ):It works like a charm and saved me a few hours of my life.
以下是我在基于 boost 文件系统构建的库中执行此操作的方法:
步骤 1:确定“最深的公共根”。基本上,它就像两条路径的最大公约数。例如,如果您的 2 个路径是“C:\a\b\c\d”和“C:\a\b\c\l.txt”,那么它们共享的公共根是“C:\a \公元前\”。
要实现这一点,请将两个路径转换为绝对形式(而不是规范形式)(您将希望能够对推测路径和符号链接执行此操作)。
步骤 2:要从 A 到 B,请在 A 后添加足够的“../”副本,以将目录树向上移动到公共根,然后添加 B 的字符串以沿着树向下移动到该目录。在 Windows 上,您可以有 2 条没有公共根的路径,因此从任何 A 到任何 B 并不总是可能的。
这
将执行您想要的操作 - 您从 A 上升,直到到达它和 B 都是其后代的公共文件夹,然后下降到 B。您可能不需要我拥有的“commonDirsInOrder”返回,但是“routeFrom1To2” return 就是您要的那个。
如果您打算实际将工作目录更改为“B”,您可以直接使用“routeFrom1To2”。请注意,尽管有所有“..”部分,此函数仍将生成绝对路径,但这应该不是问题。
Here's how I do it in the library I build on top of boost filesystem:
Step 1: Determine "deepest common root". Basically, its like the greatest common denominator for 2 paths. For example, if you're 2 paths are "C:\a\b\c\d" and "C:\a\b\c\l.txt" then the common root they both share is "C:\a\b\c\".
To get this, convert both paths into absolute- NOT canonical- form (you'll want to be able to do this for speculative paths & symlinks).
Step 2: To go from A to B, you suffix A with enough copies of "../" to shift up the directory tree to the common root, then add the string for B to travel down the tree to it. On windows you can have 2 paths with no common root, so going from any A to any B is not always possible.
}
This will do what you want- you go up from A until you hit the common folder it and B are both descendants of, then go down to B. You probably don't need the "commonDirsInOrder" return that I have, but the "routeFrom1To2" return IS the one you're asking for.
If you plan to actually change the working directory to "B" you can use "routeFrom1To2" directly. Be aware that this function will produce an absolute path despite all the ".." parts, but that shouldn't be a problem.
我为这个技巧写下了一个简单的解决方案。
boost库没有使用,只有STL的std::string,std::vector。
Win32平台已经过测试。
只需调用:
并且,它将返回从
PathA
到PathB
的相对路径。例子:
I have write down one simple solution for this trick.
There's no usage on boost libraries, only STL's
std::string
,std::vector
.The Win32 platform has been tested.
Just calling:
And, it would return relative path from
PathA
toPathB
.Example:
我需要在没有 Boost 的情况下执行此操作,而其他基于 std 的解决方案无法为我执行此操作,因此我重新实现了它。当我正在研究这个时,我意识到我以前也做过......
无论如何,它不像其他一些那样完整,但可能对人们有用。它是 Windows 特定的;使其 POSIX 的更改涉及目录分隔符和字符串比较中的区分大小写。
在我实现并工作后不久,我必须将周围的功能转移到Python,所以所有这些都归结为
os.path.relpath(to, from)
。I needed to do this without Boost and the other std based solution didn't do it for me so I reimplemented it. As I was working on this I realized that I'd done it before too...
Anyway, it's not as complete as some of the others but might be useful to people. It's Windows-specific; changes to make it POSIX involve directory separator and case sensitivity in the string compare.
Shortly after I got this implemented and working I had to transfer the surrounding functionality to Python so all of this just boiled down to
os.path.relpath(to, from)
.