output.lisp - clic - Clic is an command line interactive client for gopher written in Common LISP
 (HTM) git clone git://bitreich.org/clic/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/clic/
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
       output.lisp (7034B)
       ---
            1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: FLEXI-STREAMS; Base: 10 -*-
            2 ;;; $Header: /usr/local/cvsrep/flexi-streams/output.lisp,v 1.65 2008/05/24 23:15:25 edi Exp $
            3 
            4 ;;; Copyright (c) 2005-2008, Dr. Edmund Weitz.  All rights reserved.
            5 
            6 ;;; Redistribution and use in source and binary forms, with or without
            7 ;;; modification, are permitted provided that the following conditions
            8 ;;; are met:
            9 
           10 ;;;   * Redistributions of source code must retain the above copyright
           11 ;;;     notice, this list of conditions and the following disclaimer.
           12 
           13 ;;;   * Redistributions in binary form must reproduce the above
           14 ;;;     copyright notice, this list of conditions and the following
           15 ;;;     disclaimer in the documentation and/or other materials
           16 ;;;     provided with the distribution.
           17 
           18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
           19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
           20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
           21 ;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
           22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
           23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
           24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
           25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
           26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
           27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
           28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
           29 
           30 (in-package :flexi-streams)
           31 
           32 (defgeneric write-byte* (byte stream)
           33   (declare #.*standard-optimize-settings*)
           34   (:documentation "Writes one byte \(octet) to the underlying stream
           35 STREAM."))
           36 
           37 #-:lispworks
           38 (defmethod write-byte* (byte (flexi-output-stream flexi-output-stream))  
           39   (declare #.*standard-optimize-settings*)
           40   (with-accessors ((stream flexi-stream-stream))
           41       flexi-output-stream
           42     (write-byte byte stream)))
           43 
           44 #+:lispworks
           45 (defmethod write-byte* (byte (flexi-output-stream flexi-output-stream))
           46   (declare #.*standard-optimize-settings*)
           47   (with-accessors ((stream flexi-stream-stream))
           48       flexi-output-stream
           49     (write-byte byte stream)))
           50 
           51 #+:lispworks
           52 (defmethod write-byte* (byte (flexi-output-stream flexi-char-output-stream))
           53   "This method is only used for LispWorks bivalent streams which
           54 aren't binary."
           55   (declare #.*standard-optimize-settings*)
           56   ;; we use WRITE-SEQUENCE because WRITE-BYTE doesn't work with all
           57   ;; bivalent streams in LispWorks (4.4.6)
           58   (with-accessors ((stream flexi-stream-stream))
           59       flexi-output-stream
           60     (write-sequence (make-array 1 :element-type 'octet
           61                                 :initial-element byte)
           62                     stream)
           63     byte))
           64 
           65 (defmethod stream-write-char ((stream flexi-output-stream) char)
           66   (declare #.*standard-optimize-settings*)
           67   (with-accessors ((external-format flexi-stream-external-format))
           68       stream
           69     (flet ((writer (octet)
           70              (write-byte* octet stream)))
           71       (declare (dynamic-extent (function writer)))
           72       (char-to-octets external-format char #'writer))))
           73 
           74 (defmethod stream-write-char :after ((stream flexi-output-stream) char)
           75   (declare #.*standard-optimize-settings*)
           76   ;; update the column unless we're in the middle of the line and
           77   ;; the current value is NIL
           78   (with-accessors ((column flexi-stream-column))
           79       stream
           80     (cond ((char= char #\Newline) (setq column 0))
           81           (column (incf (the integer column))))))
           82 
           83 (defmethod stream-clear-output ((flexi-output-stream flexi-output-stream))
           84   "Simply calls the corresponding method for the underlying
           85 output stream."
           86   (declare #.*standard-optimize-settings*)
           87   (with-accessors ((stream flexi-stream-stream))
           88       flexi-output-stream
           89     (clear-output stream)))
           90 
           91 (defmethod stream-finish-output ((flexi-output-stream flexi-output-stream))
           92   "Simply calls the corresponding method for the underlying
           93 output stream."
           94   (declare #.*standard-optimize-settings*)
           95   (with-accessors ((stream flexi-stream-stream))
           96       flexi-output-stream
           97     (finish-output stream)))
           98 
           99 (defmethod stream-force-output ((flexi-output-stream flexi-output-stream))
          100   "Simply calls the corresponding method for the underlying
          101 output stream."
          102   (declare #.*standard-optimize-settings*)
          103   (with-accessors ((stream flexi-stream-stream))
          104       flexi-output-stream
          105     (force-output stream)))
          106 
          107 (defmethod stream-line-column ((flexi-output-stream flexi-output-stream))
          108   "Returns the column stored in the COLUMN slot of the
          109 FLEXI-OUTPUT-STREAM object STREAM."
          110   (declare #.*standard-optimize-settings*)
          111   (with-accessors ((column flexi-stream-column))
          112       flexi-output-stream
          113     column))
          114 
          115 (defmethod stream-write-byte ((flexi-output-stream flexi-output-stream) byte)
          116   "Writes a byte \(octet) to the underlying stream."
          117   (declare #.*standard-optimize-settings*)
          118   (with-accessors ((column flexi-stream-column))
          119       flexi-output-stream
          120     ;; set column to NIL because we don't know how to handle binary
          121     ;; output mixed with character output
          122     (setq column nil)
          123     (write-byte* byte flexi-output-stream)))
          124 
          125 #+:allegro
          126 (defmethod stream-terpri ((stream flexi-output-stream))
          127   "Writes a #\Newline character to the underlying stream."
          128   (declare #.*standard-optimize-settings*)
          129   ;; needed for AllegroCL - grrr...
          130   (stream-write-char stream #\Newline))
          131 
          132 (defmethod stream-write-sequence ((flexi-output-stream flexi-output-stream) sequence start end &key)
          133   "An optimized version which uses a buffer underneath.  The function
          134 can accepts characters as well as octets and it decides what to do
          135 based on the element type of the sequence \(if possible) or on the
          136 individual elements, i.e. you can mix characters and octets in
          137 SEQUENCE if you want.  Whether that really works might also depend on
          138 your Lisp, some of the implementations are more picky than others."
          139   (declare #.*standard-optimize-settings*)
          140   (declare (fixnum start end))
          141   (with-accessors ((column flexi-stream-column)
          142                    (external-format flexi-stream-external-format)
          143                    (stream flexi-stream-stream))
          144       flexi-output-stream
          145     (when (>= start end)
          146       (return-from stream-write-sequence sequence))
          147     (when (and (vectorp sequence)
          148                (subtypep (array-element-type sequence) 'integer))
          149       ;; if this is pure binary output, just send all the stuff to the
          150       ;; underlying stream directly and skip the rest
          151       (setq column nil)
          152       (return-from stream-write-sequence
          153         (write-sequence sequence stream :start start :end end)))
          154     ;; otherwise hand over to the external format to do the work
          155     (write-sequence* external-format flexi-output-stream sequence start end))
          156   sequence)
          157 
          158 (defmethod stream-write-string ((stream flexi-output-stream) string
          159                                 &optional (start 0) (end (length string)))
          160   "Simply hands over to the optimized method for STREAM-WRITE-SEQUENCE."
          161   (declare #.*standard-optimize-settings*)
          162   (stream-write-sequence stream string start (or end (length string))))