Revision: 415
Author: ygrekheretix
Date: Sun Jul 7 00:04:11 2013
Log: + IO.output_strings (Closes issue 22)
http://code.google.com/p/ocaml-extlib/source/detail?r=415
Modified:
/trunk/extlib/IO.ml
/trunk/extlib/IO.mli
=======================================
--- /trunk/extlib/IO.ml Tue Nov 27 11:46:53 2007
+++ /trunk/extlib/IO.ml Sun Jul 7 00:04:11 2013
@@ -247,6 +247,33 @@
out_close = (fun () -> Buffer.contents b);
out_flush = (fun () -> ());
}
+
+let output_strings() =
+ let sl = ref [] in
+ let size = ref 0 in
+ let b = Buffer.create 0 in
+ {
+ out_write = (fun c ->
+ if !size = Sys.max_string_length then begin
+ sl := Buffer.contents b :: !sl;
+ Buffer.clear b;
+ size := 0;
+ end else incr size;
+ Buffer.add_char b c
+ );
+ out_output = (fun s p l ->
+ if !size + l > Sys.max_string_length then begin
+ sl := Buffer.contents b :: !sl;
+ Buffer.clear b;
+ size := 0;
+ end else size := !size + l;
+ Buffer.add_substring b s p l;
+ l
+ );
+ out_close = (fun () -> sl := Buffer.contents b :: !sl; List.rev (!sl));
+ out_flush = (fun () -> ());
+ }
+
let input_channel ch =
{
=======================================
--- /trunk/extlib/IO.mli Thu May 19 03:48:33 2005
+++ /trunk/extlib/IO.mli Sun Jul 7 00:04:11 2013
@@ -109,6 +109,12 @@
(** Create an output that will write into a string in an efficient way.
When closed, the output returns all the data written into it. *)
+val output_strings : unit -> string list output
+(** Create an output that will write into a string in an efficient way.
+ When closed, the output returns all the data written into it.
+ Several strings are used in case the output size excess max_string_length
+*)
+
val input_channel : in_channel -> input
(** Create an input that will read from a channel. *)