input buffered wrapper extended to accept any reading function

This commit is contained in:
Pascal Brisset
2009-04-15 18:28:21 +00:00
parent 469bc0d130
commit 58b4a769e6
2 changed files with 7 additions and 5 deletions
+2 -2
View File
@@ -93,7 +93,7 @@ let close = Unix.close
type 'a closure = Closure of 'a
let buffer_len = 256
let input = fun f ->
let input = fun ?(read = Unix.read) f ->
let buffer = String.create buffer_len
and index = ref 0 in
@@ -102,7 +102,7 @@ let input = fun f ->
index := n in
Closure (fun fd ->
let n = !index + Unix.read fd buffer !index (buffer_len - !index) in
let n = !index + read fd buffer !index (buffer_len - !index) in
Debug.call 'T' (fun f -> fprintf f "input: %d %d\n" !index n);
let rec parse = fun start n ->
Debug.call 'T' (fun f -> fprintf f "input parse: %d %d\n" start n);
+5 -3
View File
@@ -54,11 +54,13 @@ val close : Unix.file_descr -> unit
val set_dtr : Unix.file_descr -> bool -> unit
val set_speed : Unix.file_descr -> speed -> unit
val input : (string -> int) -> (Unix.file_descr -> unit) closure
(** Buffered input. [input f] Returns a closure which must be called when
val input :
?read:(Unix.file_descr -> string -> int -> int -> int) ->
(string -> int) -> (Unix.file_descr -> unit) closure
(** Buffered input. [input ?read f] Returns a closure which must be called when
characters are available on the stream. These characters are stored in a
a buffer. [f] is then called on the buffer. [f] must return the number
of consumed characters. *)
of consumed characters. Default [read] is [Unix.read] *)
type payload