简体   繁体   English

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

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

I did a Google search to see how to check if a given path is valid, preferably using boost. 我进行了一次Google搜索,以查看如何检查给定路径是否有效,最好使用boost方法。

It brought me here: 它把我带到这里:

How to check if path is valid in boost::filesystem? 如何检查boost :: filesystem中的路径是否有效?

Great! 大! I say to myself. 我对自己说。 I then Google up the boost doc here: http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/portability_guide.htm 然后,我在这里使用Google增强文档: http : //www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/portability_guide.htm

I then write myself a test: 然后我给自己写一个测试:

#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;
}

The Test's Output: 测试的输出:

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/

What is wrong with that path that it says it is not valid for my native Windows 10 operating system? 它对我的本机Windows 10操作系统无效的路径有什么问题?

Let's have a look at the implementation of this function. 让我们看一下该函数的实现

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 == "..");
}

The requirements on Windows are: Windows上的要求是:

  • String is not empty. 字符串不为空。
  • String does not begin with a space. 字符串不能以空格开头。
  • String does not contain any invalid characters. 字符串不包含任何无效字符。 Those are ASCII codes 0x01 - 0x1F , < , > , : , " , / , \\ , and | . 这些是ASCII码0x01 - 0x1F<>:"/\\ ,和|
  • The string does not end with a space. 字符串不以空格结尾。
  • The string does not begin with a . 字符串不以开头. UNLESS the whole string is either "." 除非整个字符串是"." or ".." . ".."

Otherwise the requirements are: 否则要求是:

  • String is not empty. 字符串不为空。
  • String does not begin with a space. 字符串不能以空格开头。
  • String does not contain / . 字符串不包含/

Since the path separator is forbidden in both scenarios, we can conclude that this function is only intended to validate the individual components of the path (ie directory names, file names), not the full path. 由于在两种情况下都禁止使用路径分隔符,因此可以得出结论,此功能仅用于验证路径的各个组成部分(即目录名,文件名),而不是完整路径。

The documentation confirms all this: 文档确认了以下所有内容:

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. 如果name_check函数的参数作为特定操作系统或文件系统的目录和常规文件名有效,则它返回true。 A number of these functions are provided. 提供了许多这些功能。 ... ...

In your example native would return true for things like 在你的榜样本地将返回true喜欢的事情

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM