package extlib

  1. Overview
  2. Docs

Module IOSource

High-order abstract I/O.

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.

Sourcetype input

The abstract input type.

Sourcetype 'a output

The abstract output type, 'a is the accumulator data, it is returned when the close_out function is called.

Sourceexception No_more_input

This exception is raised when reading on an input with the read or nread functions while there is no available token to read.

Sourceexception Input_closed

This exception is raised when reading on a closed input.

Sourceexception Output_closed

This exception is raised when reading on a closed output.

Standard API
Sourceval read : input -> char

Read a single char from an input or raise No_more_input if no input available.

Sourceval nread : input -> int -> ExtBytes.Bytes.t

nread i n reads a byte sequence 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.

Sourceval really_nread : input -> int -> ExtBytes.Bytes.t

really_nread i n reads a byte sequence 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.

Sourceval nread_string : input -> int -> string

as nread, but reads a string.

Sourceval really_nread_string : input -> int -> string

as really_nread, but reads a string.

Sourceval input : input -> ExtBytes.Bytes.t -> int -> int -> int

input i b p l reads up to l characters from the given input, storing them in buffer b, 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 subsequence of b.

Sourceval really_input : input -> ExtBytes.Bytes.t -> int -> int -> int

really_input i b p l reads exactly l characters from the given input, storing them in the buffer b, 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 subsequence of b.

Sourceval close_in : input -> unit

Close the input. It can no longer be read from.

Sourceval write : 'a output -> char -> unit

Write a single char to an output.

Sourceval nwrite : 'a output -> ExtBytes.Bytes.t -> unit

Write a byte sequence to an output.

Sourceval nwrite_string : 'a output -> string -> unit

Write a string to an output.

Sourceval output : 'a output -> ExtBytes.Bytes.t -> int -> int -> int

output o b p l writes up to l characters from byte sequence b, 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 subsequence of b.

Sourceval really_output : 'a output -> ExtBytes.Bytes.t -> int -> int -> int

really_output o b p l writes exactly l characters from byte sequence b 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 subsequence of b.

Sourceval flush : 'a output -> unit

Flush an output.

Sourceval close_out : 'a output -> 'a

Close the output and return its accumulator data. It can no longer be written.

Creation of IO Inputs/Outputs
Sourceval input_string : string -> input

Create an input that will read from a string.

Sourceval input_bytes : ExtBytes.Bytes.t -> input

Create an input that will read from a byte sequence.

Sourceval output_string : unit -> string output

Create an output that will write into a string in an efficient way. When closed, the output returns all the data written into it.

Sourceval output_bytes : unit -> ExtBytes.Bytes.t output

Create an output that will write into a byte sequence in an efficient way. When closed, the output returns all the data written into it.

Sourceval output_strings : unit -> string list output

Create an output that will write into a string in an efficient way. When closed, the output returns all the data written into it. Several strings are used in case the output size excess max_string_length

Sourceval input_channel : in_channel -> input

Create an input that will read from a channel.

Sourceval output_channel : out_channel -> unit output

Create an output that will write into a channel.

Sourceval input_enum : char Enum.t -> input

Create an input that will read from an enum.

Sourceval output_enum : unit -> char Enum.t output

Create an output that will write into an enum. The final enum is returned when the output is closed.

Sourceval create_in : read:(unit -> char) -> input:(ExtBytes.Bytes.t -> int -> int -> int) -> close:(unit -> unit) -> input

Fully create an input by giving all the needed functions.

Sourceval create_out : write:(char -> unit) -> output:(ExtBytes.Bytes.t -> int -> int -> int) -> flush:(unit -> unit) -> close:(unit -> 'a) -> 'a output

Fully create an output by giving all the needed functions.

Utilities
Sourceval scanf : input -> ('a, 'b, 'c, 'd) Scanf.scanner

The scanf function works for any input.

Sourceval printf : 'a output -> ('b, unit, string, unit) format4 -> 'b

The printf function works for any output.

Sourceval read_all : input -> string

read all the contents of the input until No_more_input is raised.

Sourceval pipe : unit -> input * unit output

Create a pipe between an input and an ouput. Data written from the output can be read from the input.

Sourceval pos_in : input -> input * (unit -> int)

Create an input that provide a count function of the number of Bytes.t read from it.

Sourceval pos_out : 'a output -> 'a output * (unit -> int)

