From: Nicolas C. <war...@fr...> - 2004-08-30 20:34:59
|
> 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. Nicolas Cannasse |