Welcome to the CHICKEN Scheme pasting service

make-delimited-input-port added by DerGuteMoritz on Sun Aug 12 16:59:14 2012

(use ports test)

(define (make-delimited-input-port max-length port)
  (let ((remaining-length max-length))
    (make-input-port
     (lambda ()
       (if (zero? remaining-length)
           #!eof
           (let ((char (read-char port)))
             (set! remaining-length (- remaining-length 1))
             char)))
     (lambda ()
       (or (zero? remaining-length)
           (char-ready? port)))
     (lambda ()
       (set! remaining-length 0)))))

(let ((in (open-input-string "foobar")))
  (test "foo" (read-string #f (make-delimited-input-port 3 in)))
  (test "bar" (read-string #f (make-delimited-input-port 3 in)))
  (test-assert (eof-object? (read-char (make-delimited-input-port 3 in)))))

(let ((in (make-delimited-input-port 2 (open-input-string "xy"))))
  (test #\x (peek-char in))
  (test #\x (peek-char in))
  (test #\x (read-char in))
  (test #\y (read-char in)))

(let* ((in  (open-input-string "xy"))
       (in* (make-delimited-input-port 1 in)))
  (test "x" (read-string #f in*))
  (test "y" (read-string #f in)))

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
What type of object does `for-each' expect as second argument?
Visually impaired? Let me spell it for you (wav file) download WAV