Skip to content

Instantly share code, notes, and snippets.

@yamasushi
Created July 28, 2012 08:13
Show Gist options
  • Save yamasushi/3192391 to your computer and use it in GitHub Desktop.
Save yamasushi/3192391 to your computer and use it in GitHub Desktop.
ポートのダンプをするジェネレータ。
(use gauche.generator)
(use gauche.uvector)
(define (call-with-port-chunk port chunk-size size proc)
(let1 chunk (make-u8vector chunk-size)
(cond
[ (eqv? size 0) #t ]
[ (not size)
(until (read-block! chunk port 0 chunk-size) eof-object? => len
(proc (uvector-alias <u8vector> chunk 0 len) ) ; hook
) ]
[ (> size 0)
(let loop [ (total size)
(len (read-block! chunk port 0 (min size chunk-size)) )]
(unless (eof-object? len)
(proc (uvector-alias <u8vector> chunk 0 len)) ; hook
(let1 n (- total len)
(unless (= n 0)
(loop n (read-block! chunk port 0 (min n chunk-size) ) ) ) ) ) ) ]
) ) )
(define (port->uv-generator port chunk-size size)
(cond
[ (eqv? size 0) (null-generator) ]
[ (or (not size) (> size 0) )
(generate (^[yield] (call-with-port-chunk port chunk-size size yield))) ]
) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment