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 (sb-impl::lisp-stream-in stream) #'sb-impl::closed-flame)))
38 (defmethod pcl-open-stream-p ((stream fundamental-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 (setf (fdefinition 'close) #'pcl-close)
59 (fmakunbound 'input-stream-p)
61 (defgeneric input-stream-p (stream)
63 (:documentation "Returns non-nil if the given Stream can perform input operations."))
65 (defmethod input-stream-p ((stream lisp-stream))
66 (and (not (eq (sb-impl::lisp-stream-in stream) #'sb-impl::closed-flame))
67 (or (not (eq (sb-impl::lisp-stream-in stream) #'ill-in))
68 (not (eq (lisp-stream-bin stream) #'ill-bin)))))
70 (defmethod input-stream-p ((stream fundamental-input-stream))
73 (fmakunbound 'output-stream-p)
75 (defgeneric output-stream-p (stream)
77 (:documentation "Returns non-nil if the given Stream can perform output operations."))
79 (defmethod output-stream-p ((stream lisp-stream))
80 (and (not (eq (sb-impl::lisp-stream-in stream) #'sb-impl::closed-flame))
81 (or (not (eq (lisp-stream-out stream) #'ill-out))
82 (not (eq (lisp-stream-bout stream) #'ill-bout)))))
84 (defmethod output-stream-p ((stream fundamental-output-stream))
87 ;;; character input streams
89 ;;; A character input stream can be created by defining a class that
90 ;;; includes FUNDAMENTAL-CHARACTER-INPUT-STREAM and defining methods
91 ;;; for the generic functions below.
93 (defgeneric stream-read-char (stream)
96 "This reads one character from the stream. It returns either a
97 character object, or the symbol :EOF if the stream is at end-of-file.
98 Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM must define a
99 method for this function."))
101 (defgeneric stream-unread-char (stream character)
104 "Un-does the last call to STREAM-READ-CHAR, as in UNREAD-CHAR.
105 Returns NIL. Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM
106 must define a method for this function."))
108 (defgeneric stream-read-char-no-hang (stream)
111 "This is used to implement READ-CHAR-NO-HANG. It returns either a
112 character, or NIL if no input is currently available, or :EOF if
113 end-of-file is reached. The default method provided by
114 FUNDAMENTAL-CHARACTER-INPUT-STREAM simply calls STREAM-READ-CHAR; this
115 is sufficient for file streams, but interactive streams should define
118 (defmethod stream-read-char-no-hang ((stream fundamental-character-input-stream))
119 (stream-read-char stream))
121 (defgeneric stream-peek-char (stream)
124 "Used to implement PEEK-CHAR; this corresponds to peek-type of NIL.
125 It returns either a character or :EOF. The default method calls
126 STREAM-READ-CHAR and STREAM-UNREAD-CHAR."))
128 (defmethod stream-peek-char ((stream fundamental-character-input-stream))
129 (let ((char (stream-read-char stream)))
130 (unless (eq char :eof)
131 (stream-unread-char stream char))
134 (defgeneric stream-listen (stream)
137 "Used by LISTEN. Returns true or false. The default method uses
138 STREAM-READ-CHAR-NO-HANG and STREAM-UNREAD-CHAR. Most streams should
139 define their own method since it will usually be trivial and will
140 always be more efficient than the default method."))
142 (defmethod stream-listen ((stream fundamental-character-input-stream))
143 (let ((char (stream-read-char-no-hang stream)))
144 (when (characterp char)
145 (stream-unread-char stream char)
148 (defgeneric stream-read-line (stream)
151 "Used by READ-LINE. A string is returned as the first value. The
152 second value is true if the string was terminated by end-of-file
153 instead of the end of a line. The default method uses repeated
154 calls to STREAM-READ-CHAR."))
156 (defmethod stream-read-line ((stream fundamental-character-input-stream))
157 (let ((res (make-string 80))
161 (let ((ch (stream-read-char stream)))
163 (return (values (shrink-vector res index) t)))
165 (when (char= ch #\newline)
166 (return (values (shrink-vector res index) nil)))
169 (let ((new (make-string len)))
172 (setf (schar res index) ch)
175 (defgeneric stream-clear-input (stream)
178 "Implements CLEAR-INPUT for the stream, returning NIL. The default
179 method does nothing."))
181 (defmethod stream-clear-input ((stream fundamental-character-input-stream))
184 ;;; character output streams
186 ;;; A character output stream can be created by defining a class that
187 ;;; includes FUNDAMENTAL-CHARACTER-OUTPUT-STREAM and defining methods
188 ;;; for the generic functions below.
190 (defgeneric stream-write-char (stream character)
193 "Writes character to the stream and returns the character. Every
194 subclass of FUNDAMENTAL-CHARACTER-OUTPUT-STREAM must have a method
195 defined for this function."))
197 (defgeneric stream-line-column (stream)
200 "This function returns the column number where the next character
201 will be written, or NIL if that is not meaningful for this stream.
202 The first column on a line is numbered 0. This function is used in
203 the implementation of PPRINT and the FORMAT ~T directive. For every
204 character output stream class that is defined, a method must be
205 defined for this function, although it is permissible for it to
206 always return NIL."))
208 ;;; STREAM-LINE-LENGTH is a CMU CL extension to Gray streams.
209 ;;; FIXME: Should we support it? Probably not..
210 (defgeneric stream-line-length (stream)
212 (:documentation "Return the stream line length or Nil."))
214 (defmethod stream-line-length ((stream fundamental-character-output-stream))
217 (defgeneric stream-start-line-p (stream)
220 "This is a predicate which returns T if the stream is positioned at
221 the beginning of a line, else NIL. It is permissible to always return
222 NIL. This is used in the implementation of FRESH-LINE. Note that
223 while a value of 0 from STREAM-LINE-COLUMN also indicates the
224 beginning of a line, there are cases where STREAM-START-LINE-P can be
225 meaningfully implemented although STREAM-LINE-COLUMN can't be. For
226 example, for a window using variable-width characters, the column
227 number isn't very meaningful, but the beginning of the line does have
228 a clear meaning. The default method for STREAM-START-LINE-P on class
229 FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses STREAM-LINE-COLUMN, so if
230 that is defined to return NIL, then a method should be provided for
231 either STREAM-START-LINE-P or STREAM-FRESH-LINE."))
233 (defmethod stream-start-line-p ((stream fundamental-character-output-stream))
234 (eql (stream-line-column stream) 0))
236 (defgeneric stream-write-string (stream string &optional (start 0) end)
239 "This is used by WRITE-STRING. It writes the string to the stream,
240 optionally delimited by start and end, which default to 0 and NIL.
241 The string argument is returned. The default method provided by
242 FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses repeated calls to
243 STREAM-WRITE-CHAR."))
245 (defmethod stream-write-string ((stream fundamental-character-output-stream)
246 string &optional (start 0) end)
247 (declare (string string)
249 (let ((end (or end (length string))))
250 (declare (fixnum end))
251 (do ((pos start (1+ pos)))
253 (declare (type index pos))
254 (stream-write-char stream (aref string pos))))
257 (defgeneric stream-terpri (stream)
260 "Writes an end of line, as for TERPRI. Returns NIL. The default
261 method does (STREAM-WRITE-CHAR stream #\NEWLINE)."))
263 (defmethod stream-terpri ((stream fundamental-character-output-stream))
264 (stream-write-char stream #\Newline))
266 (defgeneric stream-fresh-line (stream)
269 "Outputs a new line to the Stream if it is not positioned at the
270 begining of a line. Returns T if it output a new line, nil
271 otherwise. Used by FRESH-LINE. The default method uses
272 STREAM-START-LINE-P and STREAM-TERPRI."))
274 (defmethod stream-fresh-line ((stream fundamental-character-output-stream))
275 (unless (stream-start-line-p stream)
276 (stream-terpri stream)
279 (defgeneric stream-finish-output (stream)
282 "Attempts to ensure that all output sent to the Stream has reached
283 its destination, and only then returns false. Implements
284 FINISH-OUTPUT. The default method does nothing."))
286 (defmethod stream-finish-output ((stream fundamental-output-stream))
289 (defgeneric stream-force-output (stream)
292 "Attempts to force any buffered output to be sent. Implements
293 FORCE-OUTPUT. The default method does nothing."))
295 (defmethod stream-force-output ((stream fundamental-output-stream))
298 (defgeneric stream-clear-output (stream)
301 "Clears the given output Stream. Implements CLEAR-OUTPUT. The
302 default method does nothing."))
304 (defmethod stream-clear-output ((stream fundamental-output-stream))
307 (defgeneric stream-advance-to-column (stream column)
310 "Writes enough blank space so that the next character will be
311 written at the specified column. Returns true if the operation is
312 successful, or NIL if it is not supported for this stream. This is
313 intended for use by by PPRINT and FORMAT ~T. The default method uses
314 STREAM-LINE-COLUMN and repeated calls to STREAM-WRITE-CHAR with a
315 #\SPACE character; it returns NIL if STREAM-LINE-COLUMN returns NIL."))
317 (defmethod stream-advance-to-column ((stream fundamental-character-output-stream)
319 (let ((current-column (stream-line-column stream)))
321 (let ((fill (- column current-column)))
322 (dotimes-fixnum (i fill)
323 (stream-write-char stream #\Space)))
328 ;;; Binary streams can be created by defining a class that includes
329 ;;; either FUNDAMENTAL-BINARY-INPUT-STREAM or
330 ;;; FUNDAMENTAL-BINARY-OUTPUT-STREAM (or both) and defining a method
331 ;;; for STREAM-ELEMENT-TYPE and for one or both of the following
332 ;;; generic functions.
334 (defgeneric stream-read-byte (stream)
337 "Used by READ-BYTE; returns either an integer, or the symbol :EOF
338 if the stream is at end-of-file."))
340 (defgeneric stream-write-byte (stream integer)
343 "Implements WRITE-BYTE; writes the integer to the stream and
344 returns the integer as the result."))
346 ;;; example character output stream encapsulating a lisp-stream
347 (defun make-character-output-stream (lisp-stream)
348 (declare (type lisp-stream lisp-stream))
349 (make-instance 'character-output-stream :lisp-stream lisp-stream))
351 (defmethod open-stream-p ((stream character-output-stream))
352 (open-stream-p (character-output-stream-lisp-stream stream)))
354 (defmethod close ((stream character-output-stream) &key abort)
355 (close (character-output-stream-lisp-stream stream) :abort abort))
357 (defmethod input-stream-p ((stream character-output-stream))
358 (input-stream-p (character-output-stream-lisp-stream stream)))
360 (defmethod output-stream-p ((stream character-output-stream))
361 (output-stream-p (character-output-stream-lisp-stream stream)))
363 (defmethod stream-write-char ((stream character-output-stream) character)
364 (write-char character (character-output-stream-lisp-stream stream)))
366 (defmethod stream-line-column ((stream character-output-stream))
367 (charpos (character-output-stream-lisp-stream stream)))
369 (defmethod stream-line-length ((stream character-output-stream))
370 (line-length (character-output-stream-lisp-stream stream)))
372 (defmethod stream-finish-output ((stream character-output-stream))
373 (finish-output (character-output-stream-lisp-stream stream)))
375 (defmethod stream-force-output ((stream character-output-stream))
376 (force-output (character-output-stream-lisp-stream stream)))
378 (defmethod stream-clear-output ((stream character-output-stream))
379 (clear-output (character-output-stream-lisp-stream stream)))
381 ;;; example character input stream encapsulating a lisp-stream
383 (defun make-character-input-stream (lisp-stream)
384 (declare (type lisp-stream lisp-stream))
385 (make-instance 'character-input-stream :lisp-stream lisp-stream))
387 (defmethod open-stream-p ((stream character-input-stream))
388 (open-stream-p (character-input-stream-lisp-stream stream)))
390 (defmethod close ((stream character-input-stream) &key abort)
391 (close (character-input-stream-lisp-stream stream) :abort abort))
393 (defmethod input-stream-p ((stream character-input-stream))
394 (input-stream-p (character-input-stream-lisp-stream stream)))
396 (defmethod output-stream-p ((stream character-input-stream))
397 (output-stream-p (character-input-stream-lisp-stream stream)))
399 (defmethod stream-read-char ((stream character-input-stream))
400 (read-char (character-input-stream-lisp-stream stream)))
402 (defmethod stream-unread-char ((stream character-input-stream) character)
403 (unread-char character (character-input-stream-lisp-stream stream)))
405 (defmethod stream-read-char-no-hang ((stream character-input-stream))
406 (read-char-no-hang (character-input-stream-lisp-stream stream) nil :eof))
409 (defmethod stream-peek-char ((stream character-input-stream))
410 (peek-char nil (character-input-stream-lisp-stream stream) nil :eof))
413 (defmethod stream-listen ((stream character-input-stream))
414 (listen (character-input-stream-lisp-stream stream)))
416 (defmethod stream-clear-input ((stream character-input-stream))
417 (clear-input (character-input-stream-lisp-stream stream)))