2016-11-10 93 views
0

我做了一次Google搜索,看看如何檢查給定路徑是否有效,最好使用boost。boost :: filesystem :: native路徑的預期形式是什麼?

它把我帶到這裏:

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

太好了!我對自己說。 我那麼谷歌了升壓DOC這裏: 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操作系統無效?

+0

這裏「有效」是什麼意思? –

+0

@Nicol Bolas對我有效意味着路徑不一定必須指向一個文件或目錄,但是它可以用於指向一個文件或目錄,一旦它被創建。無論如何,這正是我正在尋找的。基本上,拒絕像「%&?!」這樣的東西根本不像%^的路徑!「 –

+0

字符串'test2'是否故意包含'\ P'而不是'\\ P'? –

回答

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,<,>,:,", /,\|
  • 該字符串不以空格結尾。
  • 該字符串不以.開頭,除非整個字符串是"."".."

否則要求是:

  • 字符串不是空的。
  • 字符串不以空格開頭。
  • 字符串不包含/

由於兩種場景都禁止使用路徑分隔符,因此我們可以得出結論,此功能僅用於驗證路徑的各個組件(即目錄名稱,文件名),而不是完整路徑。

documentation證實了這一切:如果它的參數是爲特定操作系統或文件系統的目錄和普通文件名有效

一個name_check函數返回true。提供了許多這些功能。 ...

在你的榜樣本地將返回true喜歡的事情

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