Created
July 28, 2012 08:13
-
-
Save yamasushi/3192391 to your computer and use it in GitHub Desktop.
ポートのダンプをするジェネレータ。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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