Next Previous Up Top Contents Index

15.2 An illustrative example of user defined streams

15.2.5 Stream output

The following definition for thestream-write-char useswrite-char to write a character to the stream. If the character written tounicode-ls-stream is a#\Newline, then the method writes a#\Line-Separator to the file stream.

(defmethod stream:stream-write-char ((stream unicode-ls-stream) 
                                     char)
  (write-char (if (eq char #\Newline)
                   #\Line-Separator
               char)
              (ls-stream-file-stream stream)))

stream-write-char

Generic Function

stream-write-char stream character

Package:stream
The generic functionstream-write-char writes character to stream. Every subclass offundamental-character-output-stream must have a method defined for this function.

The default method forstream-write-string calls the above generic function and successfully write a string to the stream. However, the following is a more efficient implementation for our stream.

(defmethod stream:stream-write-string ((stream unicode-ls-stream)
                                        string &optional (start 0)
                                        (end (length string)))
  (loop with i = start
        until (>= i end)
        do (let* ((newline (position #\Newline 
                            string :start i :end end))
                  (this-end (or newline end)))
             (write-string string (ls-stream-file-stream stream) 
                           :start i :end this-end)
             (incf i this-end)
             (when newline 
                   (stream:stream-terpri stream)
                   (incf i)))
        finally (return string)))

We do not need to define our own method forstream-terpri, as the default usesstream-write-char, and therefore works appropriately

To be useful, thestream-line-column andstream-line-start-p generic functions need to know the number of characters preceding a#\Line-Separator. However, since the LispWorks file stream records line position only by#\Newline characters, this information is not available. Hence we define the two generic functions to returnnil:

(defmethod stream:stream-line-column 
  ((stream unicode-ls-stream))
  nil)

(defmethod stream:stream-start-line-p ((stream unicode-ls-stream)) nil)

stream-line-column

Generic Function

stream-line-column stream

Package:stream
The generic functionstream-line-column returns the column number where the next character will be written from stream, ornil if this is not meaningful for the stream. This function is used in the implementation ofprint and theformat ~t directive. A method for this function must be defined for every character output stream class that is defined, although at its simplest it may be defined to always returnnil.
stream-start-line-p

Generic Function

stream-start-line-p stream

Package:stream
The generic functionstream-start-line-p returnst if stream is positioned at the beginning of a line, andnil otherwise. It is permissible to define a method that always returnsnil.

Finally, the methods forstream-force-output,stream-finish-output andstream-clear-output are "trampolined" from the standardforce-output,finish-output andclear-output functions.

(defmethod stream:stream-force-output ((stream 
                                        unicode-ls-stream))
  (force-output (ls-stream-file-stream stream)))

(defmethod stream:stream-finish-output ((stream unicode-ls-stream)) (finish-output (ls-stream-file-stream stream)))

(defmethod stream:stream-clear-output ((stream unicode-ls-stream)) (clear-output (ls-stream-file-stream stream)))


LispWorks User Guide - 14 Oct 1998

Next Previous Up Top Contents Index

Generated with Harlequin WebMaker