dune-common 2.9.0
|
Utilities for filesystem path management. More...
Files | |
file | path.cc |
Utilities for handling filesystem paths. | |
file | path.hh |
Utilities for handling filesystem paths. | |
Functions | |
std::string | Dune::concatPaths (const std::string &base, const std::string &p) |
concatenate two paths | |
std::string | Dune::processPath (const std::string &p) |
sanitize a path for further processing | |
bool | Dune::pathIndicatesDirectory (const std::string &p) |
check whether the given path indicates that it is a directory | |
std::string | Dune::prettyPath (const std::string &p, bool isDirectory) |
pretty print path | |
std::string | Dune::prettyPath (const std::string &p) |
pretty print path | |
std::string | Dune::relativePath (const std::string &newbase, const std::string &p) |
compute a relative path between two paths | |
Utilities for filesystem path management.
std::string Dune::concatPaths | ( | const std::string & | base, |
const std::string & | p | ||
) |
concatenate two paths
base | The base path. |
p | The path to concatenate onto base. |
If p is an absolute path, return p. Otherwise return the string-concatenation of base and path, possibly with a '/' in between, if necessary.
Some examples:
base | p | result |
---|---|---|
anything | "/abs/path" | "/abs/path" |
"a" | "b" | "a/b" |
"/a" | "b" | "/a/b" |
"a/" | "b" | "a/b" |
"a" | "b/" | "a/b/" |
".." | "b" | "../b" |
"a" | ".." | "a/.." |
"." | "b" | "./b" |
"a" | "." | "a/." |
"" | "b" | "b" |
"a" | "" | "a" |
"" | "" | "" |
If both base and p are sanitized as per processPath(), and if p does not contain any leading "../", then the result will also be sanitized.
bool Dune::pathIndicatesDirectory | ( | const std::string & | p | ) |
check whether the given path indicates that it is a directory
In particular the following kinds of paths indicate a directory:
std::string Dune::prettyPath | ( | const std::string & | p | ) |
pretty print path
p | Path to pretty-print. |
This is like prettyPath(const std::string& p, bool isDirectory) with isDirectory automatically determined using pathIndicatesDirectory(p).
std::string Dune::prettyPath | ( | const std::string & | p, |
bool | isDirectory | ||
) |
pretty print path
p | Path to pretty-print. |
isDirectory | Whether to append a '/' to make clear this is a directory. |
Pretty print the path. This removes any duplicate '/' and any superfluous occurrences of ".." and ".". The resulting path will have a trailing '/' if it is the root path or if isDirectory is true. It will however not have a trailing '/' if it is otherwise clear that it is a directory – i.e. if its last component is "." or "..".
Some examples:
p | isDirectory | result |
---|---|---|
"" | anything | "." |
"." | anything | "." |
"./" | anything | "." |
"a/.." | anything | "." |
".." | anything | ".." |
"../a" | true | "../a/" |
"../a" | false | "../a" |
"a" | true | "a/" |
"a" | false | "a" |
"a//" | true | "a/" |
"a//" | false | "a" |
"a///b" | true | "a/b/" |
"a///b" | false | "a/b" |
"/" | anything | "/" |
"/." | anything | "/" |
"/.." | anything | "/" |
"/a/.." | anything | "/" |
"/a" | true | "/a/" |
"/a" | false | "/a" |
"/a/" | true | "/a/" |
"/a/" | false | "/a" |
"/../a/" | true | "/a/" |
"/../a/" | false | "/a" |
std::string Dune::processPath | ( | const std::string & | p | ) |
sanitize a path for further processing
Sanitize the path as far as possible to make further processing easier. The resulting path has the following properties:
Some examples:
p | result |
---|---|
"" | "" |
"." | "" |
"./" | "" |
"a/.." | "" |
".." | "../" |
"../a" | "../a/" |
"a" | "a/" |
"a//" | "a/" |
"a///b" | "a/b/" |
"/" | "/" |
"/." | "/" |
"/.." | "/" |
"/a/.." | "/" |
"/a" | "/a/" |
"/a/" | "/a/" |
"/../a/" | "/a/" |
std::string Dune::relativePath | ( | const std::string & | newbase, |
const std::string & | p | ||
) |
compute a relative path between two paths
newbase | Base path for the resulting relative path. |
p | Path re sulting path should resolve to, when taken reltively to newbase. |
Compute a relative path from newbase to p. newbase is assumed to be a directory. p and newbase should either both be absolute, or both be relative. In the latter case they are assumed to both be relative to the same unspecified directory. The has the form of something sanitized by processPath().
NotImplemented | The condition that newbase and p must both be relative or both be absolute does not hold. |
NotImplemented | After sanitization newbase has more leading ".." components than p. |