add CCIO.with_in_out to read and write a file

This commit is contained in:
Simon Cruanes 2015-05-29 20:58:54 +02:00
parent 36bd12ff45
commit d98c9cabca
2 changed files with 21 additions and 0 deletions

View file

@ -173,6 +173,19 @@ let rec write_lines oc g = match g () with
let write_lines_l oc l = let write_lines_l oc l =
List.iter (write_line 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 let tee funs g () = match g() with
| None -> None | None -> None
| Some x as res -> | Some x as res ->

View file

@ -113,6 +113,14 @@ val write_lines : out_channel -> string gen -> unit
val write_lines_l : out_channel -> string list -> 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} *) (** {2 Misc for Generators} *)
val tee : ('a -> unit) list -> 'a gen -> 'a gen val tee : ('a -> unit) list -> 'a gen -> 'a gen