module IO: sig end
IO module simply deals with abstract inputs/outputs. It provides a set of methods for working with these IO as well as several constructors that enable to write to an underlying channel, buffer, or enum.
Each input and output can read/write two kinds of tokens: single
token (such as a char
) and token-buffer (such as a string
).
A single token has a size of 1 and a token-buffer has a variable
size. The size of a token-buffer can be fully determined by itself
(for example, the size of the string can be determined using the
String.length
function on itself).
type ('a, 'b)
input
'a
is the type for the single-token which
can be read using the read
function, 'b
is the type for the
token-buffer which can be read using the nread
function.type ('a, 'b, 'c)
output
'a
is the type for the single-token which
can be written using the write
function, 'b
is the type for the
token-buffer which can be written using the nwrite
function.
'c
is the accumulator data, it is returned when the close_out
function is called.typestdin =
(char, string) input
char
and string
.type'a
stdout =(char, string, 'a) output
char
and string
.exception No_more_input
read
or
nread
functions while there is no available token to read.exception Input_closed
exception Output_closed
exception Not_implemented
available
pos_in
or pos_out
on a IO that does not support them.
Standard API
|
val read : ('a, 'b) input -> 'a
No_more_input
if
no token available.val nread : ('a, 'b) input -> int -> 'b
nread i n
reads a token-buffer of size up to n
from an input.
The function will raise No_more_input if no token is available and
if n
> 0. It will raise Invalid_argument
is n
< 0.val close_in : ('a, 'b) input -> unit
val available : ('a, 'b) input -> int
Not_implemented
if the IO can't deal with it.val pos_in : ('a, 'b) input -> int
Not_implemented
if the IO can't deal with it.val write : ('a, 'b, 'c) output -> 'a -> unit
val nwrite : ('a, 'b, 'c) output -> 'b -> unit
val flush : ('a, 'b, 'c) output -> unit
val close_out : ('a, 'b, 'c) output -> 'c
val pos_out : ('a, 'b, 'c) output -> int
Not_implemented
if the IO can't deal with it.val printf : ('a, string, 'b) output -> ('c, unit, string, unit) format4 -> 'c
Creation of IO Inputs/Outputs
|
val input_string : string -> stdin
val output_string : unit -> string stdout
val input_channel : Pervasives.in_channel -> stdin
val output_channel : Pervasives.out_channel -> unit stdout
val input_enum : 'a Enum.t -> ('a, 'a Enum.t) input
enum
.val output_enum : unit -> ('a, 'a Enum.t, 'a Enum.t) output
enum
. The
final enum is returned when the output is closed.val create_in : read:(unit -> 'a) ->
nread:(int -> 'b) ->
pos:(unit -> int) ->
available:(unit -> int) -> close:(unit -> unit) -> ('a, 'b) input
val create_out : write:('a -> unit) ->
nwrite:('b -> unit) ->
pos:(unit -> int) ->
flush:(unit -> unit) -> close:(unit -> 'c) -> ('a, 'b, 'c) output
val pipe : unit -> ('a, 'a list) input * ('a, 'a list, 'a list) output
pos_in
, pos_out
and
available
are implemented.
Binary files API
|
Here is some API useful for working with binary files, in particular
binary files generated by C applications.
exception Overflow of string
val read_byte : (char, 'a) input -> int
val read_ui16 : (char, 'a) input -> int
val read_i16 : (char, 'a) input -> int
val read_i32 : (char, 'a) input -> int
Overflow
if the
read integer cannot be represented as a Caml 31-bit integer.val read_string : (char, 'a) input -> string
val read_line : (char, 'a) input -> string
val write_byte : (char, 'a, 'b) output -> int -> unit
val write_ui16 : (char, 'a, 'b) output -> int -> unit
val write_i16 : (char, 'a, 'b) output -> int -> unit
val write_i32 : (char, 'a, 'b) output -> int -> unit
val write_string : 'a stdout -> string -> unit
val write_line : 'a stdout -> string -> unit
val input_bits : (char, 'a) input -> (bool, int) input
read ch
will return a bit.
nread ch n
will return an n-bit integer.val output_bits : (char, 'a, 'b) output -> (bool, int * int, 'b) output
write ch false
will write the bit 0.
write ch true
will write the bit 1.
nwrite ch (n,v)
will write the unsigned integer v
using exactly n
bits.
Don't forget to call flush
if you want to write to the original channel
after you're done with bits-writing.