package containers
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=1e7992cb2e59c0d2290d1b6c3a31531b3f310be6170b8ef3dde17ccd876b5b79
sha512=bb124e69ad0690f88393e18eee499be07761e767593558867aab32f643466b43258ced503170b154ca3b56dbd68987abd6d9438cf473707ec9866511589a5b84
doc/containers/CCIO/index.html
Module CCIO
Source
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 ofic
. 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
)
) ;;
See Gen
in the gen library.
Input
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.
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.
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.
Read the channel's content into chunks of size at most size
Read a line from the channel. Returns None
if the input is terminated. The "\n" is removed from the line.
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.
Read all lines. NOTE the seq must be used within the lifetime of the channel, see warning at the top of the file.
Read all lines.
Read all lines into a list.
Read the whole channel into a buffer, then converted into a string.
Read the whole channel into a mutable byte array.
Output
Like with_in
but for an output channel.
Like with_out
but with the [Open_append; Open_creat; Open_wronly]
flags activated, to append to the file.
Write the given string on the channel, followed by "\n".
Write the given strings on the output. If provided, add sep
between every two strings (but not at the end).
Write the given strings on the output. If provided, add sep
between every two strings (but not at the end).
Write every string on the output, followed by "\n".
Write every string on the output, followed by "\n".
Write every string on the output, followed by "\n".
Both
val with_in_out :
?mode:int ->
?flags:open_flag list ->
string ->
(in_channel -> out_channel -> 'a) ->
'a
copy_into ic oc
writes the content of ic
into oc
. It is a blocking call.
Misc for Generators
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;;