(read-buffer) bug?

For the Compleat Fan
Locked
Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

(read-buffer) bug?

Post 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".
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post 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
WBR, Dmi

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

Lutz

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Thanks!
WBR, Dmi

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Actualy the Idea from DMI could be used in Streaming applications...
Could be a nice option thought ;-)
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

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

Lutz

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Very nice!
Thanks Lutz!
WBR, Dmi

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post 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

Locked