package containers

  1. Overview
  2. Docs
A modular, clean and powerful extension of the OCaml standard library

Install

Dune Dependency

Authors

Maintainers

Sources

v2.8.1.tar.gz
md5=d84e09c5d0abc501aa17cd502e31a038
sha512=8b832f4ada6035e80d81be0cfb7bdffb695ec67d465ed6097a144019e2b8a8f909095e78019c3da2d8181cc3cd730cd48f7519e87d3162442562103b7f36aabb

doc/containers/CCIO/index.html

Module CCIOSource

IO Utils

Simple utilities to deal with basic Input/Output tasks in a resource-safe way. For advanced IO tasks, the user is advised to use something like Lwt or Async, that are far more comprehensive.

Examples:

  • obtain the list of lines of a file:
  # let l = CCIO.(with_in "/tmp/some_file" read_lines_l);;
  • transfer one file into another:
  # CCIO.(
      with_in "/tmp/input"
        (fun ic ->
           let chunks = read_chunks_gen ic in
           with_out ~flags:[Open_binary; Open_creat] ~mode:0o644 "/tmp/output"
             (fun oc ->
                write_gen oc chunks
             )
        )
    ) ;;
  • Note that the lifetime of an IO generator is tied to the underlying channel. In the example above, chunks must be used in the scope of ic. This will raise an error:
  # CCIO.(
      let chunks =
        with_in "/tmp/input"
          (fun ic -> read_chunks_gen ic)
      in
      with_out ~flags:[Open_binary;Open_creat] ~mode:0o644 "/tmp/output"
         (fun oc ->
            write_gen oc chunks
         )
    ) ;;
  • since 0.6
  • before 0.12

    was in 'containers.io', now moved into 'containers'

Sourcetype 'a or_error = ('a, string) result
Sourcetype 'a gen = unit -> 'a option

See Gen in the gen library.

Input

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

  • parameter flags

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

Sourceval read_chunks : ?size:int -> in_channel -> string gen
  • deprecated use read_chunks_gen
Sourceval read_chunks_gen : ?size:int -> in_channel -> string gen

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

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

Sourceval read_lines : in_channel -> string gen
  • deprecated use read_lines_gen
Sourceval 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.

Sourceval read_lines_l : in_channel -> string list

Read all lines into a list.

Sourceval 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
Sourceval 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

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

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

Sourceval write_line : out_channel -> string -> unit

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

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

Sourceval write_lines : out_channel -> string gen -> unit

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

Sourceval write_lines_l : out_channel -> string list -> unit

Both

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

  • since 0.12

Misc for Generators

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

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;;
Sourcemodule File : sig ... end
OCaml

Innovation. Community. Security.