Module CCIO
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,
chunksmust 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
)
) ;;- since
- 0.6
- before 0.12
was in 'containers.io', now moved into 'containers'
type 'a or_error= ('a, string) Pervasives.resulttype 'a gen= unit -> 'a optionSee
Genin the gen library.
Input
val with_in : ?mode:int -> ?flags:Pervasives.open_flag list -> string -> (Pervasives.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.
- raises Sys_error
in case of error (same as
open_inandclose_in).
- parameter flags
opening flags (default
[Open_text]).Open_rdonlyis used in any cases.
val read_chunks : ?size:int -> Pervasives.in_channel -> string genval read_chunks_gen : ?size:int -> Pervasives.in_channel -> string genRead 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 : Pervasives.in_channel -> string optionRead a line from the channel. Returns
Noneif the input is terminated. The "\n" is removed from the line.
val read_lines : Pervasives.in_channel -> string genval read_lines_gen : Pervasives.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.
Output
val with_out : ?mode:int -> ?flags:Pervasives.open_flag list -> string -> (Pervasives.out_channel -> 'a) -> 'aLike
with_inbut for an output channel.- parameter flags
opening flags (default
[Open_creat; Open_trunc; Open_text]).
- raises Sys_error
in case of error (same as
open_outandclose_out).Open_wronlyis used in any cases.
val with_out_a : ?mode:int -> ?flags:Pervasives.open_flag list -> string -> (Pervasives.out_channel -> 'a) -> 'aLike
with_outbut with the[Open_append; Open_creat; Open_wronly]flags activated, to append to the file.- raises Sys_error
in case of error (same as
open_outandclose_out).
val write_line : Pervasives.out_channel -> string -> unitWrite the given string on the channel, followed by "\n".
val write_gen : ?sep:string -> Pervasives.out_channel -> string gen -> unitWrite the given strings on the output. If provided, add
sepbetween every two strings (but not at the end).
val write_lines : Pervasives.out_channel -> string gen -> unitWrite every string on the output, followed by "\n".
Both
Misc for Generators
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;;module File : sig ... end