From: Nicolas C. <war...@fr...> - 2004-04-09 15:30:23
|
> > Actually IO does not work maybe well with buffered streams : the > > specification is that when you're reading n "elements" (characters or any) > > from an input, the result will be exactly the n elements you needed. Not > > more, and not less. This might actually be questionable : if you have spare > > time, could you suggest what parts of IO module should be modified in order > > to support buffered streams? > > I attach a patch that (IMO) improves things, but it does involve > changing the interface somewhat - maybe that's still okay at this stage? > > It changes the semantics of nread to return anything up to the requested > number items. Unlike Pervasives.input, the only situation in which > fewer are returned is if the end of the stream is reached first. The > return value is an (int, 'b) pair where the int is the number actually > returned. A new read_exactly function is added that behaves like nread > did before, i.e. it calls nread and raises No_more_input if the returned > item count does not match that requested. > > A side-effect of the implementation is that a failed call to > read_exactly is guaranteed to consume the remainder of the stream. This > may or may not be desirable, but it was already the case for streams > wrapping enums, so this merely defines a previously undefined behaviour. > > There's one problem, which is input_bits/output_bits, where the old > behaviour makes more sense. For now all I've done is changed the return > value of nread for input_bits so it compiles. > > Of course, I won't be offended if you don't think it's worth making > these changes. I'm sorry but that's not exactly what I was thinking of. I don't like so much the idea of having nread returning a pair. For example if we have an (char,string) input , the int returned is already stored into the String.length value. Same for a ('a, 'a list) input. I have been looking a little and it looks ok to modify the behavior of IO without actually modifying the interface. We just have to replace really_input by input - followed by a String resize if the readed number of chars is lower than the requested one. Same for the Enum's IOs : actually we're throwing an exception if we're requesting more than available : we will now just return the partial enum (not an empty one). Please tell me if it's ok for you this way, I'll then make the changes. Regards, Nicolas Cannasse |