module watt.path

Simple functions for dealing with paths, finding the path to the currently running executable, returning the extension of a filename, getting a full path from a relative path, and so on.

Code Map

//! Simple functions for dealing with paths, finding the path to the
//! currently running executable, returning the extension of a filename,
//! getting a full path from a relative path, and so on.
module watt.path;


enum dirSeparator;
enum pathSeparator;

//! Create a directory.
//! If the given directory exists, this function succeeds.
fn mkdir(dir: const(char)[]) { }
//! Create a directory and any intermediate directories.
//! Uses the dirSeparator string for the appropriate platform.
fn mkdirP(dir: const(char)[]) { }
//! Is c a path separator for this platform?
//! This is different to checking against dirSeparator because Windows
//! allows both forward and backward slashes in paths, whereas *nix only
//! allows forward ones.
fn isSlash(c: char) bool { }
//! Count how many path separators are in a given string.
fn countSlashes(s: const(char)[]) size_t { }
//! Remove any trailing path separators from s.
fn removeTrailingSlashes(s: const(char)[]) { }
//! Return the directory portion of a pathname.
fn dirName(path: const(char)[]) string { }
//! Return the non-directory portion of a pathname.
fn baseName(path: const(char)[], suffix: const(char)[]) string { }
//! Return the file extension of a path, or an empty string.
fn extension(path: const(char)[]) string { }
//! Get a temporary filename in the temp directory for the current
//! platform.
fn temporaryFilename(extension: string, subdir: string) string { }
//! Given a path, return an absolute path.
fn fullPath(file: string) string { }
//! Get the path to the executable.
fn getExecFile() string { }
//! Get the directory that the current executable is in.
fn getExecDir() string { }
fn mkdir(dir: const(char)[])

Create a directory.
If the given directory exists, this function succeeds.

Parameters

dir

The path to the new directory to make.

fn mkdirP(dir: const(char)[])

Create a directory and any intermediate directories.
Uses the dirSeparator string for the appropriate platform.

Parameters

dir

The path string to make.

fn isSlash(c: char) bool

Is c a path separator for this platform?
This is different to checking against dirSeparator because Windows allows both forward and backward slashes in paths, whereas *nix only allows forward ones.

fn countSlashes(s: const(char)[]) size_t

Count how many path separators are in a given string.

fn removeTrailingSlashes(s: const(char)[])

Remove any trailing path separators from s.

Examples

removeTrailingSlashes(`a/`);  // "a"
removeTrailingSlashes(`D:\Documents\`);  // `D:\Documents`
fn dirName(path: const(char)[]) string

Return the directory portion of a pathname.

An implementation of http://pubs.opengroup.org/onlinepubs/9699919799/utilities/dirname.html.

Examples

dirName("a/path/to/a/file.txt");  // "a/path/to/a"
fn baseName(path: const(char)[], suffix: const(char)[]) string

Return the non-directory portion of a pathname.

An implementation of http://pubs.opengroup.org/onlinepubs/9699919799/utilities/basename.html.

Parameters

path

The path to retrieve the non-directory portion from.

suffix

If the extracted portion of the path ends in this, remove it.

fn extension(path: const(char)[]) string

Return the file extension of a path, or an empty string.

Examples

extension("hello.volt");  // ".volt"
extension(".foo");  // ""
extension("hello");  // ""
fn temporaryFilename(extension: string, subdir: string) string

Get a temporary filename in the temp directory for the current platform.

fn fullPath(file: string) string

Given a path, return an absolute path.

Relative to the current working directory.

Parameters

file

The filename to get an absolute path to.

Return

The full path to file.

fn getExecFile() string

Get the path to the executable.

fn getExecDir() string

Get the directory that the current executable is in.

This is not the same as the current working directory of the process, this is the path to the directory the executable is physically in.