Utilities for handling and transforming filesystem paths.
This module is designed to work with the datcxx
build tool. To add this
module to your project us the following command...
build add datcxx/path
build test
Join all arguments together and normalize the resulting path. Arguments must be strings.
Path::join("/foo", "bar", "baz/asdf", "quux", "..");
// returns "/foo/bar/baz/asdf"
Determines whether path is an absolute path. An absolute path will always resolve to the same location, regardless of the working directory.
Path::isAbsolute("/foo/bar"); // true
Path::isAbsolute("/baz/.."); // true
Path::isAbsolute("qux/"); // false
Path::isAbsolute("."); // false
Return the directory name of a path. Similar to the Unix dirname command.
Path::dirname("/foo/bar/baz/asdf/quux");
// returns "/foo/bar/baz/asdf"
Return the last portion of a path. Similar to the Unix basename command.
Path::basename("/foo/bar/baz/asdf/quux.html");
// returns "quux.html"
Path::basename("/foo/bar/baz/asdf/quux.html", ".html");
// returns "quux"
Return the extension of the path, from the last "." to end of string in the last portion of the path. If there is no "." in the last portion of the path or the first character of it is ".", then it returns an empty string. Examples:
Path::extname("index.html");
// returns ".html"
Path::extname("index.coffee.md");
// returns ".md"
Path::extname("index.");
// returns "."
Path::extname("index");
// returns ""
Return the relative path between from
and to
according to the current working directory. If from
and to
resolve to the
same path (using path.resolve
) an empty string is returned.
If either of the provided from
or to
parameters, the current
working directory will be used in their place.
Examples:
Path::relative("/var/lib", "/var");
// returns ".."
Path::relative("/var/lib", "/bin");
// return "../../bin"
Path::relative("/foo/test", "/foo/test/bar/package.json");
// returns "bar/package.json"
Path::relative("/Users/a/web/b/test/mails", "/Users/a/web/b");
// returns "../.."
Returns an object from a path string.
auto o = Path::parse("/home/user/dir/file.txt");
Returns a struct of type PathObject with the following members...
o.root = "/";
o.dir = "/home/user/dir";
o.base = "file.txt";
o.ext = ".txt";
o.name = "file";
Returns a path string from an object, the opposite of path.parse above.
The struct can be created with the path.createObject()
method.
auto o = Path::createObject();
o.dir = "/home/user/dir";
o.base = "file.txt";
o.ext = ".txt";
o.name = "file";
Path::format(o);
// returns "/home/user/dir/file.txt"
Resolves to to an absolute path. If to isn"t already absolute from arguments are prepended in right to left order, until an absolute path is found. The resulting path is normalized, and trailing slashes are removed unless the path gets resolved to the root directory. Non-string from arguments will throw an error.
Another way to think of it is as a sequence of cd commands in a shell.
Path::resolve("/foo/bar", "./baz");
// returns "/foo/bar/baz"
Path::resolve("/foo/bar", "/tmp/file/");
// returns "/tmp/file"