Custom path extension with boost :: filesystem

Is there a function in boost::filesystem for extending paths starting with the character of the user's home directory ( ~ on Unix), similar to os.path.expanduser in Python?

+5
source share
1 answer

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, look at a similar question suggested by @WhiteViking.

+4
source

Source: https://habr.com/ru/post/1234064/


All Articles