Page 1 of 1

(read-buffer) bug?

Posted: Tue Oct 04, 2005 8:25 am
by Dmi

Code: Select all

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".

Posted: Tue Oct 04, 2005 1:10 pm
by Lutz
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

Posted: Tue Oct 04, 2005 3:01 pm
by Dmi
Thank for comments!

I'm confused with this quote from manual:
Optionally 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

Posted: Tue Oct 04, 2005 8:40 pm
by Lutz
It makes some sense, but I have to think about this a little bit longer.

Lutz

Posted: Wed Oct 05, 2005 5:36 am
by Dmi
Thanks!

Posted: Wed Oct 05, 2005 6:32 am
by newdep
Actualy the Idea from DMI could be used in Streaming applications...
Could be a nice option thought ;-)

Posted: Wed Oct 05, 2005 12:42 pm
by Lutz
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

Code: Select all

(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

Posted: Thu Oct 06, 2005 10:19 pm
by Lutz
See here for a description of 'read-buffer' in 8.7.0-rc1

http://newlisp.org/downloads/developmen ... Notes.html

Lutz

Posted: Fri Oct 07, 2005 7:15 am
by Dmi
Very nice!
Thanks Lutz!

Posted: Fri Oct 07, 2005 3:50 pm
by pjot
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