Dune Core Modules (2.9.1)
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 More... | |
std::string | Dune::processPath (const std::string &p) |
sanitize a path for further processing More... | |
bool | Dune::pathIndicatesDirectory (const std::string &p) |
check whether the given path indicates that it is a directory More... | |
std::string | Dune::prettyPath (const std::string &p, bool isDirectory) |
pretty print path More... | |
std::string | Dune::prettyPath (const std::string &p) |
pretty print path More... | |
std::string | Dune::relativePath (const std::string &newbase, const std::string &p) |
compute a relative path between two paths More... | |
Detailed Description
Utilities for filesystem path management.
Function Documentation
◆ concatPaths()
std::string Dune::concatPaths | ( | const std::string & | base, |
const std::string & | p | ||
) |
concatenate two paths
- Parameters
-
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.
References Dune::hasSuffix().
Referenced by Dune::VTKWriter< GridView >::getSerialPieceName(), and Dune::VTKWriter< GridView >::pwrite().
◆ pathIndicatesDirectory()
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:
- The empty path (denotes the current directory),
- any path with a trailing '/',
- any path whose last component is "." or "..".
References Dune::hasSuffix().
Referenced by Dune::prettyPath().
◆ prettyPath() [1/2]
std::string Dune::prettyPath | ( | const std::string & | p | ) |
pretty print path
- Parameters
-
p Path to pretty-print.
This is like prettyPath(const std::string& p, bool isDirectory) with isDirectory automatically determined using pathIndicatesDirectory(p).
References Dune::pathIndicatesDirectory(), and Dune::prettyPath().
◆ prettyPath() [2/2]
std::string Dune::prettyPath | ( | const std::string & | p, |
bool | isDirectory | ||
) |
pretty print path
- Parameters
-
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" |
References Dune::hasSuffix(), and Dune::processPath().
Referenced by Dune::prettyPath().
◆ processPath()
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:
- The path is a series of components, each followed by a single '/'.
- An absolute path starts with an empty component followed by a '/', so its first character will be '/'. This is the only case where an empty component can occur.
- The path does not contain any component ".". Any such component in the input is removed.
- A ".." component may only occur in the following case: A relative path may contain a series of ".." in the beginning. Any other occurrences of ".." in the input is collapsed with a preceding component or simply removed if it is at the beginning of an absolute path.
- Note
- The result is really meant for processing only since it has two unusual properties: First, any path denoting the current directory in the input, such as "." will result in an empty path "". Second, any non-empty result path will have a trailing '/'. For other uses, prettyPath() may be more appropriate.
Some examples:
p | result |
---|---|
"" | "" |
"." | "" |
"./" | "" |
"a/.." | "" |
".." | "../" |
"../a" | "../a/" |
"a" | "a/" |
"a//" | "a/" |
"a///b" | "a/b/" |
"/" | "/" |
"/." | "/" |
"/.." | "/" |
"/a/.." | "/" |
"/a" | "/a/" |
"/a/" | "/a/" |
"/../a/" | "/a/" |
References Dune::hasPrefix().
Referenced by Dune::prettyPath(), and Dune::relativePath().
◆ relativePath()
std::string Dune::relativePath | ( | const std::string & | newbase, |
const std::string & | p | ||
) |
compute a relative path between two paths
- Parameters
-
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().
- Exceptions
-
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.
References DUNE_THROW, Dune::hasPrefix(), and Dune::processPath().
Referenced by Dune::VTKWriter< GridView >::pwrite().