module IO:High-order abstract I/O.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.
type
input
type 'a
output
'a
is the accumulator data, it is returned
when the close_out
function is called.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 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
IO.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 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
IO.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 close_out : 'a output -> 'a
val input_string : string -> input
val output_string : unit -> string output
val input_channel : Pervasives.in_channel -> input
val output_channel : Pervasives.out_channel -> unit output
val input_enum : char Enum.t -> input
enum
.val output_enum : unit -> char Enum.t output
enum
. The
final enum is returned when the output is closed.val create_in : read:(unit -> char) ->
input:(string -> int -> int -> int) -> close:(unit -> unit) -> input
val create_out : write:(char -> unit) ->
output:(string -> int -> int -> int) ->
flush:(unit -> unit) -> close:(unit -> 'a) -> 'a output
val printf : 'a output -> ('b, unit, string, unit) format4 -> 'b
val read_all : input -> string
No_more_input
is raised.val pipe : unit -> input * unit output
val pos_in : input -> input * (unit -> int)
val pos_out : 'a output -> 'a output * (unit -> int)
val cast_output : 'a output -> unit output
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_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_string : 'a output -> string -> unit
val write_line : 'a output -> string -> unit
module BigEndian:sig
..end
This enable you to read and write from an IO bit-by-bit or several bits
at the same time.
type
in_bits
type
out_bits
exception Bits_error
val input_bits : input -> in_bits
val output_bits : 'a output -> out_bits
val read_bits : in_bits -> int -> int
val write_bits : out_bits -> nbits:int -> int -> unit
val flush_bits : out_bits -> unit
val drop_bits : in_bits -> unit
Theses OO Wrappers have been written to provide easy support of ExtLib
IO by external librairies. If you want your library to support ExtLib
IO without actually requiring ExtLib to compile, you can should implement
the classes in_channel
, out_channel
, poly_in_channel
and/or
poly_out_channel
which are the common IO specifications established
for ExtLib, OCamlNet and Camomile.
(see http://www.ocaml-programming.de/tmp/IO-Classes.html for more details).
class in_channel :input ->
object
..end
class out_channel :'a output ->
object
..end
class in_chars :input ->
object
..end
class out_chars :'a output ->
object
..end
val from_in_channel : #in_channel -> input
val from_out_channel : #out_channel -> unit output
val from_in_chars : #in_chars -> input
val from_out_chars : #out_chars -> unit output