chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Re: Request for a read-string! variant


From: Alejandro Forero Cuervo
Subject: [Chicken-users] Re: Request for a read-string! variant
Date: Sun, 11 Jul 2010 13:34:56 +0200
User-agent: Mutt/1.5.13 (2006-08-11)

> I want to write something akin to the following C code:
> 
>   char buffer[BUFSIZ];
>   int len;
>   while (0 < (len = read(fd, buffer, BUFSIZ))) {
>     consume(buffer, len);
>   }
> 
> I'd rather not consume characters one by one —as I'm somewhat
> concerned with efficiency— but, on the other hand, I would rather
> consume inputs as soon as they are available to my process (because
> the file descriptor may be a socket, which may wait for a reaction on
> something it sent before sending more).
> 
> What would be the recommended way to do this in Chicken Scheme?

While this is sorted out, I'll go with:

  (define (read-buffer buffer port)
    (let loop ((pos 0))
      (if (or (and (not (char-ready? port)) (< 0 pos))
              (= pos (string-length buffer)))
        (values pos buffer)
        (let ((char (read-char port)))
          (cond
            ((eof-object? char) (values pos buffer))
            (else
              (string-set! buffer pos char)
              (loop (+ pos 1))))))))

However, I would much prefer if read-string! could be adjusted as I
proposed in my original message, to avoid having to do this
char-by-char iteration.

Alejo.
http://azul.freaks-unidos.net/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]