19

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?

David Doria
  • 9,873
  • 17
  • 85
  • 147

4 Answers4

14

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
Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
Montes
  • 340
  • 2
  • 8
  • 1
    In case someone is looking for canonical in boost lib: https://www.boost.org/doc/libs/1_66_0/libs/filesystem/doc/reference.html#canonical – dvlper Apr 07 '20 at 15:03
  • 2
    With the caveat that the path must already exist – Neptilo Dec 16 '22 at 11:32
12

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"
Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
Wurmloch
  • 519
  • 3
  • 9
  • @Nacho There we go :) – David Doria Apr 29 '16 at 17:25
  • 2
    Interestingly, this does NOT handle multiple trailing separators. That is, "/home/user//".remove_trailing_separator().parent_path() returns "/home/user" – David Doria Apr 29 '16 at 17:36
  • you could then use `filePath1.lexically_normal().remove_trailing_separator().parent_path()`, but starts looking weird aigain.. – Wurmloch Apr 29 '16 at 17:46
  • Also, an additional check against root paths may be needed before applying `remove_trailing_separator()`. Since `"/"` becomes `""` (empty path) and `"d:/"` becomes `"d:"`, which in some cases is not what is actually needed. – Alex Che Jan 03 '19 at 12:36
  • Actually, the check may be needed anyway, since `path("/").parent_path()` also gives `""`. – Alex Che Jan 03 '19 at 12:51
  • Implementation of `remove_trailing_separator` https://www.boost.org/doc/libs/1_66_0/libs/filesystem/src/path.cpp – Swapnil May 17 '22 at 06:00
3

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.

Nacho
  • 1,104
  • 1
  • 13
  • 30
  • That is still very manual. And if you're on a non-unix system, then you have to check for '\' as well. And you should probably check trailing '//' as well, etc. I was asking if boost::filesystem would had something more like "justHandleItCorrectly(directory)" :) – David Doria Apr 29 '16 at 16:55
  • @DavidDoria Indeed, after deleting the answer I re-considered adding logic to handle the cases you mention, check it out. – Nacho Apr 29 '16 at 17:05
  • Sure, that is still totally valid (but could you say ALWAYS? What about '\\\', etc.), but my point is that this seems to me like a standard thing that `boost::filesystem` is usually good about handling but seems to fail in a non-intuitive way in this functionality. Making everyone handle this on their own seems silly. Usually when this is the case in Boost someone just points to the function that I missed :) – David Doria Apr 29 '16 at 17:07
  • @DavidDoria It is a `while` now, so that cases are implicitly considered. I agree with you. Do you always need to end with `/home` or its just an example? – Nacho Apr 29 '16 at 17:08
  • Sorry, I didn't notice the loop :), but still I'm wondering if anyone knows how to handle this with boost::filesystem directly. /home/... was just an example. – David Doria Apr 29 '16 at 17:18
1

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();
}
aprstar
  • 81
  • 1
  • 5