NextPrevUpTopContentsIndex

21.2.5 Stream output

The following definition for the stream-write-char uses write-char to write a character to the stream. If the character written to unicode-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 function stream-write-char writes character to stream . Every subclass of fundamental-character-output-stream must have a method defined for this function.

The default method for stream-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 for stream-terpri , as the default uses stream-write-char , and therefore works appropriately

To be useful, the stream-line-column and stream-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 return nil :

(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 function stream-line-column returns the column number where the next character will be written from stream , or nil if this is not meaningful for the stream. This function is used in the implementation of print and the format ~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 return nil .

stream-start-line-p

Generic Function

stream-start-line-p stream

Package: stream

The generic function stream-start-line-p returns t if stream is positioned at the beginning of a line, and nil otherwise. It is permissible to define a method that always returns nil .

Finally, the methods for stream-force-output , stream-finish-output and stream-clear-output are "trampolined" from the standard force-output , finish-output and clear-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 - 11 Mar 2008

NextPrevUpTopContentsIndex