containers

A modular, clean and powerful extension of the OCaml standard library
Library containers
Module CCIO
type 'a or_error = ( 'a, string ) result
type 'a iter = ( 'a -> unit ) -> unit
type 'a gen = unit -> 'a option

See Gen in the gen library.

Input

val with_in : ?mode:int -> ?flags:open_flag list -> string -> ( in_channel -> 'a ) -> 'a

Open an input file with the given optional flag list, calls the function on the input channel. When the function raises or returns, the channel is closed.

  • raises Sys_error

    in case of error (same as open_in and close_in).

  • parameter flags

    opening flags (default [Open_text]). Open_rdonly is used in any cases.

val read_chunks_gen : ?size:int -> in_channel -> string gen

Read the channel's content into chunks of size at most size. NOTE the generator must be used within the lifetime of the channel, see warning at the top of the file.

val read_chunks_seq : ?size:int -> in_channel -> string Seq.t

Read the channel's content into chunks of size at most size. NOTE the generator must be used within the lifetime of the channel, see warning at the top of the file.

  • since 3.5
val read_chunks_iter : ?size:int -> in_channel -> string iter

Read the channel's content into chunks of size at most size

  • since 3.6
val read_line : in_channel -> string option

Read a line from the channel. Returns None if the input is terminated. The "\n" is removed from the line.

val read_lines_gen : in_channel -> string gen

Read all lines. The generator should be traversed only once. NOTE the generator must be used within the lifetime of the channel, see warning at the top of the file.

val read_lines_seq : in_channel -> string Seq.t

Read all lines. NOTE the seq must be used within the lifetime of the channel, see warning at the top of the file.

  • since 3.5
val read_lines_iter : in_channel -> string iter

Read all lines.

  • since 3.6
val read_lines_l : in_channel -> string list

Read all lines into a list.

val read_all : ?size:int -> in_channel -> string

Read the whole channel into a buffer, then converted into a string.

  • parameter size

    the internal buffer size.

  • since 0.7
val read_all_bytes : ?size:int -> in_channel -> Bytes.t

Read the whole channel into a mutable byte array.

  • parameter size

    the internal buffer size.

  • since 0.12

Output

val with_out : ?mode:int -> ?flags:open_flag list -> string -> ( out_channel -> 'a ) -> 'a

Like with_in but for an output channel.

  • parameter flags

    opening flags (default [Open_creat; Open_trunc; Open_text]).

  • raises Sys_error

    in case of error (same as open_out and close_out). Open_wronly is used in any cases.

val with_out_a : ?mode:int -> ?flags:open_flag list -> string -> ( out_channel -> 'a ) -> 'a

Like with_out but with the [Open_append; Open_creat; Open_wronly] flags activated, to append to the file.

  • raises Sys_error

    in case of error (same as open_out and close_out).

val write_line : out_channel -> string -> unit

Write the given string on the channel, followed by "\n".

val write_gen : ?sep:string -> out_channel -> string gen -> unit

Write the given strings on the output. If provided, add sep between every two strings (but not at the end).

val write_seq : ?sep:string -> out_channel -> string Seq.t -> unit

Write the given strings on the output. If provided, add sep between every two strings (but not at the end).

  • since 3.5
val write_lines : out_channel -> string gen -> unit

Write every string on the output, followed by "\n".

val write_lines_iter : out_channel -> string iter -> unit

Write every string on the output, followed by "\n".

  • since 3.6
val write_lines_seq : out_channel -> string Seq.t -> unit

Write every string on the output, followed by "\n".

  • since 3.5
val write_lines_l : out_channel -> string list -> unit

Both

val with_in_out : ?mode:int -> ?flags:open_flag list -> string -> ( in_channel -> out_channel -> 'a ) -> 'a

Combines with_in and with_out.

  • parameter flags

    opening flags (default [Open_creat]).

  • raises Sys_error

    in case of error.

  • since 0.12
val copy_into : ?bufsize:int -> in_channel -> out_channel -> unit

copy_into ic oc writes the content of ic into oc. It is a blocking call.

  • since 3.0

Misc for Generators

val tee : ( 'a -> unit ) list -> 'a gen -> 'a gen

tee funs gen behaves like gen, but each element is given to every function f in funs at the time the element is produced. The returned generator will raise any exception that f raises

File and file names

How to list recursively files in a directory:

# let files = CCIO.File.read_dir ~recurse:true (CCIO.File.make "/tmp");;
# CCIO.write_lines stdout files;;

See File.walk if you also need to list directories:

# let content = CCIO.File.walk (CCIO.File.make "/tmp");;
# Gen.map CCIO.File.show_walk_item content |> CCIO.write_lines stdout;;
module File : sig ... end