Create a relative path using boost

I am trying to create relative paths using boost.

My initial plan:

string base_directory; // input boost::filesystem::path base_path; string other_directory; // input boost::filesystem::path other_path; // assume base_path is absolute - did that already (using complete() // if path is relative, to root it in the current directory) -> base_directory = base_path.string(); if (other_path.empty()) other_directory = base_directory; else { other_path = boost::filesystem::path(other_directory); if(!other_path.is_complete()) { other_path = base_path / other_path; other_directory = other_path.string(); } if(!boost::filesystem::exists(boost::filesystem::path(other_path))) { boost::filesystem::create_directory(other_path); } } 

This works fine if other_directory is an absolute or just a name (or relative internal in base_directory).

But if I try to put ".." or "../other", I end up creating weird constructs like "c: \ test .." or "c: \ test .. \ other"

How can I properly create relative paths, preferably using boost? I tried looking in the documentation ... without positive success.

I use Windows (my preference for boost is that it should be multi-platform and I'm already dependent on it)

Edit: I increased 1.47

Thanks for any suggestion.

+4
source share
1 answer

The boost file system does not know if "C:\test" refers to a file or directory, so it does not assume that the correct "\" correct.

If you add this "\" , you can use the boost::filesystem::canonical() function to simplify the removal path . and .. elements.

 other_path = boost::filesystem::path( other_directory + "\" );    if(!other_path.is_complete()) { other_path = boost::filesystem::canonical( base_path / other_path ); 
+1
source

All Articles