boost :: filesystem :: native path的预期形式是什么?

时间:2016-11-10 18:45:29

标签: c++ windows boost boost-filesystem

我进行了Google搜索,了解如何检查给定路径是否有效,最好使用boost。

它把我带到了这里:

How to check if path is valid in boost::filesystem?

大!我对自己说。 然后我在这里谷歌了解提升文档: http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/portability_guide.htm

然后我给自己写了一个测试:

#include <iostream>
#include <sstream>

#include <boost/filesystem.hpp>

int main()
{
    const std::string test1 = "D:\\Programing Projects\\Git Workspace\\Common\\x64\\Debug";
    const std::string test2 = "D:\\Programing Projects\\Git Workspace\\Common\\x64\\Debug\\";
    const std::string test3 = "D:/Programing Projects/Git Workspace/Common/x64/Debug";
    const std::string test4 = "D:/Programing Projects/Git Workspace/Common/x64/Debug/";

    if (!boost::filesystem::native(test1))
    {
        std::cout << "Boost says the following path is not valid for the native operating system: " << test1 << std::endl;
    }

    if (!boost::filesystem::native(test2))
    {
        std::cout << "Boost says the following path is not valid for the native operating system: " << test2 << std::endl;
    }

    if (!boost::filesystem::native(test3))
    {
    std::cout << "Boost says the following path is not valid for the native operating system: " << test3 << std::endl;
    }

    if (!boost::filesystem::native(test4))
    {
        std::cout << "Boost says the following path is not valid for the native operating system: " << test4 << std::endl;

    }

    return 0;
}

测试的输出:

Boost says the following path is not valid for the native operating system: D:\Programing Projects\Git Workspace\Common\x64\Debug
Boost says the following path is not valid for the native operating system: D:\Programing Projects\Git Workspace\Common\x64\Debug\
Boost says the following path is not valid for the native operating system: D:/Programing Projects/Git Workspace/Common/x64/Debug
Boost says the following path is not valid for the native operating system: D:/Programing Projects/Git Workspace/Common/x64/Debug/

该路径有什么问题,它说它对我的原生Windows 10操作系统无效?

1 个答案:

答案 0 :(得分:2)

让我们看一下这个函数的implementation

const char invalid_chars[] =
    "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
    "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
    "<>:\"/\\|";
// note that the terminating '\0' is part of the string - thus the size below
// is sizeof(invalid_chars) rather than sizeof(invalid_chars)-1.  I 
const std::string windows_invalid_chars(invalid_chars, sizeof(invalid_chars));

...

#ifdef BOOST_WINDOWS
    BOOST_FILESYSTEM_DECL bool native(const std::string & name)
    {
      return windows_name(name);
    }
#else
    BOOST_FILESYSTEM_DECL bool native(const std::string & name)
    {
      return  name.size() != 0
        && name[0] != ' '
        && name.find('/') == std::string::npos;
    }
#endif

...

BOOST_FILESYSTEM_DECL bool windows_name(const std::string & name)
{
  return name.size() != 0
    && name[0] != ' '
    && name.find_first_of(windows_invalid_chars) == std::string::npos
    && *(name.end()-1) != ' '
    && (*(name.end()-1) != '.'
      || name.length() == 1 || name == "..");
}

Windows上的要求是:

  • 字符串不为空。
  • 字符串不以空格开头。
  • 字符串不包含任何无效字符。这些是ASCII码0x01 - 0x1F<>:"/,{{1} }和\
  • 字符串不以空格结尾。
  • 该字符串不以|开头,除非整个字符串为."."

否则要求是:

  • 字符串不为空。
  • 字符串不以空格开头。
  • 字符串不包含".."

由于在两种情况下都禁止使用路径分隔符,因此我们可以得出结论,此函数仅用于验证路径的各个组件(即目录名,文件名),而不是完整路径。

documentation证实了这一切:

  

如果/函数的参数作为特定操作或文件系统的目录和常规文件名有效,则返回true。提供了许多这些功能。 ...

在您的示例中,本机将返回name_check等类似

的内容
  • true
  • "Programing Projects"
  • "Git Workspace"
  • "Common"
  • "x64"