Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parent_path() with or without trailing slash

As explained in the documentation, the expected output of the following is:

boost::filesystem::path filePath1 = "/home/user/";
cout << filePath1.parent_path() << endl; // outputs "/home/user"

boost::filesystem::path filePath2 = "/home/user";
cout << filePath2.parent_path() << endl; // outputs "/home"

The question is, how do you deal with this? That is, if I accept a path as an argument, I don't want the user to care whether or not it should have a trailing slash. It seems like the easiest thing to do would be to append a trailing slash, then call parent_path() TWICE to get the parent path of "/home" that I want:

boost::filesystem::path filePath1 = "/home/user/";
filePath1 /= "/";
cout << filePath1.parent_path().parent_path() << endl; // outputs "/home"

boost::filesystem::path filePath2 = "/home/user";
filePath2 /= "/";
cout << filePath2.parent_path().parent_path() << endl; // outputs "/home"

but that just seems ridiculous. Is there a better way to handle this within the framework?

like image 919
David Doria Avatar asked Apr 29 '16 15:04

David Doria


4 Answers

There is a (undocumented?) member function: path& path::remove_trailing_separator();

I tried this and it worked for me on Windows using boost 1.60.0:

boost::filesystem::path filePath1 = "/home/user/";
cout << filePath1.parent_path() << endl; // outputs "/home/user"
cout << filePath1.remove_trailing_separator().parent_path() << endl; // outputs "/home"

boost::filesystem::path filePath2 = "/home/user";
cout << filePath2.parent_path() << endl; // outputs "/home"
cout << filePath2.remove_trailing_separator().parent_path() << endl; // outputs "/home"
like image 54
Wurmloch Avatar answered Nov 18 '22 11:11

Wurmloch


You can use std::filesystem::canonical with C++17:

namespace fs = std::filesystem;

fs::path tmp = "c:\\temp\\";

tmp = fs::canonical(tmp); // will remove slash

fs::path dir_name = tmp.filename(); // will get temp
like image 24
Montes Avatar answered Nov 18 '22 12:11

Montes


To remove the trailing separator from a path that is to a directory, so far this is working for me:

/**
 * Creates lexically normal (removes extra path separators and dots) directory
 * path without trailing path separator slash(es)
 * @param dir_path - directory path to normalize
 */
void normalize_dir_path(boost::filesystem::path& dir_path) {
    // @HACK - append non-existing file to path so that we may later resolve
    // normalized directory path using parent_path()
    dir_path /= "FILE.TXT";
    // Remove unneeded dots and slashes
    dir_path = dir_path.lexically_normal();
    // Remove trailing slash from original path!
    dir_path = dir_path.parent_path();
}

The above answer is similar to OP's original posted workaround (add '/') in combination with Wurmloch's comment about using lexically_normal(). One advantage is that only the documented methods from boost::filesystem are used. One possible disadvantage is that caller must be confident the input argument dir_path is intended to be a directory and not a regular file.

Using the normalize_dir_path(...) method to answer OP's question:

boost::filesystem::path filePath1 = "/home/user/";
normalize_dir_path(filePath1); // filePath1 is now "/home/user"
cout << filePath1.parent_path() << endl; // outputs "/home"

boost::filesystem::path filePath2 = "/home/user";
normalize_dir_path(filePath2); // filePath2 is now "/home/user"
cout << filePath2.parent_path() << endl; // outputs "/home"

boost::filesystem::path filePath3 = "/home/user/.";
normalize_dir_path(filePath3); // filePath3 is now "/home/user"
cout << filePath3.parent_path() << endl; // outputs "/home"

Update

Just realized that boost::filesystem::path::lexically_normal() is only available on BOOST version >= 1_60_0. For earlier versions, there appears to be a deprecated function available by default boost::filesystem::path::normalize() (as long as BOOST_FILESYSTEM_NO_DEPRECATED is not defined). So, my current normalize directory path method is along the lines:

#include <boost/version.hpp>

void normalize_dir_path(boost::filesystem::path& dir_path) {
    // @HACK - append non-existing file to path so that we may later resolve
    // normalized directory path using parent_path()
    dir_path /= "FILE.TXT";
    // Remove unneeded dots and slashes
#if BOOST_VERSION >= 106000
    dir_path = dir_path.lexically_normal();
#else
    dir_path.normalize();
#endif
    // Remove trailing slash from original path!
    dir_path = dir_path.parent_path();
}
like image 1
aprstar Avatar answered Nov 18 '22 12:11

aprstar


Seems like it, although I would recommend doing a previous manipulation with the directory string instead of calling twice to parent_path():

std::string directory = "/home/user"; // Try with "/home/user/" too, result is the same

while ((directory.back() == '/') || (directory.back() == '\\')))
    directory.erase(directory.size()-1);    

boost::filesystem::path filePath(directory);
std::cout << filePath.parent_path() << std::endl; // outputs "/home" 

It is important to note that std::string::back() is a C++11 feature. Should you need to compile with a previous version you will have to change the algorithm a bit.

like image 3
Nacho Avatar answered Nov 18 '22 12:11

Nacho