package batteries
Install
Dune Dependency
Authors
Maintainers
Sources
md5=ea26b5c72e6731e59d856626049cca4d
sha512=55975b62c26f6db77433a3ac31f97af609fc6789bb62ac38b267249c78fd44ff37fe81901f1cf560857b9493a6046dd37b0d1c0234c66bd59e52843aac3ce6cb
doc/batteries.unthreaded/BatFile/index.html
Module BatFile
Source
File manipulation.
Utilities
lines_of name
reads the contents of file name
as an enumeration of lines. The file is automatically closed once the last line has been reached or the enumeration is garbage-collected.
count_lines filename
count the number of lines in given file. Lines are read by the stdlib's input_line function.
write_lines name lines
writes strings given by lines
to file name
with newline character appended to each line.
size_of name
returns the size of file name
in bytes.
size_of_big name
returns the size of file name
in bytes, as a 64-bit integer.
This function is provided as the size of a file larger than 1 Gb cannot be represented with an int
on a 32-bit machine.
File permissions
File permissions are used when creating a file to allow controlling which users may read, write or open that file. To use a permission, create a value of type permission
and pass it as argument to open_in
, open_out
, with_file_in
or with_file_out
.
The list of operations which are permitted on a file.
Default permissions.
Give the current user permission to read the file. Ignored under Windows.
Give the current user permission to write the file
Give the current user permission to execute the file. Ignored under Windows.
Give the permission to read the file to the group containing the user. Ignored under Windows.
Give the permission to write the file to the group containing the user. Ignored under Windows.
Give the permission to execute the file to the group containing the user. Ignored under Windows.
Give the permission to read the file to the rest of the world. Ignored under Windows.
Give the permission to modify the file to the rest of the world. Ignored under Windows.
Give the permission to execute the file to the rest of the world. Ignored under Windows.
Join permissions
Create a permission from a Unix-style octal integer. See your favorite Unix documentation on chmod
for more details.
Set the permissions on a file.
Opening a file for reading
type open_in_flag = [
| `create
| `excl
(*Fail if the file exists and
*)`create
is set| `text
(*Open in ascii mode -- if this flag is not specified or if the operating system does not perform conversions, the file is opened in binary mode.
*)| `nonblock
(*Open in non-blocking mode
*)| `mmap
(*Open in memory-mapped mode (experimental)
*)
]
open_in file_name
opens the file named file_name
for reading.
Note You will need to close the file manually, with BatIO.close_in
. An alternative is to call with_file_in
instead of open_in
.
Naming conventions for files are platform-dependent.
val with_file_in :
?mode:open_in_flag list ->
?perm:permission ->
string ->
(BatInnerIO.input -> 'a) ->
'a
with_file_in file_name f
opens the file named file_name
for reading, invokes f
to process the contents of that file then, once f
has returned or triggered an exception, closes the file before proceeding.
Opening a file for writing
type open_out_flag = [
| `append
(*Start writing at the end of the file rather than the start
*)| `create
(*Create the file if it does not exist
*)| `trunc
(*Empty the file if it already exists; on by default
*)| `excl
(*Fail if the file exists and
*)`create
is set| `text
(*Open in ascii mode -- if this flag is not specified or if the operating system does not perform conversions, the file is opened in binary mode.
*)| `nonblock
(*Open in non-blocking mode
*)
]
Flags governing file output; they correspond to the relevant flags to the POSIX open()
call. The default flags are [`create; `trunc]
.
val open_out :
?mode:open_out_flag list ->
?perm:permission ->
string ->
unit BatInnerIO.output
open_out file_name
opens the file named file_name
for writing.
Note You will need to close the file manually, with BatIO.close_out
. An alternative is to call with_file_out
instead of open_out
.
Naming conventions for files are platform-dependent.
val with_file_out :
?mode:open_out_flag list ->
?perm:permission ->
string ->
(unit BatInnerIO.output -> 'a) ->
'a
with_file_out file_name f
opens the file named file_name
for writing, invokes f
to write onto that file then, once f
has returned or triggered an exception, closes the file before proceeding.
Opening a temporary file for writing
type open_temporary_out_flag = [
| open_out_flag
| `delete_on_exit
(*Should the file be deleted when program ends?
*)
]
val open_temporary_out :
?mode:open_temporary_out_flag list ->
?prefix:string ->
?suffix:string ->
?temp_dir:string ->
unit ->
unit BatInnerIO.output * string
open_temporary_out ()
opens a new temporary file for writing.
Note You will need to close the file manually. An alternative is to call with_temporary_out
instead of open_out
.
Naming conventions for files are platform-dependent.
val with_temporary_out :
?mode:open_temporary_out_flag list ->
?prefix:string ->
?suffix:string ->
?temp_dir:string ->
(unit BatInnerIO.output -> string -> 'a) ->
'a
with_temporary_out f
opens a new temporary file for writing, invokes f
with to write onto that file then, once f
has returned or triggered an exception, closes the file before proceeding.
Naming conventions for files are platform-dependent.