0.pre7.75:
[sbcl.git] / src / pcl / gray-streams.lisp
index 8afe99d..02a3a06 100644 (file)
@@ -1,12 +1,13 @@
 ;;;; Gray streams implementation for SBCL, based on the Gray streams
-;;;; implementation for CMU CL, based on the stream-definition-by-user proposal
-;;;; by David N. Gray.
+;;;; implementation for CMU CL, based on the stream-definition-by-user
+;;;; proposal by David N. Gray.
 
 ;;;; This software is part of the SBCL system. See the README file for
 ;;;; more information.
 
-;;;; This software is in the public domain and is provided with absolutely no
-;;;; warranty. See the COPYING and CREDITS files for more information.
+;;;; This software is in the public domain and is provided with
+;;;; absolutely no warranty. See the COPYING and CREDITS files for
+;;;; more information.
 
 (in-package "SB-GRAY")
 \f
 (defgeneric stream-element-type (stream)
   #+sb-doc
   (:documentation
-   "Returns a type specifier for the kind of object returned by the
-  Stream. Class FUNDAMENTAL-CHARACTER-STREAM provides a default method
+   "Return a type specifier for the kind of object returned by the
+  STREAM. The class FUNDAMENTAL-CHARACTER-STREAM provides a default method
   which returns CHARACTER."))
 
-(defmethod stream-element-type ((stream lisp-stream))
-  (funcall (lisp-stream-misc stream) stream :element-type))
+(defmethod stream-element-type ((stream ansi-stream))
+  (funcall (ansi-stream-misc stream) stream :element-type))
 
 (defmethod stream-element-type ((stream fundamental-character-stream))
   'character)
 (defgeneric pcl-open-stream-p (stream)
   #+sb-doc
   (:documentation
-   "Return true if Stream is not closed. A default method is provided
+   "Return true if STREAM is not closed. A default method is provided
   by class FUNDAMENTAL-STREAM which returns true if CLOSE has not been
   called on the stream."))
 
-(defmethod pcl-open-stream-p ((stream lisp-stream))
-  (not (eq (lisp-stream-in stream) #'closed-flame)))
+(defmethod pcl-open-stream-p ((stream ansi-stream))
+  (not (eq (ansi-stream-in stream) #'closed-flame)))
 
 (defmethod pcl-open-stream-p ((stream fundamental-stream))
   (stream-open-p stream))
 (defgeneric pcl-close (stream &key abort)
   #+sb-doc
   (:documentation
-   "Closes the given Stream. No more I/O may be performed, but
-  inquiries may still be made. If :Abort is non-nil, an attempt is made
+   "Close the given STREAM. No more I/O may be performed, but
+  inquiries may still be made. If :ABORT is true, an attempt is made
   to clean up the side effects of having created the stream."))
 
-(defmethod pcl-close ((stream lisp-stream) &key abort)
+(defmethod pcl-close ((stream ansi-stream) &key abort)
   (when (open-stream-p stream)
-    (funcall (lisp-stream-misc stream) stream :close abort))
+    (funcall (ansi-stream-misc stream) stream :close abort))
   t)
 
 (defmethod pcl-close ((stream fundamental-stream) &key abort)
+  (declare (ignore abort))
   (setf (stream-open-p stream) nil)
   t)
 
 
 (defgeneric input-stream-p (stream)
   #+sb-doc
-  (:documentation "Return non-nil if the given Stream can perform input operations."))
+  (:documentation "Can STREAM perform input operations?"))
 
-(defmethod input-stream-p ((stream lisp-stream))
-  (and (not (eq (lisp-stream-in stream) #'closed-flame))
-       (or (not (eq (lisp-stream-in stream) #'ill-in))
-          (not (eq (lisp-stream-bin stream) #'ill-bin)))))
+(defmethod input-stream-p ((stream ansi-stream))
+  (and (not (eq (ansi-stream-in stream) #'closed-flame))
+       (or (not (eq (ansi-stream-in stream) #'ill-in))
+          (not (eq (ansi-stream-bin stream) #'ill-bin)))))
 
 (defmethod input-stream-p ((stream fundamental-input-stream))
   t)
 
 (defgeneric output-stream-p (stream)
   #+sb-doc
-  (:documentation "Return non-nil if the given Stream can perform output operations."))
+  (:documentation "Can STREAM perform output operations?"))
 
-(defmethod output-stream-p ((stream lisp-stream))
-  (and (not (eq (lisp-stream-in stream) #'closed-flame))
-       (or (not (eq (lisp-stream-out stream) #'ill-out))
-          (not (eq (lisp-stream-bout stream) #'ill-bout)))))
+(defmethod output-stream-p ((stream ansi-stream))
+  (and (not (eq (ansi-stream-in stream) #'closed-flame))
+       (or (not (eq (ansi-stream-out stream) #'ill-out))
+          (not (eq (ansi-stream-bout stream) #'ill-bout)))))
 
 (defmethod output-stream-p ((stream fundamental-output-stream))
   t)
@@ -97,7 +99,7 @@
 (defgeneric stream-read-char (stream)
   #+sb-doc
   (:documentation
-   "This reads one character from the stream. It returns either a
+   "Read one character from the stream. Return either a
   character object, or the symbol :EOF if the stream is at end-of-file.
   Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM must define a
   method for this function."))
 (defgeneric stream-unread-char (stream character)
   #+sb-doc
   (:documentation
-   "Un-does the last call to STREAM-READ-CHAR, as in UNREAD-CHAR.
-  Returns NIL. Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM
+   "Un-do the last call to STREAM-READ-CHAR, as in UNREAD-CHAR.
+  Return NIL. Every subclass of FUNDAMENTAL-CHARACTER-INPUT-STREAM
   must define a method for this function."))
 
 (defgeneric stream-read-char-no-hang (stream)
 (defgeneric stream-peek-char (stream)
   #+sb-doc
   (:documentation
-   "Used to implement PEEK-CHAR; this corresponds to peek-type of NIL.
+   "This is used to implement PEEK-CHAR; this corresponds to PEEK-TYPE of NIL.
   It returns either a character or :EOF. The default method calls
   STREAM-READ-CHAR and STREAM-UNREAD-CHAR."))
 
 (defgeneric stream-listen (stream)
   #+sb-doc
   (:documentation
-   "Used by LISTEN. Returns true or false. The default method uses
+   "This is used by LISTEN. It returns true or false. The default method uses
   STREAM-READ-CHAR-NO-HANG and STREAM-UNREAD-CHAR. Most streams should
   define their own method since it will usually be trivial and will
   always be more efficient than the default method."))
 (defgeneric stream-read-line (stream)
   #+sb-doc
   (:documentation
-   "Used by READ-LINE. A string is returned as the first value. The
+   "This is used by READ-LINE. A string is returned as the first value. The
   second value is true if the string was terminated by end-of-file
   instead of the end of a line. The default method uses repeated
   calls to STREAM-READ-CHAR."))
 (defgeneric stream-clear-input (stream)
   #+sb-doc
   (:documentation
-   "Implements CLEAR-INPUT for the stream, returning NIL. The default
-  method does nothing."))
+   "This is like CL:CLEAR-INPUT, but for Gray streams, returning NIL.
+  The default method does nothing."))
 
 (defmethod stream-clear-input ((stream fundamental-character-input-stream))
   nil)
+
+(defgeneric stream-read-sequence (stream seq &optional start end)
+  (:documentation
+   "This is like CL:READ-SEQUENCE, but for Gray streams."))
+
+;;; Destructively modify SEQ by reading elements from STREAM. That
+;;; part of SEQ bounded by START and END is destructively modified by
+;;; copying successive elements into it from STREAM. If the end of
+;;; file for STREAM is reached before copying all elements of the
+;;; subsequence, then the extra elements near the end of sequence are
+;;; not updated, and the index of the next element is returned.
+(defun basic-io-type-stream-read-sequence (stream seq start end read-fun)
+  (declare (type sequence seq)
+          (type stream stream)
+          (type index start)
+          (type sequence-end end)
+           (type function read-fun)
+          (values index))
+  (let ((end (or end (length seq))))
+    (declare (type index end))
+    (etypecase seq
+      (list
+        (do ((rem (nthcdr start seq) (rest rem))
+             (i start (1+ i)))
+            ((or (endp rem) (>= i end)) i)
+          (declare (type list rem)
+                   (type index i))
+          (let ((el (funcall read-fun stream)))
+            (when (eq el :eof)
+              (return i))
+            (setf (first rem) el))))
+      (vector
+        (with-array-data ((data seq) (offset-start start) (offset-end end))
+          (do ((i offset-start (1+ i)))
+              ((>= i offset-end) end)
+            (declare (type index i))
+            (let ((el (funcall read-fun stream)))
+              (when (eq el :eof)
+                (return (+ start (- i offset-start))))
+              (setf (aref data i) el))))))))
+
+(defmethod stream-read-sequence ((stream fundamental-character-input-stream)
+                                 (seq sequence)
+                                 &optional (start 0) (end nil))
+  (basic-io-type-stream-read-sequence stream seq start end
+                                      #'stream-read-char))
 \f
 ;;; character output streams
 ;;;
 (defgeneric stream-write-char (stream character)
   #+sb-doc
   (:documentation
-   "Writes character to the stream and returns the character. Every
+   "Write CHARACTER to STREAM and return CHARACTER. Every
   subclass of FUNDAMENTAL-CHARACTER-OUTPUT-STREAM must have a method
   defined for this function."))
 
 (defgeneric stream-line-column (stream)
   #+sb-doc
   (:documentation
-   "This function returns the column number where the next character
+   "Return the column number where the next character
   will be written, or NIL if that is not meaningful for this stream.
   The first column on a line is numbered 0. This function is used in
   the implementation of PPRINT and the FORMAT ~T directive. For every
 ;;; FIXME: Should we support it? Probably not..
 (defgeneric stream-line-length (stream)
   #+sb-doc
-  (:documentation "Return the stream line length or Nil."))
+  (:documentation "Return the stream line length or NIL."))
 
 (defmethod stream-line-length ((stream fundamental-character-output-stream))
   nil)
 (defgeneric stream-start-line-p (stream)
   #+sb-doc
   (:documentation
-   "This is a predicate which returns T if the stream is positioned at
-  the beginning of a line, else NIL. It is permissible to always return
+   "Is STREAM known to be positioned at the beginning of a line?
+  It is permissible for an implementation to always return
   NIL. This is used in the implementation of FRESH-LINE. Note that
   while a value of 0 from STREAM-LINE-COLUMN also indicates the
   beginning of a line, there are cases where STREAM-START-LINE-P can be
 (defgeneric stream-clear-output (stream)
   #+sb-doc
   (:documentation
-   "Clears the given output Stream. Implements CLEAR-OUTPUT. The
-  default method does nothing."))
+   "This is like CL:CLEAR-OUTPUT, but for Gray streams: clear the given
+  output STREAM. The default method does nothing."))
 
 (defmethod stream-clear-output ((stream fundamental-output-stream))
   nil)
 (defgeneric stream-advance-to-column (stream column)
   #+sb-doc
   (:documentation
-   "Writes enough blank space so that the next character will be
+   "Write enough blank space so that the next character will be
   written at the specified column. Returns true if the operation is
   successful, or NIL if it is not supported for this stream. This is
   intended for use by by PPRINT and FORMAT ~T. The default method uses
        (dotimes (i fill)
          (stream-write-char stream #\Space)))
       T)))
+
+(defgeneric stream-write-sequence (stream seq &optional start end)
+  (:documentation
+   "This is like CL:WRITE-SEQUENCE, but for Gray streams."))
+
+;;; Write the elements of SEQ bounded by START and END to STREAM.
+(defun basic-io-type-stream-write-sequence (stream seq start end write-fun)
+  (declare (type sequence seq)
+          (type stream stream)
+          (type index start)
+          (type sequence-end end)
+           (type function write-fun)
+          (values sequence))
+  (let ((end (or end (length seq))))
+    (declare (type index start end))
+    (etypecase seq
+      (list
+        (do ((rem (nthcdr start seq) (rest rem))
+             (i start (1+ i)))
+            ((or (endp rem) (>= i end)) seq)
+          (declare (type list rem)
+                   (type index i))
+          (funcall write-fun stream (first rem))))
+      (vector
+        (do ((i start (1+ i)))
+            ((>= i end) seq)
+          (declare (type index i))
+          (funcall write-fun stream (aref seq i)))))))
+
+(defmethod stream-write-sequence ((stream fundamental-character-output-stream)
+                                  (seq sequence)
+                                  &optional (start 0) (end nil))
+  (typecase seq
+    (string
+      (stream-write-string stream seq start end))
+    (t
+      (basic-io-type-stream-write-sequence stream seq start end
+                                           #'stream-write-char))))
+
 \f
 ;;; binary streams
 ;;;
    "Implements WRITE-BYTE; writes the integer to the stream and
   returns the integer as the result."))
 \f
+;;; This is not in the Gray stream proposal, so it is left here
+;;; as example code.
 #|
-This is not in the gray-stream proposal, so it is left here
-as example code.
 ;;; example character output stream encapsulating a lisp-stream
 (defun make-character-output-stream (lisp-stream)
   (declare (type lisp-stream lisp-stream))
@@ -407,7 +494,7 @@ as example code.
   (output-stream-p (character-input-stream-lisp-stream stream)))
 
 (defmethod stream-read-char ((stream character-input-stream))
-  (read-char (character-input-stream-lisp-stream stream)))
+  (read-char (character-input-stream-lisp-stream stream) nil :eof))
 
 (defmethod stream-unread-char ((stream character-input-stream) character)
   (unread-char character (character-input-stream-lisp-stream stream)))