Legend:
Library
Module
Module type
Parameter
Class
Class type
File paths.
A file system path specifies a file or a directory in a file system hierarchy. It is made of three parts:
An optional, platform-dependent, volume.
An optional root directory separator dir_sep whose presence distinguishes absolute paths ("/a") from relative ones ("a")
A non-empty list of dir_sep separated segments. Segments are non empty strings except for maybe the last one. The latter syntactically distinguishes directory paths ("a/b/") from file paths ("a/b").
The paths segments "." and ".." are relative path segments that respectively denote the current and parent directory. The basename of a path is its last non-empty segment if it is not a relative path segment or the empty string otherwise (e.g. on "/" or "..").
Separators and segments
val dir_sep_char : char
dir_sep_char is the platform dependent natural directory separator. This is / on POSIX and \ on Windows.
Warning. In code only use "/" as the directory separator even on Windows platforms (don't be upset, the module gives them back to you with backslashes).
add_seg p seg if p's last segment is non-empty this is p with seg added. If p's last segment is empty, this is p with the empty segment replaced by seg.
Note. The following functions use syntactic semantic properties of paths. Given a path, these properties can be different from the ones your file system attributes to it.
to_dir_path p is add_seg p "". It ensures that the resulting path syntactically represents a directory and thus, if converted to a string, that it ends with a dir_sep.
rem_empty_seg p is p without an existing last empty segment when p is not a root path, ensuring the result has no trailing dir_sep when converted to a string.
Basename and parent directory
Note. The following functions use syntactic semantic properties of paths. Given a path, these properties can be different from the ones your file system attributes to it.
basename p is the last non-empty segment of p or the empty string otherwise. The latter occurs only on root paths and on paths whose last non-empty segment is a relative segment. If no_ext is true (default to false) the basename's multiple extension, if any, is removed from the result.
is_prefix prefix p is true iff prefix is a strict prefix of p that respects path segments. More formally iff the following two conditions hold:
not Fpath.(equal (to_dir_path prefix) (to_dir_path p))
Fpath.(String.is_prefix (to_string (to_dir_path prefix)
(to_string p))) is true
Warning. By definition is_prefix p p is false. Note also that the prefix relation does not entail directory containement; for example is_prefix (v "..") (v "../..") holds.
Some q otherwise where q is p without the string prefix Fpath.to_dir_path prefix. This means that q is always relative, that it preserves p's directoryness and that Fpath.(equal (prefix
// q) p) holds.
drop_prefixed ps is ps without elements that have a strict prefixes in ps. The list order is preserved. Duplicates are not removed use uniquify for this.
relative to_dir p is q such that to_dir // q represents the same path as p. Note that q is not necessarily relative: if to_dir is relative and p is absolute p is returned.
compare p0 p1 is a total order on paths compatible with equal.
File extensions
The file extension (resp. multiple file extension) of a path segment is the suffix that starts at the last (resp. first) occurence of a '.' that is preceeded by at least one non '.' character. If there is no such occurence in the segment, the extension is empty. With these definitions, ".", "..", "..." and dot files like ".ocamlinit" or "..ocamlinit" have no extension, but ".emacs.d" and "..emacs.d" do have one.
type ext = string
The type for file extensions, '.' separator included.
get_ext p is p's basename file extension or the empty string if there is no extension. If multi is true (defaults to false), returns the multiple file extension.
to_uri_path p is the path p as an URI path. This is p with the system specific dir_sep_char directory separator replaced by '/' and with the following characters percent encoded: '%', '?', '#', ' ' (unless escape_space is false, defaults to true), and the US-ASCII control characters.
Note. In 2019, the standard definition of URIs is in a sorry state. Assuming p is UTF-8 encoded. It is believed the above function should lead to an URI path component that can be parsed by HTML5's definition of URI parsing.
sort_by_ext ~multi ps maps elements of ps by their extension as determined by Fpath.get_ext ~multi.
Search paths
A search path is a list of paths separated by a designated separator. A well known search path is PATH in which executable binaries are looked up.
val search_path_sep : string
search_path_sep is the default platform specific separator for search paths, this is ";" if Sys.win32 is true and ":" otherwise.
val list_of_search_path : ?sep:string ->string ->(t list, string)result
list_of_search_path ~sep s parses sep separated file paths from s. sep is not allowed to appear in the file paths, it defaults to search_path_sep. The order in the list matches the order from left to right in s.