On Mon, Aug 30, 2004 at 10:35:51PM +0200, Nicolas Cannasse wrote:
> > Hi all,
> > It seems there is no function to read exactly n characters
> > into a string (ie. IO.nread just without automatically
> > shortening the string if EOF is hit while reading). Since
> > at least one person needs it (me :)) I thought I would add
> > this:
> > let really_nread i n =
> > if n < 0 then raise (Invalid_argument "IO.really_nread");
> > if n = 0 then
> > ""
> > else
> > let s = String.create n in
> > let l = ref n in
> > let p = ref 0 in
> > while !l > 0 do
> > let r = i.in_input s !p !l in
> > p := !p + r;
> > l := !l - r;
> > done;
> > s
> > which is exactly the same code as IO.nread, except it
> > doesn't try to catch the No_more_input exception.
> > Any objections to adding this?
> This will cause an infinite loop on non-blocking IO (r = 0).
> You might want to raise Sys_blocked_io in this case.
Hmm... the channel adapter uses Pervasives.input to read.
This in turn uses Pervasives.unsafe_input, which uses
caml_ml_input(), which uses caml_do_read() which will
actually raise Sys_blocked_io itself when the read()
function returns the appropriate error code. So unless I'm
missing something it should actually work correctly for
Hard work often pays off after time. But laziness always pays off