module BatInnerIO:Core of the BatIO module.sig
..end
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
.
Author(s): Nicolas Cannasse, David Teller, Philippe Strauss, Edgar Friendly
type
input
type 'a
output
exception No_more_input
read
or
nread
functions while there is no available token to read.exception Input_closed
exception Output_closed
val read : input -> char
No_more_input
if
no input available.val read_all : input -> string
No_more_input
is raised.val pipe : unit -> input * unit output
val nread : input -> int -> string
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.val really_nread : input -> int -> string
really_nread i n
reads a string of exactly n
characters
from the input. Raises No_more_input
if at least n
characters are
not available. Raises Invalid_argument
if n
< 0.val input : input -> string -> int -> int -> int
input i s p l
reads up to l
characters from the given input, storing
them in string s
, starting at character number p
. It returns the actual
number of characters read or raise No_more_input
if no character can be
read. It will raise Invalid_argument
if p
and l
do not designate a
valid substring of s
.val really_input : input -> string -> int -> int -> int
really_input i s p l
reads exactly l
characters from the given input,
storing them in the string s
, starting at position p
. For consistency with
BatIO.input
it returns l
. Raises No_more_input
if at l
characters are
not available. Raises Invalid_argument
if p
and l
do not designate a
valid substring of s
.val close_in : input -> unit
val write : 'a output -> char -> unit
val nwrite : 'a output -> string -> unit
val write_buf : 'a output -> Buffer.t -> unit
val output : 'a output -> string -> int -> int -> int
output o s p l
writes up to l
characters from string s
, starting at
offset p
. It returns the number of characters written. It will raise
Invalid_argument
if p
and l
do not designate a valid substring of s
.val really_output : 'a output -> string -> int -> int -> int
really_output o s p l
writes exactly l
characters from string s
onto
the the output, starting with the character at offset p
. For consistency with
BatIO.output
it returns l
. Raises Invalid_argument
if p
and l
do not
designate a valid substring of s
.val flush : 'a output -> unit
val flush_all : unit -> unit
val close_out : 'a output -> 'a
val close_all : unit -> unit
val input_string : string -> input
val output_string : unit -> string output
val output_buffer : Buffer.t -> string output
val on_close_out : 'a output -> ('a output -> unit) -> unit
val create_in : read:(unit -> char) ->
input:(string -> int -> int -> int) ->
close:(unit -> unit) -> input
Note Do not use this function for creating an input
which reads from one or more underlying inputs. Rather, use
BatInnerIO.wrap_in
.
val inherit_in : ?read:(unit -> char) ->
?input:(string -> int -> int -> int) ->
?close:(unit -> unit) -> input -> input
BatInnerIO.wrap_in
whenever only
one input appears as dependency.val wrap_in : read:(unit -> char) ->
input:(string -> int -> int -> int) ->
close:(unit -> unit) -> underlying:input list -> input
This function is a more general version of BatInnerIO.create_in
which also handles dependency management between inputs.
val create_out : write:(char -> unit) ->
output:(string -> int -> int -> int) ->
flush:(unit -> unit) -> close:(unit -> 'a) -> 'a output
write
: Write one character to the output (see BatInnerIO.write
).output
: Write a (sub)string to the output (see BatInnerIO.output
).flush
: Flush any buffers of this output (see BatInnerIO.flush
).close
: Close this output. The output will be automatically
flushed.
Note Do not use this function for creating an output which
writes to one or more underlying outputs. Rather, use BatInnerIO.wrap_out
.
val inherit_out : ?write:(char -> unit) ->
?output:(string -> int -> int -> int) ->
?flush:(unit -> unit) ->
?close:(unit -> unit) -> 'a output -> unit output
BatInnerIO.wrap_out
whenever only
one output appears as dependency.val wrap_out : write:(char -> unit) ->
output:(string -> int -> int -> int) ->
flush:(unit -> unit) ->
close:(unit -> 'a) ->
underlying:'b output list -> 'a output
This function is a more general version of BatInnerIO.create_out
,
which also handles dependency management between outputs.
To illustrate the need for dependency management, let us consider the following values:
out
f : _ output -> _ output
, using BatInnerIO.create_out
to
create a new output for writing some data to an underyling
output (for instance, a function comparale to tab_out
or a
function performing transparent compression or transparent
traduction between encodings)f out
is createdf out
but not flushedout
is closed, perhaps manually or as a consequence
of garbage-collection, or because the program has endedf out
is flushed.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.
write
: Write one character to the output (see BatInnerIO.write
).output
: Write a (sub)string to the output (see BatInnerIO.output
).flush
: Flush any buffers of this output (see BatInnerIO.flush
).close
: Close this output. The output will be automatically
flushed.underlying
: The list of outputs to which the new output will
write.
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.
val default_buffer_size : int
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 Overflow of string
val read_byte : input -> int
val read_signed_byte : input -> int
val read_ui16 : input -> int
val read_i16 : input -> int
val read_i32 : input -> int
Overflow
if the
read integer cannot be represented as a Caml 31-bit integer.val read_real_i32 : input -> int32
val read_i64 : input -> int64
val read_float : input -> float
val read_double : input -> float
val read_string : input -> string
val read_line : input -> string
val write_byte : 'a output -> int -> unit
val write_ui16 : 'a output -> int -> unit
val write_i16 : 'a output -> int -> unit
val write_i32 : 'a output -> int -> unit
val write_real_i32 : 'a output -> int32 -> unit
val write_i64 : 'a output -> int64 -> unit
val write_double : 'a output -> float -> unit
val write_float : 'a output -> float -> unit
val write_string : 'a output -> string -> unit
val write_line : 'a output -> string -> unit
val cast_output : 'a output -> unit output
val input_channel : ?autoclose:bool -> ?cleanup:bool -> Pervasives.in_channel -> input
autoclose
: If true or unspecified, the BatInnerIO.input
will be automatically closed when the underlying in_channel
has reached its end.cleanup
: If true, the channel
will be automatically closed when the BatInnerIO.input
is closed.
Otherwise, you will need to close the channel manually.val output_channel : ?cleanup:bool -> Pervasives.out_channel -> unit output
cleanup
: If true, the channel
will be automatically closed when the BatInnerIO.output
is closed.
Otherwise, you will need to close the channel manually.val stdin : input
val stdout : unit output
Use this output to display regular messages.
val stderr : unit output
Use this output to display warnings and error messages.
val stdnull : unit output
Use this output to ignore messages.
The following modules may be useful to create hashtables of inputs or outputs.
module Input:sig
..end
module Output:sig
..end
module Printf:sig
..end