Is there functionality in boost::filesystem
to expand paths that begin with a user home directory symbol (~
on Unix), similar to the os.path.expanduser function provided in Python?
No.
But you can implement it by doing something like this:
namespace bfs = boost::filesystem;
using std;
bfs::path expand (bfs::path in) {
if (in.size () < 1) return in;
const char * home = getenv ("HOME");
if (home == NULL) {
cerr << "error: HOME variable not set." << endl;
throw std::invalid_argument ("error: HOME environment variable not set.");
}
string s = in.c_str ();
if (s[0] == '~') {
s = string(home) + s.substr (1, s.size () - 1);
return bfs::path (s);
} else {
return in;
}
}
Also, have a look at the similar question suggested by @WhiteViking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With