Module B0_std.Fpath
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 distiguishes 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 distiguishes 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 "/"
).
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.
val dir_sep : string
dir_sep
isdir_sep_char
as a string.
val is_seg : string -> bool
is_seg s
istrue
iffs
does not contain adir_sep
or a null byte.
Paths
val v : string -> t
v s
is the strings
as a path.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).- raises Invalid_argument
if
s
is not a valid path. Useof_string
to deal with untrusted input.
val add_seg : t -> string -> t
add_seg p seg
ifp
's last segment is non-empty this isp
withseg
added. Ifp
's last segment is empty, this isp
with the empty segment replaced byseg
.- raises Invalid_argument
if
is_seg seg
isfalse
.
Directory paths
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.
val is_dir_path : t -> bool
is_dir_path p
istrue
iffp
syntactically represents a directory. This means thatp
is.
,..
or ends with/
,/.
or/..
.
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.
val basename : t -> string
basename p
is the last non-empty segment ofp
or the empty string otherwise. The latter occurs only on root paths and on paths whose last non-empty segment is a relative segment.
val parent : t -> t
parent p
is a directory path that containsp
. Ifp
is a root path this isp
itself. Ifp
is in the current directory this is./
.
Predicates and comparison
val is_rel : t -> bool
is_rel p
istrue
iffp
is a relative path, i.e. the root directory separator is missing inp
.
val is_abs : t -> bool
is_abs p
istrue
iffp
is an absolute path, i.e. the root directory separator is present inp
.
val is_root : t -> bool
is_root p
istrue
iffp
is a root directory, i.e.p
has the root directory separator and a single, empty, segment.
val is_current_dir : t -> bool
is_current_dir p
istrue
iffp
is either"."
or"./"
.
val is_parent_dir : t -> bool
is_parent_dir p
istrue
iffp
is either".."
or"../"
.
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.
val get_ext : ?multi:bool -> t -> ext
get_ext p
isp
's basename file extension or the empty string if there is no extension. Ifmulti
istrue
(defaults tofalse
), returns the multiple file extension.
val has_ext : ext -> t -> bool
has_ext ext p
istrue
iffString.equal (get_ext p) e || String.equal (get_ext ~multi:true p) e
.
Converting
val of_string : string -> (t, string) Stdlib.result
of_string s
is the strings
as a path. The following transformations are performed on the string:- On Windows any / (
0x2F
) occurence is converted to \ (0x5C
)
An error returned if
s
is""
or if it contains a null byte. The error string mentionss
.- On Windows any / (
val to_string : t -> string
to_string p
is the pathp
as a string. The result can be safely converted back withv
.
val conv : t Conv.t
conv
converts file paths. The textual representation uses non-empty atoms. See alsoconv_only
.
val conv_only : t Conv.t
conv_only
converts file paths. Textual conversion is not composable, useconv
instead. Textual encoding pass the string as is and decoding ignores the initial starting point and parses the whole input string into a file path.
val dump : t Fmt.t
dump ppf p
prints pathp
onppf
usingString.dump
.
Uniqueness
Paths map and sets
type path
= t
module Set : sig ... end
Path sets.
module Map : sig ... end
Path maps.
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";"
ifSys
.win32 istrue
and":"
otherwise.
val list_of_search_path : ?sep:string -> string -> (t list, string) Stdlib.result
list_of_search_path ~sep s
parsessep
separated file paths froms
.sep
is not allowed to appear in the file paths, it defaults tosearch_path_sep
. The order in the list matches the order from left to right ins
.