# Path¶

Usage

use Path;


A file utilities library, specifically related to path operations

The Path module focuses on manipulation of the path to a file or directory. Also provided are constant values representing common idioms that may vary across operating systems (though rarely in the modern era), such as general references to a parent directory or the current directory.

Note

This module is currently missing the implementation for absPath, expandUser, normCase, normPath, and relPath. Once those are implemented, it will be considered complete.

Operations which occur on the files or directories referred to by these paths may be found in FileSystem (for operations on the file) or IO (for operations within the file).

## Constant and Function Definitions¶

const curDir = "."

Represents generally the current directory. This starts as the directory where the program is being executed from.

const parentDir = ".."

Represents generally the parent directory.

const pathSep = "/"

Denotes the separator between a directory and its child.

proc basename(name: string): string

Returns the basename of the file name provided. For instance:

writeln(basename("/foo/bar/baz")); // Prints "baz"
writeln(basename("/foo/bar/")); // Prints "", because of the empty string


Note that this is different from the Unix basename function.

Arguments: name : string -- A string file name. Note that this string does not have to be a valid file name, as the file itself will not be affected.
proc commonPath(paths: string ...?n): string

Determines and returns the longest common path prefix of all the string pathnames provided.

Arguments: paths : string -- Any number of paths. The longest common path prefix. string
proc commonPath(paths: []): string

Determines and returns the longest common path prefix of all the string pathnames provided.

Arguments: paths : array -- Any number of paths as an array. The longest common path prefix. string
proc dirname(name: string): string

Returns the parent directory of the file name provided. For instance:

writeln(dirname("/foo/bar/baz")); // Prints "/foo/bar"
writeln(dirname("/foo/bar/")); // Also prints "/foo/bar"

Arguments: name : string -- a string file name. Note that this string does not have to be a valid file name, as the file itself will not be affected.
proc expandVars(path: string): string

Expands any environment variables in the path of the form $<name> or ${<name>} into their values. If <name> does not exist, they are left in place. Returns the path which includes these expansions.

Arguments: path : string -- a string representation of a path, which may or may not include $ or ${}. path, having replaced all references to environment variables with their values. string
proc file.getParentName(): string throws

Returns the parent directory of the file record. For instance:

var myFile = open("/foo/bar/baz.txt", iomode.r);
writeln(myFile.getParentName()); // Prints "/foo/bar"


Will throw a SystemError if one occurs.

Returns: The parent directory of the file. string
proc isAbsPath(name: string): bool

Determines whether the path specified is an absolute path.

Note

This is currently only implemented in a Unix environment. It will not behave correctly in a non-Unix environment.

Arguments: name : string -- The path to be checked. true if name is an absolute path, false otherwise. bool
proc joinPath(paths: string ...?n): string

Join and return one or more paths, putting precedent on the last absolute path seen. Return value is the concatenation of the paths with one directory separator following each non-empty argument except the last. Examples:

writeln(joinPath("/foo/bar", "/baz")); // Prints "/baz"
writeln(joinPath("/foo", "./baz")); // Prints "/foo/./baz"
writeln(joinPath("/foo/", "", "./baz")); // Prints "/foo/./baz"

Arguments: paths : string -- Any number of paths. The concatenation of the last absolute path with everything following it, or all the paths provided if no absolute path is present. string
proc realPath(name: string): string throws

Given a path name, attempts to determine the canonical path referenced. This resolves and removes any curDir and parentDir uses present, as well as any symbolic links. Returns the result.

Will throw a SystemError if one occurs.

Arguments: name : string -- A path to resolve. If the path does not refer to a valid file or directory, an error will occur. A canonical version of the argument. string
proc file.realPath(): string throws

Determines the canonical path referenced by the file record performing this operation. This resolves and removes any curDir and parentDir uses present, as well as any symbolic links. Returns the result.

Will throw a SystemError if one occurs.

Returns: A canonical path to the file referenced by this file record. If the file record is not valid, an error will occur. string
proc splitPath(name: string): (string, string)

Split name into a tuple that is equivalent to (dirname, basename). The second part of the tuple will never contain a slash. Examples:

writeln(splitPath("foo/bar")); // Prints "(foo, bar)"
writeln(splitPath("bar")); // Prints "(, bar)"
writeln(splitPath("foo/")); // Prints "(foo, )"
writeln(splitPath("")); // Prints "(, )"
writeln(splitPath("/")); // Prints "(/, )"


With the exception of a path of the empty string or just "/", the original path can be recreated from this function's returned parts by joining them with the path separator character, either explicitly or by calling joinPath:

var res = splitPath("foo/bar");
var dirnameVar = res(1);
var basenameVar = res(2);
writeln(dirnameVar + "/" + basenameVar); // Prints "foo/bar"
writeln(joinPath(dirnameVar, basenameVar)); // Prints "foo/bar"

Arguments: name : string -- Path to be split.