1 ;;;; Gray streams implementation for SBCL, based on the Gray streams
2 ;;;; implementation for CMU CL, based on the stream-definition-by-user
3 ;;;; proposal by David N. Gray.
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
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
12 (in-package "SB-GRAY")
14 (fmakunbound 'stream-element-type)
16 (defgeneric stream-element-type (stream)
19 "Return a type specifier for the kind of object returned by the
20 STREAM. The class FUNDAMENTAL-CHARACTER-STREAM provides a default method
21 which returns CHARACTER."))
23 (defmethod stream-element-type ((stream ansi-stream))
24 (ansi-stream-element-type stream))
26 (defmethod stream-element-type ((stream fundamental-character-stream))
29 (defgeneric pcl-open-stream-p (stream)
32 "Return true if STREAM is not closed. A default method is provided
33 by class FUNDAMENTAL-STREAM which returns true if CLOSE has not been
34 called on the stream."))
36 (defmethod pcl-open-stream-p ((stream ansi-stream))
37 (ansi-stream-open-stream-p stream))
39 (defmethod pcl-open-stream-p ((stream fundamental-stream))
40 (stream-open-p stream))
42 ;;; bootstrapping hack
43 (pcl-open-stream-p (make-string-output-stream))
44 (setf (fdefinition 'open-stream-p) #'pcl-open-stream-p)
46 (defgeneric pcl-close (stream &key abort)
49 "Close the given STREAM. No more I/O may be performed, but
50 inquiries may still be made. If :ABORT is true, an attempt is made
51 to clean up the side effects of having created the stream."))
53 (defmethod pcl-close ((stream ansi-stream) &key abort)
54 (ansi-stream-close stream abort))
56 (defmethod pcl-close ((stream fundamental-stream) &key abort)
57 (declare (ignore abort))
58 (setf (stream-open-p stream) nil)
61 (setf (fdefinition 'close) #'pcl-close)
64 (fmakunbound 'input-stream-p)
66 (defgeneric input-stream-p (stream)
68 (:documentation "Can STREAM perform input operations?"))
70 (defmethod input-stream-p ((stream ansi-stream))
71 (ansi-stream-input-stream-p stream))
73 (defmethod input-stream-p ((stream fundamental-input-stream))
77 (fmakunbound 'output-stream-p)
79 (defgeneric output-stream-p (stream)
81 (:documentation "Can STREAM perform output operations?"))
83 (defmethod output-stream-p ((stream ansi-stream))
84 (ansi-stream-output-stream-p stream))
86 (defmethod output-stream-p ((stream fundamental-output-stream))
89 ;;; character input streams
91 ;;; A character input stream can be created by defining a class that
92 ;;; includes FUNDAMENTAL-CHARACTER-INPUT-STREAM and defining methods
93 ;;; for the generic functions below.
95 (defgeneric stream-read-char (stream)
98 "Read one character from the stream. Return either a
99 character object, or the symbol :EOF if the stream is at end-of-file.
100 Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM must define a
101 method for this function."))
103 (defgeneric stream-unread-char (stream character)
106 "Un-do the last call to STREAM-READ-CHAR, as in UNREAD-CHAR.
107 Return NIL. Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM
108 must define a method for this function."))
110 (defgeneric stream-read-char-no-hang (stream)
113 "This is used to implement READ-CHAR-NO-HANG. It returns either a
114 character, or NIL if no input is currently available, or :EOF if
115 end-of-file is reached. The default method provided by
116 FUNDAMENTAL-CHARACTER-INPUT-STREAM simply calls STREAM-READ-CHAR; this
117 is sufficient for file streams, but interactive streams should define
120 (defmethod stream-read-char-no-hang ((stream fundamental-character-input-stream))
121 (stream-read-char stream))
123 (defgeneric stream-peek-char (stream)
126 "This is used to implement PEEK-CHAR; this corresponds to PEEK-TYPE of NIL.
127 It returns either a character or :EOF. The default method calls
128 STREAM-READ-CHAR and STREAM-UNREAD-CHAR."))
130 (defmethod stream-peek-char ((stream fundamental-character-input-stream))
131 (let ((char (stream-read-char stream)))
132 (unless (eq char :eof)
133 (stream-unread-char stream char))
136 (defgeneric stream-listen (stream)
139 "This is used by LISTEN. It returns true or false. The default method uses
140 STREAM-READ-CHAR-NO-HANG and STREAM-UNREAD-CHAR. Most streams should
141 define their own method since it will usually be trivial and will
142 always be more efficient than the default method."))
144 (defmethod stream-listen ((stream fundamental-character-input-stream))
145 (let ((char (stream-read-char-no-hang stream)))
146 (when (characterp char)
147 (stream-unread-char stream char)
150 (defgeneric stream-read-line (stream)
153 "This is used by READ-LINE. A string is returned as the first value. The
154 second value is true if the string was terminated by end-of-file
155 instead of the end of a line. The default method uses repeated
156 calls to STREAM-READ-CHAR."))
158 (defmethod stream-read-line ((stream fundamental-character-input-stream))
159 (let ((res (make-string 80))
163 (let ((ch (stream-read-char stream)))
165 (return (values (shrink-vector res index) t)))
167 (when (char= ch #\newline)
168 (return (values (shrink-vector res index) nil)))
171 (let ((new (make-string len)))
174 (setf (schar res index) ch)
177 (defgeneric stream-clear-input (stream)
180 "This is like CL:CLEAR-INPUT, but for Gray streams, returning NIL.
181 The default method does nothing."))
183 (defmethod stream-clear-input ((stream fundamental-character-input-stream))
186 (defgeneric stream-read-sequence (stream seq &optional start end)
188 "This is like CL:READ-SEQUENCE, but for Gray streams."))
190 ;;; Destructively modify SEQ by reading elements from STREAM. That
191 ;;; part of SEQ bounded by START and END is destructively modified by
192 ;;; copying successive elements into it from STREAM. If the end of
193 ;;; file for STREAM is reached before copying all elements of the
194 ;;; subsequence, then the extra elements near the end of sequence are
195 ;;; not updated, and the index of the next element is returned.
196 (defun basic-io-type-stream-read-sequence (stream seq start end read-fun)
197 (declare (type sequence seq)
200 (type sequence-end end)
201 (type function read-fun)
203 (let ((end (or end (length seq))))
204 (declare (type index end))
207 (do ((rem (nthcdr start seq) (rest rem))
209 ((or (endp rem) (>= i end)) i)
210 (declare (type list rem)
212 (let ((el (funcall read-fun stream)))
215 (setf (first rem) el))))
217 (with-array-data ((data seq) (offset-start start) (offset-end end))
218 (do ((i offset-start (1+ i)))
219 ((>= i offset-end) end)
220 (declare (type index i))
221 (let ((el (funcall read-fun stream)))
223 (return (+ start (- i offset-start))))
224 (setf (aref data i) el))))))))
226 (defmethod stream-read-sequence ((stream fundamental-character-input-stream)
228 &optional (start 0) (end nil))
229 (basic-io-type-stream-read-sequence stream seq start end
232 (defmethod stream-read-sequence ((stream fundamental-binary-input-stream)
234 &optional (start 0) (end nil))
235 (basic-io-type-stream-read-sequence stream seq start end
239 ;;; character output streams
241 ;;; A character output stream can be created by defining a class that
242 ;;; includes FUNDAMENTAL-CHARACTER-OUTPUT-STREAM and defining methods
243 ;;; for the generic functions below.
245 (defgeneric stream-write-char (stream character)
248 "Write CHARACTER to STREAM and return CHARACTER. Every
249 subclass of FUNDAMENTAL-CHARACTER-OUTPUT-STREAM must have a method
250 defined for this function."))
252 (defgeneric stream-line-column (stream)
255 "Return the column number where the next character
256 will be written, or NIL if that is not meaningful for this stream.
257 The first column on a line is numbered 0. This function is used in
258 the implementation of PPRINT and the FORMAT ~T directive. For every
259 character output stream class that is defined, a method must be
260 defined for this function, although it is permissible for it to
261 always return NIL."))
263 (defmethod stream-line-column ((stream fundamental-character-output-stream))
266 ;;; STREAM-LINE-LENGTH is a CMU CL extension to Gray streams.
267 ;;; FIXME: Should we support it? Probably not..
268 (defgeneric stream-line-length (stream)
270 (:documentation "Return the stream line length or NIL."))
272 (defmethod stream-line-length ((stream fundamental-character-output-stream))
275 (defgeneric stream-start-line-p (stream)
278 "Is STREAM known to be positioned at the beginning of a line?
279 It is permissible for an implementation to always return
280 NIL. This is used in the implementation of FRESH-LINE. Note that
281 while a value of 0 from STREAM-LINE-COLUMN also indicates the
282 beginning of a line, there are cases where STREAM-START-LINE-P can be
283 meaningfully implemented although STREAM-LINE-COLUMN can't be. For
284 example, for a window using variable-width characters, the column
285 number isn't very meaningful, but the beginning of the line does have
286 a clear meaning. The default method for STREAM-START-LINE-P on class
287 FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses STREAM-LINE-COLUMN, so if
288 that is defined to return NIL, then a method should be provided for
289 either STREAM-START-LINE-P or STREAM-FRESH-LINE."))
291 (defmethod stream-start-line-p ((stream fundamental-character-output-stream))
292 (eql (stream-line-column stream) 0))
294 (defgeneric stream-write-string (stream string &optional start end)
297 "This is used by WRITE-STRING. It writes the string to the stream,
298 optionally delimited by start and end, which default to 0 and NIL.
299 The string argument is returned. The default method provided by
300 FUNDAMENTAL-CHARACTER-OUTPUT-STREAM uses repeated calls to
301 STREAM-WRITE-CHAR."))
303 (defmethod stream-write-string ((stream fundamental-character-output-stream)
304 string &optional (start 0) end)
305 (declare (string string)
307 (let ((end (or end (length string))))
308 (declare (fixnum end))
309 (do ((pos start (1+ pos)))
311 (declare (type index pos))
312 (stream-write-char stream (aref string pos))))
315 (defgeneric stream-terpri (stream)
318 "Writes an end of line, as for TERPRI. Returns NIL. The default
319 method does (STREAM-WRITE-CHAR stream #\NEWLINE)."))
321 (defmethod stream-terpri ((stream fundamental-character-output-stream))
322 (stream-write-char stream #\Newline))
324 (defgeneric stream-fresh-line (stream)
327 "Outputs a new line to the Stream if it is not positioned at the
328 begining of a line. Returns T if it output a new line, nil
329 otherwise. Used by FRESH-LINE. The default method uses
330 STREAM-START-LINE-P and STREAM-TERPRI."))
332 (defmethod stream-fresh-line ((stream fundamental-character-output-stream))
333 (unless (stream-start-line-p stream)
334 (stream-terpri stream)
337 (defgeneric stream-finish-output (stream)
340 "Attempts to ensure that all output sent to the Stream has reached
341 its destination, and only then returns false. Implements
342 FINISH-OUTPUT. The default method does nothing."))
344 (defmethod stream-finish-output ((stream fundamental-output-stream))
347 (defgeneric stream-force-output (stream)
350 "Attempts to force any buffered output to be sent. Implements
351 FORCE-OUTPUT. The default method does nothing."))
353 (defmethod stream-force-output ((stream fundamental-output-stream))
356 (defgeneric stream-clear-output (stream)
359 "This is like CL:CLEAR-OUTPUT, but for Gray streams: clear the given
360 output STREAM. The default method does nothing."))
362 (defmethod stream-clear-output ((stream fundamental-output-stream))
365 (defgeneric stream-advance-to-column (stream column)
368 "Write enough blank space so that the next character will be
369 written at the specified column. Returns true if the operation is
370 successful, or NIL if it is not supported for this stream. This is
371 intended for use by by PPRINT and FORMAT ~T. The default method uses
372 STREAM-LINE-COLUMN and repeated calls to STREAM-WRITE-CHAR with a
373 #\SPACE character; it returns NIL if STREAM-LINE-COLUMN returns NIL."))
375 (defmethod stream-advance-to-column ((stream fundamental-character-output-stream)
377 (let ((current-column (stream-line-column stream)))
379 (let ((fill (- column current-column)))
381 (stream-write-char stream #\Space)))
384 (defgeneric stream-write-sequence (stream seq &optional start end)
386 "This is like CL:WRITE-SEQUENCE, but for Gray streams."))
388 ;;; Write the elements of SEQ bounded by START and END to STREAM.
389 (defun basic-io-type-stream-write-sequence (stream seq start end write-fun)
390 (declare (type sequence seq)
393 (type sequence-end end)
394 (type function write-fun)
396 (let ((end (or end (length seq))))
397 (declare (type index start end))
400 (do ((rem (nthcdr start seq) (rest rem))
402 ((or (endp rem) (>= i end)) seq)
403 (declare (type list rem)
405 (funcall write-fun stream (first rem))))
407 (do ((i start (1+ i)))
409 (declare (type index i))
410 (funcall write-fun stream (aref seq i)))))))
412 (defmethod stream-write-sequence ((stream fundamental-character-output-stream)
414 &optional (start 0) (end nil))
417 (stream-write-string stream seq start end))
419 (basic-io-type-stream-write-sequence stream seq start end
420 #'stream-write-char))))
425 ;;; Binary streams can be created by defining a class that includes
426 ;;; either FUNDAMENTAL-BINARY-INPUT-STREAM or
427 ;;; FUNDAMENTAL-BINARY-OUTPUT-STREAM (or both) and defining a method
428 ;;; for STREAM-ELEMENT-TYPE and for one or both of the following
429 ;;; generic functions.
431 (defgeneric stream-read-byte (stream)
434 "Used by READ-BYTE; returns either an integer, or the symbol :EOF
435 if the stream is at end-of-file."))
437 (defgeneric stream-write-byte (stream integer)
440 "Implements WRITE-BYTE; writes the integer to the stream and
441 returns the integer as the result."))
443 ;; Provide a reasonable default for binary Gray streams. We might be
444 ;; able to do better by specializing on the sequence type, but at
445 ;; least the behaviour is reasonable. --tony 2003/05/08.
446 (defmethod stream-write-sequence ((stream fundamental-binary-output-stream)
448 &optional (start 0) (end nil))
449 (basic-io-type-stream-write-sequence stream seq start end
450 #'stream-write-byte))
453 ;;; This is not in the Gray stream proposal, so it is left here
456 ;;; example character output stream encapsulating a lisp-stream
457 (defun make-character-output-stream (lisp-stream)
458 (declare (type lisp-stream lisp-stream))
459 (make-instance 'character-output-stream :lisp-stream lisp-stream))
461 (defmethod open-stream-p ((stream character-output-stream))
462 (open-stream-p (character-output-stream-lisp-stream stream)))
464 (defmethod close ((stream character-output-stream) &key abort)
465 (close (character-output-stream-lisp-stream stream) :abort abort))
467 (defmethod input-stream-p ((stream character-output-stream))
468 (input-stream-p (character-output-stream-lisp-stream stream)))
470 (defmethod output-stream-p ((stream character-output-stream))
471 (output-stream-p (character-output-stream-lisp-stream stream)))
473 (defmethod stream-write-char ((stream character-output-stream) character)
474 (write-char character (character-output-stream-lisp-stream stream)))
476 (defmethod stream-line-column ((stream character-output-stream))
477 (charpos (character-output-stream-lisp-stream stream)))
479 (defmethod stream-line-length ((stream character-output-stream))
480 (line-length (character-output-stream-lisp-stream stream)))
482 (defmethod stream-finish-output ((stream character-output-stream))
483 (finish-output (character-output-stream-lisp-stream stream)))
485 (defmethod stream-force-output ((stream character-output-stream))
486 (force-output (character-output-stream-lisp-stream stream)))
488 (defmethod stream-clear-output ((stream character-output-stream))
489 (clear-output (character-output-stream-lisp-stream stream)))
491 ;;; example character input stream encapsulating a lisp-stream
493 (defun make-character-input-stream (lisp-stream)
494 (declare (type lisp-stream lisp-stream))
495 (make-instance 'character-input-stream :lisp-stream lisp-stream))
497 (defmethod open-stream-p ((stream character-input-stream))
498 (open-stream-p (character-input-stream-lisp-stream stream)))
500 (defmethod close ((stream character-input-stream) &key abort)
501 (close (character-input-stream-lisp-stream stream) :abort abort))
503 (defmethod input-stream-p ((stream character-input-stream))
504 (input-stream-p (character-input-stream-lisp-stream stream)))
506 (defmethod output-stream-p ((stream character-input-stream))
507 (output-stream-p (character-input-stream-lisp-stream stream)))
509 (defmethod stream-read-char ((stream character-input-stream))
510 (read-char (character-input-stream-lisp-stream stream) nil :eof))
512 (defmethod stream-unread-char ((stream character-input-stream) character)
513 (unread-char character (character-input-stream-lisp-stream stream)))
515 (defmethod stream-read-char-no-hang ((stream character-input-stream))
516 (read-char-no-hang (character-input-stream-lisp-stream stream) nil :eof))
519 (defmethod stream-peek-char ((stream character-input-stream))
520 (peek-char nil (character-input-stream-lisp-stream stream) nil :eof))
523 (defmethod stream-listen ((stream character-input-stream))
524 (listen (character-input-stream-lisp-stream stream)))
526 (defmethod stream-clear-input ((stream character-input-stream))
527 (clear-input (character-input-stream-lisp-stream stream)))