Re: How to tell if port is closed?

From: Erick Gallesio <eg_at_unice.fr>
Date: Thu, 11 Mar 1999 18:44:39 +0100 (CET)

Grant Edwards writes:
>
> 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.

> [ ... ]
>
> 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


> I worked around this
> by setting a flag that is checked in the conditional for the
> while:
>
> (set! done-flag #f)
>
> (while (and done-flag (char-ready? some-port))
> (let ((c (read-char some-port)))
> (cond
> ((eof-object? c) (close-input-port some-port)
> (set! done-flag #t))
> (else (process-character c)))))
>
> It seems like there should be a better way. If char-ready?
> returns #t, should one expect that a subsequent read-char
> should not produce an 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)))))
 
which seems prettier .... (a little bit later) ... OK. port-closed?
is implemented. It will be in next release.

                -- Erick
Received on Thu Mar 11 1999 - 21:41:31 CET

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