Create an output that provide a count function of the number of Bytes.t written through it.

Sourceval cast_output : 'a output -> unit output

You can safely transform any output to an unit output in a safe way by using this function.

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.

Sourceexception Overflow of string

Exception raised when a read or write operation cannot be completed.

Sourceval read_byte : input -> int

Read an unsigned 8-bit integer.

Sourceval read_signed_byte : input -> int

Read an signed 8-bit integer.

Sourceval read_ui16 : input -> int

Read an unsigned 16-bit word.

Sourceval read_i16 : input -> int

Read a signed 16-bit word.

Sourceval read_i31 : input -> int

Read a signed 32-bit integer. Raise Overflow if the read integer cannot be represented as an OCaml 31-bit integer.

Sourceval read_i32 : input -> int

Deprecated, same as read_i31

Sourceval read_i32_as_int : input -> int

Read a signed 32-bit integer, represented as OCaml integer, wrapping around 31-bit int on 32-bit architecture

Sourceval read_real_i32 : input -> int32

Read a signed 32-bit integer as an OCaml int32.

Sourceval read_i64 : input -> int64

Read a signed 64-bit integer as an OCaml int64.

Sourceval read_float32 : input -> float

Read an IEEE single precision floating point value (32 bits).

Sourceval read_double : input -> float

Read an IEEE double precision floating point value (64 bits).

Sourceval read_string : input -> string

Read a null-terminated string.

Sourceval read_bytes : input -> ExtBytes.Bytes.t

Read a null-terminated byte sequence.

Sourceval read_line : input -> string

Read a LF or CRLF terminated string.

Sourceval write_byte : 'a output -> int -> unit

Write an unsigned 8-bit byte.

Sourceval write_ui16 : 'a output -> int -> unit

Write an unsigned 16-bit word.

Sourceval write_i16 : 'a output -> int -> unit

Write a signed 16-bit word.

Sourceval write_i31 : 'a output -> int -> unit

Write a signed 31-bit integer as 4 bytes.

Sourceval write_i32 : 'a output -> int -> unit

Write a signed 32-bit integer.

Sourceval write_real_i32 : 'a output -> int32 -> unit

Write an OCaml int32.

Sourceval write_i64 : 'a output -> int64 -> unit

Write an OCaml int64.

Sourceval write_float32 : 'a output -> float -> unit

Write an IEEE single precision floating point value (32 bits).

Sourceval write_double : 'a output -> float -> unit

Write an IEEE double precision floating point value (64 bits).

Sourceval write_string : 'a output -> string -> unit

Write a string and append an null character.

Sourceval write_bytes : 'a output -> ExtBytes.Bytes.t -> unit

Write a byte sequence and append an null character.

Sourceval write_line : 'a output -> string -> unit

Write a line and append a LF (it might be converted to CRLF on some systems depending on the underlying IO).

Sourcemodule BigEndian : sig ... end

Same as operations above, but use big-endian encoding

Bits API

This enable you to read and write from an IO bit-by-bit or several bits at the same time.

Sourcetype in_bits
Sourcetype out_bits
Sourceexception Bits_error
Sourceval input_bits : input -> in_bits

Read bits from an input

Sourceval output_bits : 'a output -> out_bits

Write bits to an output

Sourceval read_bits : in_bits -> int -> int

Read up to 31 bits, raise Bits_error if n < 0 or n > 31

Sourceval write_bits : out_bits -> nbits:int -> int -> unit

Write up to 31 bits represented as a value, raise Bits_error if nbits < 0 or nbits > 31 or the value representation excess nbits.

Sourceval flush_bits : out_bits -> unit

Flush remaining unwritten bits, adding up to 7 bits which values 0.

Sourceval drop_bits : in_bits -> unit

Drop up to 7 buffered bits and restart to next input character.

Generic IO Object Wrappers

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).

Sourceclass in_channel : input -> object ... end
Sourceclass out_channel : 'a output -> object ... end
Sourceclass in_chars : input -> object ... end
Sourceclass out_chars : 'a output -> object ... end
Sourceval from_in_channel : in_channel -> input
Sourceval from_out_channel : out_channel -> unit output
Sourceval from_in_chars : in_chars -> input
Sourceval from_out_chars : out_chars -> unit output
OCaml

Innovation. Community. Security.