Loading...
Searching...
No Matches
Filesystem Paths
Common » Utilities

Utilities for filesystem path management. More...

Collaboration diagram for Filesystem Paths:

## 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

## 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.

## 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 "..".

## 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).

## 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"

## 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/"

## 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.