我该如何与QUOT;&正常化QUOT;使用boost ::文件系统上的路径? [英] How do I "normalize" a pathname using boost::filesystem?

查看:210
本文介绍了我该如何与QUOT;&正常化QUOT;使用boost ::文件系统上的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用我们的应用程序的boost ::文件系统。我有一个是通过连接多个路径一起构建了一个'全'路径:

We are using boost::filesystem in our application. I have a 'full' path that is constructed by concatenating several paths together:

#include <boost/filesystem/operations.hpp>
#include <iostream>
     
namespace bf = boost::filesystem;

int main()
{
    bf::path root("c:\\some\\deep\\application\\folder");
    bf::path subdir("..\\configuration\\instance");
    bf::path cfgfile("..\\instance\\myfile.cfg");

    bf::path final ( root / subdir / cfgfile);

    cout << final.file_string();
}

最终路径打印为:

The final path is printed as:

c:\some\deep\application\folder\..\configuration\instance\..\instance\myfile.cfg

这是一个有效的路径,但是当我把它显示给用户我倒是preFER它是的的(注:我没有,如果正常化,甚至肯定的是这个正确的字)。像这样的:

This is a valid path, but when I display it to the user I'd prefer it to be normalized. (Note: I'm not even sure if "normalized" is the correct word for this). Like this:

c:\some\deep\application\configuration\instance\myfile.cfg

升压的早期版本有一个正常化()功能 - 但它似乎已经去precated和删除(没有任何解释)

Earlier versions of Boost had a normalize() function - but it seems to have been deprecated and removed (without any explanation).

有一个原因,我不应该使用 BOOST_FILESYSTEM_NO_DE preCATED 宏碁是否有其他的方式与提高文件系统库做到这一点?或者我应该写code直接操纵路径作为字符串?

Is there a reason I should not use the BOOST_FILESYSTEM_NO_DEPRECATED macro? Is there an alternative way to do this with the Boost Filesystem library? Or should I write code to directly manipulating the path as a string?

推荐答案

以提高1.48之前

在其他的答案提到的,你不能因为正常化的boost ::文件系统不能跟随符号链接。但是,您可以编写正常化尽可能多地(假设。和..通常处理),因为提升提供了确定文件是否是一个符号链接的能力的功能。

As mentioned in other answers, you can't normalise because boost::filesystem can't follow symbolic links. However, you can write a function that normalises "as much as possible" (assuming "." and ".." are treated normally) because boost offers the ability to determine whether or not a file is a symbolic link.

也就是说,如果家长的..是一个符号链接,那么你必须要保留它,否则它可能是安全的砸它,它可能总是安全地移除。

That is to say, if the parent of the ".." is a symbolic link then you have to retain it, otherwise it is probably safe to drop it and it's probably always safe to remove ".".

它类似于操纵的实际字符串,但稍微更优雅。

It's similar to manipulating the actual string, but slightly more elegant.

boost::filesystem::path resolve(
    const boost::filesystem::path& p,
    const boost::filesystem::path& base = boost::filesystem::current_path())
{
    boost::filesystem::path abs_p = boost::filesystem::absolute(p,base);
    boost::filesystem::path result;
    for(boost::filesystem::path::iterator it=abs_p.begin();
        it!=abs_p.end();
        ++it)
    {
        if(*it == "..")
        {
            // /a/b/.. is not necessarily /a if b is a symbolic link
            if(boost::filesystem::is_symlink(result) )
                result /= *it;
            // /a/b/../.. is not /a/b/.. under most circumstances
            // We can end up with ..s in our result because of symbolic links
            else if(result.filename() == "..")
                result /= *it;
            // Otherwise it should be safe to resolve the parent
            else
                result = result.parent_path();
        }
        else if(*it == ".")
        {
            // Ignore
        }
        else
        {
            // Just cat other path entries
            result /= *it;
        }
    }
    return result;
}

提升1.48及以上

您可以使用的boost ::文件系统::规范

path canonical(const path& p, const path& base = current_path());
path canonical(const path& p, system::error_code& ec);
path canonical(const path& p, const path& base, system::error_code& ec);

<一个href=\"http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#canonical\">http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#canonical

1.48及以上还提供了的boost ::文件系统:: read_symlink 功能解析符号链接。

1.48 and above also provide the boost::filesystem::read_symlink function for resolving symbolic links.

这篇关于我该如何与QUOT;&正常化QUOT;使用boost ::文件系统上的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