module IO: sig endIO 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'astdout =(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 -> 'aNo_more_input if
no token available.val nread : ('a, 'b) input -> int -> 'bnread 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 -> unitval available : ('a, 'b) input -> intNot_implemented if the IO can't deal with it.val pos_in : ('a, 'b) input -> intNot_implemented if the IO can't deal with it.val write : ('a, 'b, 'c) output -> 'a -> unitval nwrite : ('a, 'b, 'c) output -> 'b -> unitval flush : ('a, 'b, 'c) output -> unitval close_out : ('a, 'b, 'c) output -> 'cval pos_out : ('a, 'b, 'c) output -> intNot_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 -> stdinval output_string : unit -> string stdoutval input_channel : Pervasives.in_channel -> stdinval output_channel : Pervasives.out_channel -> unit stdoutval input_enum : 'a Enum.t -> ('a, 'a Enum.t) inputenum.val output_enum : unit -> ('a, 'a Enum.t, 'a Enum.t) outputenum. 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) inputval create_out : write:('a -> unit) ->
nwrite:('b -> unit) ->
pos:(unit -> int) ->
flush:(unit -> unit) -> close:(unit -> 'c) -> ('a, 'b, 'c) outputval pipe : unit -> ('a, 'a list) input * ('a, 'a list, 'a list) outputpos_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 -> intval read_ui16 : (char, 'a) input -> intval read_i16 : (char, 'a) input -> intval read_i32 : (char, 'a) input -> intOverflow if the
read integer cannot be represented as a Caml 31-bit integer.val read_string : (char, 'a) input -> stringval read_line : (char, 'a) input -> stringval write_byte : (char, 'a, 'b) output -> int -> unitval write_ui16 : (char, 'a, 'b) output -> int -> unitval write_i16 : (char, 'a, 'b) output -> int -> unitval write_i32 : (char, 'a, 'b) output -> int -> unitval write_string : 'a stdout -> string -> unitval write_line : 'a stdout -> string -> unitval input_bits : (char, 'a) input -> (bool, int) inputread 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) outputwrite 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.