The OpenD Programming Language

absolutePath

Transforms path into an absolute path.

The following algorithm is used:

  1. If path is empty, return null.
  2. If path is already absolute, return it.
  3. Otherwise, append path to base and return the result. If base is not specified, the current working directory is used.

The function allocates memory if and only if it gets to the third stage of this algorithm.

@safe pure
string
absolutePath
(
return scope const string path
,
lazy string base = getcwd()
)

Parameters

path string

the relative path to transform

base string

the base directory of the relative path

Return Value

Type: string

string of transformed path

Throws

Exception if the specified base directory is not absolute.

Examples

version (Posix)
{
    assert(absolutePath("some/file", "/foo/bar")  == "/foo/bar/some/file");
    assert(absolutePath("../file", "/foo/bar")    == "/foo/bar/../file");
    assert(absolutePath("/some/file", "/foo/bar") == "/some/file");
}

version (Windows)
{
    assert(absolutePath(`some\file`, `c:\foo\bar`)    == `c:\foo\bar\some\file`);
    assert(absolutePath(`..\file`, `c:\foo\bar`)      == `c:\foo\bar\..\file`);
    assert(absolutePath(`c:\some\file`, `c:\foo\bar`) == `c:\some\file`);
    assert(absolutePath(`\`, `c:\`)                   == `c:\`);
    assert(absolutePath(`\some\file`, `c:\foo\bar`)   == `c:\some\file`);
}

See Also

asAbsolutePath which does not allocate

Meta