Module CCIO

module CCIO: sig .. end

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:

      # let l = CCIO.(with_in "/tmp/some_file" read_lines);;
    

      # CCIO.(
          with_in "/tmp/input"
            (fun ic ->
               let chunks = read_chunks ic in
               with_out ~flags:[Open_binary] ~mode:0o644 "/tmp/output"
                 (fun oc ->
                    write_gen oc chunks
                 )
            )
        ) ;;
    

Before 0.12 was in 'containers.io', now moved into 'containers'
Since 0.6

type 'a or_error = ('a, string) Result.result 
type 'a gen = unit -> 'a option 
See Gen in the gen library

Input


val with_in : ?mode:int ->
?flags:Pervasives.open_flag list ->
string -> (Pervasives.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)
flags : opening flags (default [Open_text]). Open_rdonly is used in any cases
val read_chunks : ?size:int -> Pervasives.in_channel -> string gen
Read the channel's content into chunks of size size
val read_line : Pervasives.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 : Pervasives.in_channel -> string gen
Read all lines. The generator should be traversed only once.
val read_lines_l : Pervasives.in_channel -> string list
Read all lines into a list
val read_all : ?size:int -> Pervasives.in_channel -> string
Read the whole channel into a buffer, then converted into a string.
Since 0.7
size : the internal buffer size
val read_all_bytes : ?size:int -> Pervasives.in_channel -> Bytes.t
Read the whole channel into a mutable byte array
Since 0.12
size : the internal buffer size

Output


val with_out : ?mode:int ->
?flags:Pervasives.open_flag list ->
string -> (Pervasives.out_channel -> 'a) -> 'a
Same as CCIO.with_in but for an output channel
Raises Sys_error in case of error (same as open_out and close_out) Open_wronly is used in any cases
flags : opening flags (default [Open_creatOpen_truncOpen_text]).
val with_out_a : ?mode:int ->
?flags:Pervasives.open_flag list ->
string -> (Pervasives.out_channel -> 'a) -> 'a
Similar to CCIO.with_out but with the [Open_appendOpen_creatOpen_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 : Pervasives.out_channel -> string -> unit
Write the given string on the channel, followed by "\n"
val write_gen : ?sep:string -> Pervasives.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_lines : Pervasives.out_channel -> string gen -> unit
Write every string on the output, followed by "\n".
val write_lines_l : Pervasives.out_channel -> string list -> unit

Both


val with_in_out : ?mode:int ->
?flags:Pervasives.open_flag list ->
string -> (Pervasives.in_channel -> Pervasives.out_channel -> 'a) -> 'a
Combines CCIO.with_in and CCIO.with_out.
Since 0.12
Raises Sys_error in case of error
flags : opening flags (default [Open_creat])

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.

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