123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402(**************************************************************************)(* *)(* Typerex Libraries *)(* *)(* Copyright 2011-2017 OCamlPro SAS *)(* *)(* All rights reserved. This file is distributed under the terms of *)(* the GNU Lesser General Public License version 2.1, with the *)(* special exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)(** Signatures exported by other modules *)open[@ocaml.warning"-33"]EzCompat(** This module provides functions to read and write complete files
from and to strings, or working on file lines. *)moduletypeCONTENT_OPERATIONS=sigtypein_file(** The type of a file from which we read:
[in_channel] for [FileChannel], [string] for [FileString] and
[FileGen.t] for [FileGen].
*)typeout_file(** The type of a file to which we write:
[out_channel] for [FileChannel], [string] for [FileString] and
[FileGen.t] for [FileGen].
*)(*
Converting file contents to string, back and forth.
*)(** [read_file file] returns the full content of [file]. If the file
is opened, it is opened in binary mode, no conversion is
applied.*)valread_file:in_file->string(** [write_file file content] creates file [file] with content
[content]. If the file is opened, it is opened in binary mode,
no conversion is applied.*)valwrite_file:out_file->string->unit(** [read_subfile file pos len] returns a string containing [len]
bytes read from file [file] at pos [pos]. If the file is opened,
it is opened in binary mode. Raises [End_of_file] if the file is
too short. *)valread_subfile:in_file->int->int->string(*
Converting file contents to lines, back and forth.
*)(** [read_lines file] returns the content of [file] as an array of
lines. If the file is opened, it is opened in text mode. *)valread_lines:in_file->stringarray(** [read_lines_to_list file] returns the content of [file] as a
list of lines. If the file is opened, it is opened in text
mode. *)valread_lines_to_list:in_file->stringlist(** [write_lines file lines] creates the file [file] from an array of
lines, using [FileChannel.output_line] for each line. *)valwrite_lines:out_file->stringarray->unit(** [write_lines file lines] creates the file [file] from a list of
lines, using [FileChannel.output_line] for each line. *)valwrite_lines_of_list:out_file->stringlist->unit(** [read_sublines file pos len] returns at most [len] lines of the
file [file], starting at line [pos]. It differs from [read_subfile]
in that it will not raise any exception if the file is too
short. Note that it reads the file from beginning everytimes. *)valread_sublines:in_file->int->int->stringarray(** Same as [read_sublines], but returns a list of strings. *)valread_sublines_to_list:in_file->int->int->stringlist(*
Iterators on chunks and lines.
*)(** [iter_blocks f file] reads the content of file [file], and calls
[f buffer len] on each chunk. The [buffer] is reused, and only
the first [len] bytes are from the file. Chunks have a maximal
size of 32768. *)valiter_blocks:(Bytes.t->int->unit)->in_file->unit(** [iter_lines f file] calls [f line] on all the lines [line] of
the file [file]. *)valiter_lines:(string->unit)->in_file->unit(** [iteri_lines f file] calls [f line_num line] on every line [line]
of the file [file], with [line_num] the line number, starting with
line 0. *)valiteri_lines:(int->string->unit)->in_file->unit(** [copy_file src dst] copy all the content remaining in file [src]
to file [dst]. *)valcopy_file:in_file->out_file->unit(**/**)(* Obsolete functions *)valstring_of_file:in_file->stringvallines_of_file:in_file->stringarrayvalfile_of_string:out_file->string->unitvalstring_of_subfile:in_file->int->int->stringvalfile_of_lines:out_file->stringarray->unit(* Optimized functions, not documented *)valread_lines_to_revlist:in_file->stringlist(**/**)end(** This modules provides functions to create directories, read, iter
or remove directories, recursively or not.*)moduletypeDIRECTORY_OPERATIONS=sigtypet(** Type of a filename. It is [string] in [FileString] and
[FileGen.t] in [FileGen]. *)typeselector(** Type of the value used to create filters when iterating on
files and directories. *)exceptionNotADirectoryoft(** This exception is raised when one of the following functions is
called with a non-directory argument *)(** [make_dir ?mode ?p filename] creates a directory [filename], if it
does not already exist. It fails with [NotADirectory] if the file
already exists, but is not a directory.
The [mode] argument is the Unix permissions (0o755 by default).
The [p] argument controls whether parents directories should be
created as well, if they don't exist, instead of failing. *)valmake_dir:?mode:int->?p:bool->t->unit(* selector to iter only on one directory *)valonedir:selector(* selector to iter recursively on a directory *)valrecdir:selector(** [remove_dir ?all filename] removes directory [filename], or
complains the [NotADirectory] if it does not exist. The [all]
argument controls whether the function should recursively remove
all files and sub-directories included as well. If [glob] is
specified, it is called to select files to remove based on their
basename, and the directories are not deleted even if [all] is
[true].*)valremove_dir:?all:bool->?glob:string->t->unit(** [select ?deep ?dft ?glob ?filter_rec ?filter_fun ?follow_links
?error ()] creates a selctor to customize a file iterator.
The [deep] and [dft] arguments controls whether function should
recurse (false by default) in sub-directories. If [deep] is
[true], and [~dft] is not specified, the files are listed in
breadth-first mode ([a,b,a/x,b/x,a/x/y] for example). If [~dft]
is [`Before], the files are listed in depth-first mode, and the
ancestors are before their children. If [~dft] is [`After], the
are after their children.
The [glob], [path] and [ignore] arguments can be used to filter
the files with regular expressions. [glob] is used to choose the
files to keep based on their basenames, it is not used for the
recursion itself. [path] is used to choose the files to keep
based on the path, it is not used for the recursion
itself. [ignore] is used to choose the files to ignore, based on
their paths, both during the iteration and the recursion.
The [kinds] argument can be used to restrict the files to the
ones of a particular kind.
The [filter] argument is used to select the files to keep.
[filter for_rec path] should return [true] if the file is ok,
[false] otherwise. [for_rec] is [true] if [filter] is called to
check a directory for recursion, [false] if it is called for the
iteration.
The [follow_links] argument is used to decide if a link to
directory should be followed (when [deep] is also set).
The [error] argument is called when an error occurs, with [error
exn filename]. The default behavior is to ignore errors. *)valselect:?deep:bool->?dft:[`After|`Before]->?glob:string->(* check on basenames, not for recursion *)?path:string->(* check on paths, not for recursion *)?ignore:string->(* ignore these paths, always *)?kinds:Unix.file_kindlist->?filter:(bool->string->bool)->?follow_links:bool->?error:(exn->t->unit)->unit->selector(** [read_dir ?select dir] returns the files contained in the
directory [dir], recursively. Filenames start from the root of
the given directory, i.e. a full filename would be [dir / file].
In a directory, files are sorted in lexicographical order of
their names. *)valread_dir:?select:selector->t->stringarray(** Same as [read_dir], but returns a list instead of an array *)valread_dir_to_list:?select:selector->t->stringlist(** Same as [read_dir], but calls a function on every file and
directory. It is not equivalent to using [read_dir] and then
itering on the result, as [iter_dir] the function is called
during the traversal, not after, so concurrent modifications to
the directory might become visible. *)valiter_dir:?select:selector->t->f:(string->unit)->unit(** [iterator ?select dir] creates an iterator on directory [dir].
The iterator is a function that returns [None] when finished,
or [Some file] with the next file to iter on.
*)valiterator:?select:selector->t->(unit->stringoption)(** [make_select f] transforms [f], a function that takes an
optional selector, into a function that builds the selector on
the fly.
For example:
let () =
EzFile.make_select EzFile.iter_dir ~deep:true
~f:(fun path -> ...) dirname
which is equivalent to:
let () =
let select = EzFile.select ~deep:true () in
EzFile.iter_dir ~select
~f:(fun path -> ...) dirname
*)valmake_select:(?select:selector->t->'a)->?deep:bool->?dft:[`After|`Before]->?glob:string->(* check on basenames, not for recursion *)?path:string->(* check on paths, not for recursion *)?ignore:string->(* ignore these paths, always *)?kinds:Unix.file_kindlist->?filter:(bool->string->bool)->?follow_links:bool->?error:(exn->t->unit)->t->'a(** [mkdir filename mode] simply creates the directory [filename] with
permissions [mode]. *)valmkdir:t->int->unit(** [readdir filename] returns the files contained in directory
[filename] as an array of strings. The strings are sorted in
lexicographical order. *)valreaddir:t->stringarray(** [rmdir filename] removes directory [filename], or fails if it
does not exist or is not a directory. *)valrmdir:t->unit(**/**)valsafe_mkdir:t->unit(**/**)endmoduletypeFILENAME_OPERATIONS=sigtypet(*
Operations on filenames
*)valconcat:t->t->tvalis_absolute:t->boolvalis_relative:t->boolvalis_implicit:t->boolvaladd_suffix:t->string->tvalcheck_suffix:t->string->bool(* [cut_extension file] returns a pair [ basename, extensions ],
where [extensions] is the string of all extensions in lowercase
(without the leading dot), and [basename] is the file basename
without the extensions. *)valcut_extension:t->string*string(* [cut_extensions file] returns a pair [ basename, extensions ],
where [extensions] is the list of extensions in lowercase
(without dots), and [basename] is the file basename without the
extensions. *)valcut_extensions:t->string*stringlistvalbasename:t->stringvaldirname:t->tvaladd_path:t->string->tvaladd_basename:t->string->tvaladd_basenames:t->stringlist->tvalcurrent_dir_name:t(*
Standard operations on files
*)valopen_in:t->in_channelvalopen_out:t->out_channelvalopen_in_bin:t->in_channelvalopen_out_bin:t->out_channelvalopen_fd:t->MinUnix.open_flaglist->MinUnix.file_perm->MinUnix.file_descrvaltemp_file:t->string->tvalwith_in:t->(in_channel->unit)->unitvalwith_in_bin:t->(in_channel->unit)->unitvalwith_out:t->(out_channel->unit)->unitvalwith_out_bin:t->(out_channel->unit)->unitvalexists:t->boolvalgetcwd:unit->tvalsize:t->intvalis_directory:t->boolvalis_link:t->boolvalremove:t->unitvalrename:t->t->unitvalstat:t->MinUnix.statsvallstat:t->MinUnix.statsmoduleOP:sig(* concatenate ('/' must be the only file separator in the string) *)val(//):t->string->tend(*
Copying files
*)(*
(* [safe_mkdir dirname] creates a directory [dirname], potentially
creating any parent directory. A [~mode] argument can be
provided, otherwise it is assumed to be 0o755. [safe_mkdir] can
fail for wrong permissions, or if a directory name is already
used by another kind of files.*)
val safe_mkdir : ?mode:int -> t -> unit
*)(* [copy_rec src dst] creates [dst] as a copy of [src], even
if [src] is a directory. *)valcopy_rec:t->t->unit(* [uncopy_rec src dst] removes from [dst] everything that has the
same name as in [src]. Can be seen as the inverse operation of
[copy_rec]. *)valuncopy_rec:t->t->unit(** [find_in_path path filename] searches a file in a list of directories. *)valfind_in_path:stringlist->string->tendmoduletypeFILE_OPERATIONS=sigincludeFILENAME_OPERATIONSinclude(CONTENT_OPERATIONSwithtypein_file:=tandtypeout_file:=t)include(DIRECTORY_OPERATIONSwithtypet:=t)end