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


i did google search see how check if given path valid, preferably using boost.

it brought me here:

how check if path valid in boost::filesystem?

great! myself. google boost doc here: http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/portability_guide.htm

i write myself 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 following path not valid native operating system: " << test1 << std::endl;     }      if (!boost::filesystem::native(test2))     {         std::cout << "boost says following path not valid native operating system: " << test2 << std::endl;     }      if (!boost::filesystem::native(test3))     {     std::cout << "boost says following path not valid native operating system: " << test3 << std::endl;     }      if (!boost::filesystem::native(test4))     {         std::cout << "boost says following path not valid native operating system: " << test4 << std::endl;      }      return 0; } 

the test's output:

boost says following path not valid native operating system: d:\programing projects\git workspace\common\x64\debug boost says following path not valid native operating system: d:\programing projects\git workspace\common\x64\debug\ boost says following path not valid native operating system: d:/programing projects/git workspace/common/x64/debug boost says following path not valid native operating system: d:/programing projects/git workspace/common/x64/debug/ 

what wrong path says not valid native windows 10 operating system?

let's have @ implementation of 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 terminating '\0' part of string - size below // sizeof(invalid_chars) rather sizeof(invalid_chars)-1.   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:

  • string not empty.
  • string not begin space.
  • string not contain invalid characters. ascii codes 0x01 - 0x1f, <, >, :, ", /, \, , |.
  • the string not end space.
  • the string not begin . unless whole string either "." or "..".

otherwise requirements are:

  • string not empty.
  • string not begin space.
  • string not contain /.

since path separator forbidden in both scenarios, can conclude function intended validate individual components of path (i.e. directory names, file names), not full path.

the documentation confirms this:

a name_check function returns true if argument valid directory , regular file name particular operating or file system. number of these functions provided. ...

in example native return true things like

  • "programing projects"
  • "git workspace"
  • "common"
  • "x64"
  • "debug"

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -