如何判断路径是否满足某个父路径?
所以我们有一个像 somepath/**
这样的父级,所以如果有人向我们发送 somepath/myfolder/file
我们会收到 true
并且如果我们会收到 someotherpath/
我们会得到 false
。那么如何判断一条路径是否满足某个父路径呢?
So we have a parent like somepath/**
so if any one sends to us somepath/myfolder/file
we would receive true
and if we would receive someotherpath/
we would get false
. So how to find out if a path meets certain parent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要标准化两个路径,然后进行简单的子字符串比较来查看如果您的相关路径以标准化参考路径开头。
规范化包括将当前工作目录添加到相对路径、大小写规范化(如果您的文件系统不区分大小写)、可能解析符号链接,甚至可能测试硬链接。如果您想允许文件本身成为符号链接,则必须在规范化之前提取路径部分。
我已经在 Linux 中使用 realpath() 函数完成了此操作,即使引用路径包含符号链接,它也能很好地工作。但不知道如何通过提升来做到这一点。
You need to normalize both paths, then you do a simple substring comparison to see if your path in question starts with the normalized reference path.
Normalizing includes adding the current working directory to a relative path, case normalization if your file system is case-insensitive, probably resolving symbolic links and maybe even testing for hard links. If you want to allow the file itself to be a symbolic link you have to extract the path portion prior to normalization.
I've done this in linux using the
realpath()
function and it works very well, even if the reference path contains symlinks. Don't know how to do it with boost, though.