CCIOIO 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_l);;# 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
)
)
) ;;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
)
) ;;type 'a or_error = ('a, string) resultSee Gen in the gen library.
val with_in :
?mode:int ->
?flags:open_flag list ->
string ->
(in_channel -> 'a) ->
'aOpen 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.
val read_chunks_gen : ?size:int -> in_channel -> string genRead 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 Stdlib.Seq.tRead 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_iter : ?size:int -> in_channel -> string iterRead the channel's content into chunks of size at most size
val read_line : in_channel -> string optionRead 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 genRead 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 Stdlib.Seq.tRead all lines. NOTE the seq must be used within the lifetime of the channel, see warning at the top of the file.
val read_lines_iter : in_channel -> string iterRead all lines.
val read_lines_l : in_channel -> string listRead all lines into a list.
val read_all : ?size:int -> in_channel -> stringRead the whole channel into a buffer, then converted into a string.
val read_all_bytes : ?size:int -> in_channel -> Stdlib.Bytes.tRead the whole channel into a mutable byte array.
val with_out :
?mode:int ->
?flags:open_flag list ->
string ->
(out_channel -> 'a) ->
'aLike with_in but for an output channel.
val with_out_a :
?mode:int ->
?flags:open_flag list ->
string ->
(out_channel -> 'a) ->
'aLike with_out but with the [Open_append; Open_creat; Open_wronly] flags activated, to append to the file.
val write_line : out_channel -> string -> unitWrite the given string on the channel, followed by "\n".
val write_gen : ?sep:string -> out_channel -> string gen -> unitWrite 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 Stdlib.Seq.t -> unitWrite the given strings on the output. If provided, add sep between every two strings (but not at the end).
val write_lines : out_channel -> string gen -> unitWrite every string on the output, followed by "\n".
val write_lines_iter : out_channel -> string iter -> unitWrite every string on the output, followed by "\n".
val write_lines_seq : out_channel -> string Stdlib.Seq.t -> unitWrite every string on the output, followed by "\n".
val write_lines_l : out_channel -> string list -> unitval with_in_out :
?mode:int ->
?flags:open_flag list ->
string ->
(in_channel -> out_channel -> 'a) ->
'aval copy_into : ?bufsize:int -> in_channel -> out_channel -> unitcopy_into ic oc writes the content of ic into oc. It is a blocking call.
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
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