123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350(**************************************************************************)(* *)(* 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]. *)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(** [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, and the
directories are not deleted even if [all] is [true].*)valremove_dir:?all:bool->?glob:string->t->unit(** [select ?deep ?dft ?glob ?filter ?follow_links ?error ()]
creates a selctor to customize a file iterator.
The [deep] and [dft] arguments controls whether function should
recurse 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] argument can be used to filter the basenames of files
with a regular expression.
The [filter] argument is called as [filter is_dir basename path]
where [is_dir] is set when checking whether to enter or not into
a sub-directory, [basename] is the basename of the file and
[path] is the path starting with a '/', yet relative to the
initial directory. [filter] is called on every file with [is_dir]
false to decide whether it should be added or not, and only on
sub-directories with [is_dir] true to decide whether to enter or
not if [deep] is true.
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 path filename].
*)valselect:?deep:bool->?dft:[`After|`Before]->?glob:string->?filter:(bool->string->string->bool)->?follow_links:bool->?error:(exn->string->t->unit)->unit->tFileSelector.t(** [read_dir ?select filename] returns the files contained in the directory
[filename].
In a directory, files are sorted in lexicographical order of
their names. *)valread_dir:?select:tFileSelector.t->t->tarray(** Same as [read_dir], but returns a list instead of an array *)valread_dir_to_list:?select:tFileSelector.t->t->tlist(** Same as [read_dir], but calls a function on every file and
directory with the basename, the relative path (yet, starting
with a '/') and the filename (i.e. the directory name
concatenated with the relative path): [f basename path file]. 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. *)valiter_dir:?select:tFileSelector.t->(basename:string->localpath:string->file:t->unit)->t->unit(** [iterator ?select dir] creates an iterator on directory [dir].
The iterator is a function that returns [None] when finished,
or [Some (path, filename)] with the next file to iter on.
*)valiterator:?select:tFileSelector.t->t->(unit->(string*t)option)(** [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(* [extensions file] returns the list of extensions of the file *)valextensions:t->stringlistvalbasename:t->stringvaldirname:t->tvaladd_basename:t->string->tvaladd_basenames:t->stringlist->tvalchop_extension:t->tvallast_extension:t->stringoptionvalcurrent_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