package batteries
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=1fd7bddce07cf5d244fc9427f7b5e4d4
sha512=c0f2a0fdc8253e0ea999d8d4c58bfbf32b18d251a2e1d9656bf279de5f01a33e9aabac3af4d95f465f8b671e7711ebd37218043face233340a0c11b08fa62f78
doc/batteries.unthreaded/BatInnerIO/index.html
Module BatInnerIOSource
Core of the BatIO module.
This module contains the core definitions of BatIO, so as to avoid circular dependencies between modules which only need simple functions of BatIO and that module itself.
Don't use this module, use BatIO.
This exception is raised when reading on an input with the read or nread functions while there is no available token to read.
This exception is raised when reading on a closed input.
This exception is raised when reading on a closed output.
Read a single char from an input or raise No_more_input if no input available.
read all the contents of the input until No_more_input is raised.
Create a pipe between an input and an output. Data written from the output can be read from the input.
nread i n reads a string of size up to n from an input. The function will raise No_more_input if no input is available. It will raise Invalid_argument if n < 0.
really_nread i n reads a string of exactly n characters from the input.
input i s p len reads up to len bytes from the given input, storing them in byte sequence s, starting at position p. It returns the actual number of bytes read or raise No_more_input if no character can be read. It will raise Invalid_argument if p and len do not designate a valid subsequence of s.
really_input i s p len reads exactly len characters from the given input, storing them in the byte sequence s, starting at position p. For consistency with BatIO.input it returns len.
output o s p len writes up to len characters from byte sequence len, starting at offset p. It returns the number of characters written. It will raise Invalid_argument if p and len do not designate a valid subsequence of s.
like output above, but outputs from a substring instead of a subsequence of bytes
really_output o s p len writes exactly len characters from byte sequence s onto the the output, starting with the character at offset p. For consistency with BatIO.output it returns len.
like really_output above, but outputs from a substring instead of a subsequence of bytes
Flush all outputs.
Close the output and return its accumulator data. It can no longer be written.
Close all outputs. Ignore errors.
Create an output that will write into a string in an efficient way. When closed, the output returns all the data written into it.
Register a function to be triggered just before an output is closed.
val create_in :
read:(unit -> char) ->
input:(Bytes.t -> int -> int -> int) ->
close:(unit -> unit) ->
inputFully create an input by giving all the needed functions.
Note Do not use this function for creating an input which reads from one or more underlying inputs. Rather, use wrap_in.
val inherit_in :
?read:(unit -> char) ->
?input:(Bytes.t -> int -> int -> int) ->
?close:(unit -> unit) ->
input ->
inputSimplified and optimized version of wrap_in whenever only one input appears as dependency.
val wrap_in :
read:(unit -> char) ->
input:(Bytes.t -> int -> int -> int) ->
close:(unit -> unit) ->
underlying:input list ->
inputFully create an input reading from other inputs by giving all the needed functions.
This function is a more general version of create_in which also handles dependency management between inputs.
val create_out :
write:(char -> unit) ->
output:(Bytes.t -> int -> int -> int) ->
flush:(unit -> unit) ->
close:(unit -> 'a) ->
'a outputFully create an output by giving all the needed functions.
Note Do not use this function for creating an output which writes to one or more underlying outputs. Rather, use wrap_out.
val inherit_out :
?write:(char -> unit) ->
?output:(Bytes.t -> int -> int -> int) ->
?flush:(unit -> unit) ->
?close:(unit -> unit) ->
_ output ->
unit outputSimplified and optimized version of wrap_out whenever only one output appears as dependency.
val wrap_out :
write:(char -> unit) ->
output:(Bytes.t -> int -> int -> int) ->
flush:(unit -> unit) ->
close:(unit -> 'a) ->
underlying:'b output list ->
'a outputFully create an output that writes to one or more underlying outputs.
This function is a more general version of create_out, which also handles dependency management between outputs.
To illustrate the need for dependency management, let us consider the following values:
- an output
out - a function
f : _ output -> _ output, usingcreate_outto create a new output for writing some data to an underyling output (for instance, a function comparale totab_outor a function performing transparent compression or transparent traduction between encodings)
With these values, let us consider the following scenario
- a new output
f outis created - some data is written to
f outbut not flushed - output
outis closed, perhaps manually or as a consequence of garbage-collection, or because the program has ended - data written to
f outis flushed.
In this case, data reaches out only after out has been closed, which violates the protocol. Despite appearances, it is quite easy to reach such situation, especially in short programs.
The solution is to use wrap_out rather than create_out in f. Specifying that f out writes on out will then let the run-time flush and close f out when out is closed for any reason, which in turn avoids the issue.
Note Function close should not close underlying yourself. This is a common mistake which may cause sockets or standard output to be closed while they are still being used by another part of the program.
The default size of buffers.
Binary files API
Here is some API useful for working with binary files, in particular binary files generated by C applications. By default, encoding of multibyte integers is low-endian. The BigEndian module provide multibyte operations with other encoding.
Exception raised when a read or write operation cannot be completed.
Write an IEEE double precision floating point value.
Write an IEEE single precision floating point value.
Write a line and append a LF (it might be converted to CRLF on some systems depending on the underlying BatIO).
You can safely transform any output to an unit output in a safe way by using this function.
For compatibility purposes
Create an input that will read from a channel.
Create an output that will write into a channel.
Standard inputs/outputs
Standard output, as per Unix/Windows conventions (by default, console).
Use this output to display regular messages.
Standard error output, as per Unix/Windows conventions.
Use this output to display warnings and error messages.
An output which discards everything written to it.
Use this output to ignore messages.
Comparison
The following modules may be useful to create hashtables of inputs or outputs.