MacroPackage: exclToCDocOverviewCGDocRelNotesFAQIndexPermutedIndex
Allegro CL version 8.2
Unrevised from 8.1 to 8.2.
8.1 version

with-function-input-stream

Arguments: (sym function &rest args) &body body

with-function-input-stream evalutes body as an implicit progn with sym bound to the stream returned by make-function-input-stream. When control leaves the body, either normally or abnormally (such as by the use of throw), the stream is automatically closed.

See make-function-input-stream for details.

Example 1

(defun filler (writeto count)
  (dotimes (n count)
     (write-byte n writeto))
  (close writeto))

(with-function-input-stream (readfrom #'filler 5)
  (let ((count 0)
        (sum 0)
        got)
    (while (setf got (read-byte readfrom nil nil))
       (incf sum got)
       (incf count))
    (format t "Read ~d bytes, totalling ~a~%" count sum)))

Example 2

(defun head-filter (outstream instream &key (lines 10))
  (dotimes (n lines)
    (let ((line (read-line instream nil nil)))
      (if (null line)
	  (return))
      (write-line line outstream))))

(defun tail-filter (outstream instream &key (lines 10))
  (let ((buffer (make-array lines))
	(pos 0)
	(wrapped nil)
	line)
    (while (setf line (read-line instream nil nil))
      (setf (aref buffer pos) line)
      (incf pos)
      (if (= pos lines)
	  (progn 
	    (setf pos 0)
	    (setf wrapped t))))

    (if wrapped
	(dotimes (n (- lines pos))
	  (write-line (aref buffer (+ n pos)) outstream)))

    (dotimes (n pos)
      (write-line (aref buffer n) outstream))))

(defun uniq-filter (outstream instream)
  (let (lastline 
	line)
    (while (setf line (read-line instream nil nil))
      (if (or (not lastline) (string/= line lastline))
	  (progn
	    (write-line line outstream)
	    (setf lastline line))))))

;; uniq the last 5 of the first 10 lines of the sourcefile.
(defun test-filters (sourcefile)
  (with-open-file (f sourcefile)
    (with-function-input-stream (headstream #'head-filter f)
      (with-function-input-stream (tailstream #'tail-filter headstream
					      :lines 5)
	(uniq-filter *terminal-io* tailstream)))))

Copyright (c) 1998-2016, Franz Inc. Oakland, CA., USA. All rights reserved.
This page was not revised from the 8.1 page.
Created 2010.1.21.

ToCDocOverviewCGDocRelNotesFAQIndexPermutedIndex
Allegro CL version 8.2
Unrevised from 8.1 to 8.2.
8.1 version