Re: How to tell if port is closed?

From: Grant Edwards <grante_at_reddwarf.ep.frco.com>
Date: Thu, 11 Mar 1999 15:52:43 -0600

On Thu, Mar 11, 1999 at 06:44:39PM +0100, Erick Gallesio wrote:

> > It looks like char-ready? returns true for a closed port.
>
> No it is not. In fact char-ready? on a closed port is an error
> and I think that it's a good behaviour.

You are, of course, correct. I mis-read the error mesage.

> > [ ... ]
> >
> > What I was trying to do was something like:
> >
> > (while (char-ready? some-port)
> > (let ((c (read-char some-port)))
> > (cond
> > ((eof-object? c) (close-input-port some-port))
> > (else (process-character c)))))
> >
> > But, char-ready? returns #t after the port is closed and the
> > subsequent read-char produces an error.
>
> Look at the error message: it's char-ready which complains, not
> read-char. I have looked at the various implementation I have on my
> machine and they produce the same result. It seems normal for me

Yes. char-ready? on a closed port should be an error. It must
have been a late night when thought it was the read-char that
was producing the error.

> I agree that this is really not pretty. Perhaps a good solution would
> be to have a predicate port-closed? and so your program could be
> expressed as
>
> (define (char-to-read? p)
> (and (not (port-closed? p)) (char-ready? p)))
>
> (while (char-to-read? some-port)
> (let ((c (read-char some-port)))
> (cond
> ((eof-object? c) (close-input-port some-port))
> (else (process-character c)))))

That does look better.

> which seems prettier .... (a little bit later) ... OK. port-closed?
> is implemented. It will be in next release.

Thanks!

-- 
Grant Edwards
Rosemount Inc.
grante_at_rosemount.com
Received on Thu Mar 11 1999 - 22:57:52 CET

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST