(read-buffer) bug?

Started by Dmi, October 04, 2005, 01:25:49 AM

Previous topic - Next topic

Dmi

newLISP v.8.6.0 on Win32 MinGW, execute 'newlisp -h' for more info.

> (setq f (open "t" "r"))
3
> (read-buffer f 'b 2 "7")
nil
> b
nil
> (read-buffer f 'b 20)
6
> b
"345678"
> (read-file "t")
"12345678"
>

So when I specify length and wait-string, and wait-string is not found in length-bytes, then (read-buffer) returns nil.

Moreover, buffer is not touched and length-bytes will be lost from subsecuent readings.



According to the Manual, the first (read-buffer) from my example must return "12".
WBR, Dmi

Lutz

#1
When the wait-string is not found 'nil' is returned and the buffer is unchanged, but the file poniter is moved everytime, if the wait string was found or not.



The function behaves like it should, I will try to make it clearer in the documentation. At the moment it says:



"Returns the number of bytes read or nil on failure."



I could add the sentence at the beginning of this post.



Lutz

Dmi

#2
Thank for comments!



I'm confused with this quote from manual:
QuoteOptionally a string to wait for can be specified in str-wait. read-buffer will read a maximum amount of bytes specified in int-size or return earlier if str-wait was found in the data.

Here is no direction, that read-buffer will _drop_ readed bytes if no match where found.



>>IMHO begins

I think, dropping is not useful either. Can behavior be changed as such:

- On success return number of bytes read and fill the buffer

- When no match, return nil but still fill the buffer

- When failure, return nil and clear the buffer (now it stays untouched)



So real error state will be indicated by "(and nil (empty? buf))"

Quite reasonable I think. But this will turn read-buffer to general-purpose function class.

<<IMHO ends
WBR, Dmi

Lutz

#3
It makes some sense, but I have to think about this a little bit longer.



Lutz

Dmi

#4
Thanks!
WBR, Dmi

newdep

#5
Actualy the Idea from DMI could be used in Streaming applications...

Could be a nice option thought ;-)
-- (define? (Cornflakes))

Lutz

#6
Characters would be read into the buffer even when the waitstring is not found, but the buffer would be cleared before each read.



For streaming applications you can use write-buffer on a string instead of the file handle and this way stream the received characters



(set 'stream "")
(while (...)
   (read-buffer handle 'buff ...)
   (write-buffer stream buff)
)


This works a lot faster then using a (set 'stream (append stream buff)).



Lutz

Lutz

#7
See here for a description of 'read-buffer' in 8.7.0-rc1



http://newlisp.org/downloads/development/newLISP_87_Release_Notes.html">http://newlisp.org/downloads/developmen ... Notes.html">http://newlisp.org/downloads/development/newLISP_87_Release_Notes.html



Lutz

Dmi

#8
Very nice!

Thanks Lutz!
WBR, Dmi

pjot

#9
Also interesting to see that the get-url, post-url and put-url commands can have a timeout value now. I am very curious to see how you did that!



Peter