Skip to content

Instantly share code, notes, and snippets.

@ijp
Created January 9, 2012 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijp/1584643 to your computer and use it in GitHub Desktop.
Save ijp/1584643 to your computer and use it in GitHub Desktop.
;; rough conception - chunks output only
(use-modules (ice-9 q))
(define (q-for-each f q)
(while (not (q-empty? q))
(f (deq! q))))
(define f! #f)
(define (port->chunking-port port)
(define queue (make-q))
(define (put-char c)
(enq! queue c))
(define (put-string s)
(string-for-each (lambda (c) (enq! queue c))
s))
(define (flush)
(let ((len (q-length queue)))
(display (number->string len 16) port)
(display "\r\n" port)
(q-for-each (lambda (elem) (write-char elem port))
queue)
(display "\r\n" port)))
(define (get-char)
(read-char port))
(define (close)
(flush)
(flush) ;; this way a zero chunk gets written
(close-port port))
(set! f! flush)
(make-soft-port
(vector
put-char
put-string
flush
get-char
close)
"rw"))
(call-with-output-string
(lambda (out-raw)
(let ((out-chunked (port->chunking-port out-raw)))
(display "First chunk" out-chunked)
(flush-all-ports)
(f!)
;(force-output out-chunked)
(display "Second chunk" out-chunked)
(f!)
;(force-output out-chunked)
(display "Third chunk" out-chunked)
(f!)
(f!)
;(close-port out-chunked)
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment