Legend:
Library
Module
Module type
Parameter
Class
Class type
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
before0.12
was in 'containers.io', now moved into 'containers'
val with_in :
?mode:int ->?flags:Stdlib.open_flag list->string ->(Stdlib.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.
raisesSys_error
in case of error (same as open_in and close_in).
parameterflags
opening flags (default [Open_text]). Open_rdonly is used in any cases.
val read_chunks_gen : ?size:int ->Stdlib.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.
val read_line : Stdlib.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 : Stdlib.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_l : Stdlib.in_channel ->string list
Read all lines into a list.
val read_all : ?size:int ->Stdlib.in_channel -> string
Read the whole channel into a buffer, then converted into a string.
parametersize
the internal buffer size.
since 0.7
val read_all_bytes : ?size:int ->Stdlib.in_channel ->Stdlib.Bytes.t
Read the whole channel into a mutable byte array.
parametersize
the internal buffer size.
since 0.12
Output
val with_out :
?mode:int ->?flags:Stdlib.open_flag list->string ->(Stdlib.out_channel ->'a)->'a
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