[gambit-list] I seem unable to grasp I/O...

Marc Feeley feeley at iro.umontreal.ca
Mon May 11 23:04:01 EDT 2009


On 11-May-09, at 10:30 PM, Alex Queiroz wrote:

> Hallo,
>
>     Why does this code always return the output vector, instead of
> whatever I put inside the inner 'if'?
>
> (define (embed-resource file)
>  (let* ((buffer-size 4096)
>         (buffer (make-u8vector buffer-size 0))
>         (out-vector (make-u8vector 0))
>         (info (file-info file)))
>    (if (eq? (file-info-type info) 'regular)
>        (call-with-input-file file
>          (lambda (in)
>            (call-with-output-u8vector out-vector
>              (lambda (out)
>                (let loop ()
>                  (let ((size (read-subu8vector buffer 0 buffer-size  
> in)))
>                    (write-subu8vector buffer 0 size out)
>                    (if (< size buffer-size)
>                        `(define
>                           ,(string->symbol (path-strip-extension  
> file)))
>                        (loop))))))))
>        (error "bad file" file))))


The procedure call-with-output-u8vector returns the u8vector that was  
accumulated.  What you want is this:

(define (embed-resource file)
  (let* ((buffer-size 4096)
         (buffer (make-u8vector buffer-size 0))
         (out-vector (make-u8vector 0))
         (info (file-info file)))
    (if (eq? (file-info-type info) 'regular)
        (call-with-input-file file
          (lambda (in)
            `(define
               ,(string->symbol (path-strip-extension file))
               ',(call-with-output-u8vector out-vector
                   (lambda (out)
                     (let loop ()
                       (let ((size (read-subu8vector buffer 0 buffer- 
size in)))
                         (write-subu8vector buffer 0 size out)
                         (if (= size buffer-size)
                             (loop)))))))))
        (error "bad file" file))))

Marc



More information about the Gambit-list mailing list