1 ;;;; Gray streams implementation for SBCL, based on the Gray streams
2 ;;;; implementation for CMU CL, based on the stream-definition-by-user proposal
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is in the public domain and is provided with absolutely no
9 ;;;; warranty. See the COPYING and CREDITS files for more information.
11 (in-package "SB-GRAY")
13 (fmakunbound 'stream-element-type)
15 (defgeneric stream-element-type (stream)
18 "Returns a type specifier for the kind of object returned by the
19 Stream. Class FUNDAMENTAL-CHARACTER-STREAM provides a default method
20 which returns CHARACTER."))
22 (defmethod stream-element-type ((stream lisp-stream))
23 (funcall (lisp-stream-misc stream) stream :element-type))
25 (defmethod stream-element-type ((stream fundamental-character-stream))
28 (defgeneric pcl-open-stream-p (stream)
31 "Return true if Stream is not closed. A default method is provided
32 by class FUNDAMENTAL-STREAM which returns true if CLOSE has not been
33 called on the stream."))
35 (defmethod pcl-open-stream-p ((stream lisp-stream))
36 (not (eq (lisp-stream-in stream) #'closed-flame)))
38 (defmethod pcl-open-stream-p ((stream fundamental-stream))
39 (stream-open-p stream))
41 ;;; bootstrapping hack
42 (pcl-open-stream-p (make-string-output-stream))
43 (setf (fdefinition 'open-stream-p) #'pcl-open-stream-p)
45 (defgeneric pcl-close (stream &key abort)
48 "Closes the given Stream. No more I/O may be performed, but
49 inquiries may still be made. If :Abort is non-nil, an attempt is made
50 to clean up the side effects of having created the stream."))
52 (defmethod pcl-close ((stream lisp-stream) &key abort)
53 (when (open-stream-p stream)
54 (funcall (lisp-stream-misc stream) stream :close abort))
57 (defmethod pcl-close ((stream fundamental-stream) &key abort)
58 (setf (stream-open-p stream) nil)
61 (setf (fdefinition 'close) #'pcl-close)
63 (fmakunbound 'input-stream-p)
65 (defgeneric input-stream-p (stream)
67 (:documentation "Return non-nil if the given Stream can perform input operations."))
69 (defmethod input-stream-p ((stream lisp-stream))
70 (and (not (eq (lisp-stream-in stream) #'closed-flame))
71 (or (not (eq (lisp-stream-in stream) #'ill-in))
72 (not (eq (lisp-stream-bin stream) #'ill-bin)))))
74 (defmethod input-stream-p ((stream fundamental-input-stream))
77 (fmakunbound 'output-stream-p)
79 (defgeneric output-stream-p (stream)
81 (:documentation "Return non-nil if the given Stream can perform output operations."))
83 (defmethod output-stream-p ((stream lisp-stream))
84 (and (not (eq (lisp-stream-in stream) #'closed-flame))
85 (or (not (eq (lisp-stream-out stream) #'ill-out))
86 (not (eq (lisp-stream-bout stream) #'ill-bout)))))
88 (defmethod output-stream-p ((stream fundamental-output-stream))
91 ;;; character input streams
93 ;;; A character input stream can be created by defining a class that
94 ;;; includes FUNDAMENTAL-CHARACTER-INPUT-STREAM and defining methods
95 ;;; for the generic functions below.
97 (defgeneric stream-read-char (stream)
100 "This reads one character from the stream. It returns either a
101 character object, or the symbol :EOF if the stream is at end-of-file.
102 Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM must define a
103 method for this function."))
105 (defgeneric stream-unread-char (stream character)
108 "Un-does the last call to STREAM-READ-CHAR, as in UNREAD-CHAR.
109 Returns NIL. Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM
110 must define a method for this function."))
112 (defgeneric stream-read-char-no-hang (stream)
115 "This is used to implement READ-CHAR-NO-HANG. It returns either a
116 character, or NIL if no input is currently available, or :EOF if
117 end-of-file is reached. The default method provided by
118 FUNDAMENTAL-CHARACTER-INPUT-STREAM simply calls STREAM-READ-CHAR; this
119 is sufficient for file streams, but interactive streams should define
122 (defmethod stream-read-char-no-hang ((stream fundamental-character-input-stream))
123 (stream-read-char stream))
125 (defgeneric stream-peek-char (stream)
128 "Used to implement PEEK-CHAR; this corresponds to peek-type of NIL.
129 It returns either a character or :EOF. The default method calls
130 STREAM-READ-CHAR and STREAM-UNREAD-CHAR."))
132 (defmethod stream-peek-char ((stream fundamental-character-input-stream))
133 (let ((char (stream-read-char stream)))
134 (unless (eq char :eof)
135 (stream-unread-char stream char))
138 (defgeneric stream-listen (stream)
141 "Used by LISTEN. Returns true or false. The default method uses
142 STREAM-READ-CHAR-NO-HANG and STREAM-UNREAD-CHAR. Most streams should
143 define their own method since it will usually be trivial and will
144 always be more efficient than the default method."))
146 (defmethod stream-listen ((stream fundamental-character-input-stream))
147 (let ((char (stream-read-char-no-hang stream)))
148 (when (characterp char)
149 (stream-unread-char stream char)
152 (defgeneric stream-read-line (stream)
155 "Used by READ-LINE. A string is returned as the first value. The
156 second value is true if the string was terminated by end-of-file
157 instead of the end of a line. The default method uses repeated
158 calls to STREAM-READ-CHAR."))
160 (defmethod stream-read-line ((stream fundamental-character-input-stream))
161 (let ((res (make-string 80))
165 (let ((ch (stream-read-char stream)))
167 (return (values (shrink-vector res index) t)))
169 (when (char= ch #\newline)
170 (return (values (shrink-vector res index) nil)))
173 (let ((new (make-string len)))
176 (setf (schar res index) ch)
179 (defgeneric stream-clear-input (stream)
182 "Implements CLEAR-INPUT for the stream, returning NIL. The default
183 method does nothing."))
185 (defmethod stream-clear-input ((stream fundamental-character-input-stream))
188 ;;; character output streams
190 ;;; A character output stream can be created by defining a class that
191 ;;; includes FUNDAMENTAL-CHARACTER-OUTPUT-STREAM and defining methods
192 ;;; for the generic functions below.
194 (defgeneric stream-write-char (stream character)
197 "Writes character to the stream and returns the character. Every
198 subclass of FUNDAMENTAL-CHARACTER-OUTPUT-STREAM must have a method
199 defined for this function."))
201 (defgeneric stream-line-column (stream)
204 "This function returns the column number where the next character
205 will be written, or NIL if that is not meaningful for this stream.
206 The first column on a line is numbered 0. This function is used in
207 the implementation of PPRINT and the FORMAT ~T directive. For every
208 character output stream class that is defined, a method must be
209 defined for this function, although it is permissible for it to
210 always return NIL."))
212 (defmethod stream-line-column ((stream fundamental-character-output-stream))
215 ;;; STREAM-LINE-LENGTH is a CMU CL extension to Gray streams.
216 ;;; FIXME: Should we support it? Probably not..
217 (defgeneric stream-line-length (stream)
219 (:documentation "Return the stream line length or Nil."))
221 (defmethod stream-line-length ((stream fundamental-character-output-stream))
224 (defgeneric stream-start-line-p (stream)
227 "This is a predicate which returns T if the stream is positioned at
228 the beginning of a line, else NIL. It is permissible to always return
229 NIL. This is used in the implementation of FRESH-LINE. Note that
230 while a value of 0 from STREAM-LINE-COLUMN also indicates the
231 beginning of a line, there are cases where STREAM-START-LINE-P can be
232 meaningfully implemented although STREAM-LINE-COLUMN can't be. For
233 example, for a window using variable-width characters, the column
234 number isn't very meaningful, but the beginning of the line does have
235 a clear meaning. The default method for STREAM-START-LINE-P on class
236 FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses STREAM-LINE-COLUMN, so if
237 that is defined to return NIL, then a method should be provided for
238 either STREAM-START-LINE-P or STREAM-FRESH-LINE."))
240 (defmethod stream-start-line-p ((stream fundamental-character-output-stream))
241 (eql (stream-line-column stream) 0))
243 (defgeneric stream-write-string (stream string &optional (start 0) end)
246 "This is used by WRITE-STRING. It writes the string to the stream,
247 optionally delimited by start and end, which default to 0 and NIL.
248 The string argument is returned. The default method provided by
249 FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses repeated calls to
250 STREAM-WRITE-CHAR."))
252 (defmethod stream-write-string ((stream fundamental-character-output-stream)
253 string &optional (start 0) end)
254 (declare (string string)
256 (let ((end (or end (length string))))
257 (declare (fixnum end))
258 (do ((pos start (1+ pos)))
260 (declare (type index pos))
261 (stream-write-char stream (aref string pos))))
264 (defgeneric stream-terpri (stream)
267 "Writes an end of line, as for TERPRI. Returns NIL. The default
268 method does (STREAM-WRITE-CHAR stream #\NEWLINE)."))
270 (defmethod stream-terpri ((stream fundamental-character-output-stream))
271 (stream-write-char stream #\Newline))
273 (defgeneric stream-fresh-line (stream)
276 "Outputs a new line to the Stream if it is not positioned at the
277 begining of a line. Returns T if it output a new line, nil
278 otherwise. Used by FRESH-LINE. The default method uses
279 STREAM-START-LINE-P and STREAM-TERPRI."))
281 (defmethod stream-fresh-line ((stream fundamental-character-output-stream))
282 (unless (stream-start-line-p stream)
283 (stream-terpri stream)
286 (defgeneric stream-finish-output (stream)
289 "Attempts to ensure that all output sent to the Stream has reached
290 its destination, and only then returns false. Implements
291 FINISH-OUTPUT. The default method does nothing."))
293 (defmethod stream-finish-output ((stream fundamental-output-stream))
296 (defgeneric stream-force-output (stream)
299 "Attempts to force any buffered output to be sent. Implements
300 FORCE-OUTPUT. The default method does nothing."))
302 (defmethod stream-force-output ((stream fundamental-output-stream))
305 (defgeneric stream-clear-output (stream)
308 "Clears the given output Stream. Implements CLEAR-OUTPUT. The
309 default method does nothing."))
311 (defmethod stream-clear-output ((stream fundamental-output-stream))
314 (defgeneric stream-advance-to-column (stream column)
317 "Writes enough blank space so that the next character will be
318 written at the specified column. Returns true if the operation is
319 successful, or NIL if it is not supported for this stream. This is
320 intended for use by by PPRINT and FORMAT ~T. The default method uses
321 STREAM-LINE-COLUMN and repeated calls to STREAM-WRITE-CHAR with a
322 #\SPACE character; it returns NIL if STREAM-LINE-COLUMN returns NIL."))
324 (defmethod stream-advance-to-column ((stream fundamental-character-output-stream)
326 (let ((current-column (stream-line-column stream)))
328 (let ((fill (- column current-column)))
330 (stream-write-char stream #\Space)))
335 ;;; Binary streams can be created by defining a class that includes
336 ;;; either FUNDAMENTAL-BINARY-INPUT-STREAM or
337 ;;; FUNDAMENTAL-BINARY-OUTPUT-STREAM (or both) and defining a method
338 ;;; for STREAM-ELEMENT-TYPE and for one or both of the following
339 ;;; generic functions.
341 (defgeneric stream-read-byte (stream)
344 "Used by READ-BYTE; returns either an integer, or the symbol :EOF
345 if the stream is at end-of-file."))
347 (defgeneric stream-write-byte (stream integer)
350 "Implements WRITE-BYTE; writes the integer to the stream and
351 returns the integer as the result."))
354 This is not in the gray-stream proposal, so it is left here
356 ;;; example character output stream encapsulating a lisp-stream
357 (defun make-character-output-stream (lisp-stream)
358 (declare (type lisp-stream lisp-stream))
359 (make-instance 'character-output-stream :lisp-stream lisp-stream))
361 (defmethod open-stream-p ((stream character-output-stream))
362 (open-stream-p (character-output-stream-lisp-stream stream)))
364 (defmethod close ((stream character-output-stream) &key abort)
365 (close (character-output-stream-lisp-stream stream) :abort abort))
367 (defmethod input-stream-p ((stream character-output-stream))
368 (input-stream-p (character-output-stream-lisp-stream stream)))
370 (defmethod output-stream-p ((stream character-output-stream))
371 (output-stream-p (character-output-stream-lisp-stream stream)))
373 (defmethod stream-write-char ((stream character-output-stream) character)
374 (write-char character (character-output-stream-lisp-stream stream)))
376 (defmethod stream-line-column ((stream character-output-stream))
377 (charpos (character-output-stream-lisp-stream stream)))
379 (defmethod stream-line-length ((stream character-output-stream))
380 (line-length (character-output-stream-lisp-stream stream)))
382 (defmethod stream-finish-output ((stream character-output-stream))
383 (finish-output (character-output-stream-lisp-stream stream)))
385 (defmethod stream-force-output ((stream character-output-stream))
386 (force-output (character-output-stream-lisp-stream stream)))
388 (defmethod stream-clear-output ((stream character-output-stream))
389 (clear-output (character-output-stream-lisp-stream stream)))
391 ;;; example character input stream encapsulating a lisp-stream
393 (defun make-character-input-stream (lisp-stream)
394 (declare (type lisp-stream lisp-stream))
395 (make-instance 'character-input-stream :lisp-stream lisp-stream))
397 (defmethod open-stream-p ((stream character-input-stream))
398 (open-stream-p (character-input-stream-lisp-stream stream)))
400 (defmethod close ((stream character-input-stream) &key abort)
401 (close (character-input-stream-lisp-stream stream) :abort abort))
403 (defmethod input-stream-p ((stream character-input-stream))
404 (input-stream-p (character-input-stream-lisp-stream stream)))
406 (defmethod output-stream-p ((stream character-input-stream))
407 (output-stream-p (character-input-stream-lisp-stream stream)))
409 (defmethod stream-read-char ((stream character-input-stream))
410 (read-char (character-input-stream-lisp-stream stream)))
412 (defmethod stream-unread-char ((stream character-input-stream) character)
413 (unread-char character (character-input-stream-lisp-stream stream)))
415 (defmethod stream-read-char-no-hang ((stream character-input-stream))
416 (read-char-no-hang (character-input-stream-lisp-stream stream) nil :eof))
419 (defmethod stream-peek-char ((stream character-input-stream))
420 (peek-char nil (character-input-stream-lisp-stream stream) nil :eof))
423 (defmethod stream-listen ((stream character-input-stream))
424 (listen (character-input-stream-lisp-stream stream)))
426 (defmethod stream-clear-input ((stream character-input-stream))
427 (clear-input (character-input-stream-lisp-stream stream)))