mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
add CCIO.with_in_out to read and write a file
This commit is contained in:
parent
36bd12ff45
commit
d98c9cabca
2 changed files with 21 additions and 0 deletions
|
|
@ -173,6 +173,19 @@ let rec write_lines oc g = match g () with
|
|||
let write_lines_l oc l =
|
||||
List.iter (write_line oc) l
|
||||
|
||||
let with_in_out ?(mode=0o644) ?(flags=[Open_creat]) filename f =
|
||||
let ic = open_in_gen (Open_rdonly::flags) mode filename in
|
||||
let oc = open_out_gen (Open_wronly::flags) mode filename in
|
||||
try
|
||||
let x = f ic oc in
|
||||
close_out oc; (* must be first?! *)
|
||||
close_in ic;
|
||||
x
|
||||
with e ->
|
||||
close_out_noerr oc;
|
||||
close_in_noerr ic;
|
||||
raise e
|
||||
|
||||
let tee funs g () = match g() with
|
||||
| None -> None
|
||||
| Some x as res ->
|
||||
|
|
|
|||
|
|
@ -113,6 +113,14 @@ val write_lines : out_channel -> string gen -> unit
|
|||
|
||||
val write_lines_l : out_channel -> string list -> unit
|
||||
|
||||
(** {2 Both} *)
|
||||
|
||||
val with_in_out : ?mode:int -> ?flags:open_flag list ->
|
||||
string -> (in_channel -> out_channel -> 'a) -> 'a
|
||||
(** Combines {!with_in} and {!with_out}.
|
||||
@param flags opening flags (default [[Open_creat]])
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
(** {2 Misc for Generators} *)
|
||||
|
||||
val tee : ('a -> unit) list -> 'a gen -> 'a gen
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue