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

What is the expected form of boost::filesystem::native path?

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

它把我带到了这里:

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

太棒了!我对自己说。 然后我 Google 在此处更新 boost 文档: 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 操作系统无效?

我们来看看这个函数的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 '[=10=]' 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的要求是:

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

否则要求为:

  • 字符串不为空。
  • 字符串不以 space.
  • 开头
  • 字符串不包含 /.

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

documentation证实了这一切:

A name_check function returns true if its argument is valid as a directory and regular file name for a particular operating or file system. A number of these functions are provided. ...

在您的示例中,本机将 return true 用于

  • "Programing Projects"
  • "Git Workspace"
  • "Common"
  • "x64"
  • "Debug"