mixin.lisp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #+xcvb (module (:depends-on ("package")))
  2. (in-package :trivial-gray-streams)
  3. (defclass trivial-gray-stream-mixin () ())
  4. (defgeneric stream-read-sequence
  5. (stream sequence start end &key &allow-other-keys))
  6. (defgeneric stream-write-sequence
  7. (stream sequence start end &key &allow-other-keys))
  8. (defgeneric stream-file-position (stream))
  9. (defgeneric (setf stream-file-position) (newval stream))
  10. (defmethod stream-write-string
  11. ((stream trivial-gray-stream-mixin) seq &optional start end)
  12. (stream-write-sequence stream seq (or start 0) (or end (length seq))))
  13. ;; Implementations should provide this default method, I believe, but
  14. ;; at least sbcl and allegro don't.
  15. (defmethod stream-terpri ((stream trivial-gray-stream-mixin))
  16. (write-char #\newline stream))
  17. (defmethod stream-file-position ((stream trivial-gray-stream-mixin))
  18. nil)
  19. (defmethod (setf stream-file-position)
  20. (newval (stream trivial-gray-stream-mixin))
  21. (declare (ignore newval))
  22. nil)
  23. #+abcl
  24. (progn
  25. (defmethod gray-streams:stream-read-sequence
  26. ((s trivial-gray-stream-mixin) seq &optional start end)
  27. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  28. (defmethod gray-streams:stream-write-sequence
  29. ((s trivial-gray-stream-mixin) seq &optional start end)
  30. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  31. (defmethod gray-streams:stream-write-string
  32. ((stream xp::xp-structure) string &optional (start 0) (end (length string)))
  33. (xp::write-string+ string stream start end))
  34. #+#.(cl:if (cl:and (cl:find-package :gray-streams)
  35. (cl:find-symbol "STREAM-FILE-POSITION" :gray-streams))
  36. '(:and)
  37. '(:or))
  38. (defmethod gray-streams:stream-file-position
  39. ((s trivial-gray-stream-mixin) &optional position)
  40. (if position
  41. (setf (stream-file-position s) position)
  42. (stream-file-position s))))
  43. #+allegro
  44. (progn
  45. (defmethod excl:stream-read-sequence
  46. ((s trivial-gray-stream-mixin) seq &optional start end)
  47. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  48. (defmethod stream:stream-write-sequence
  49. ((s trivial-gray-stream-mixin) seq &optional start end)
  50. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  51. (defmethod excl::stream-file-position
  52. ((stream trivial-gray-stream-mixin) &optional position)
  53. (if position
  54. (setf (stream-file-position stream) position)
  55. (stream-file-position stream))))
  56. #+cmu
  57. (progn
  58. (defmethod ext:stream-read-sequence
  59. ((s trivial-gray-stream-mixin) seq &optional start end)
  60. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  61. (defmethod ext:stream-write-sequence
  62. ((s trivial-gray-stream-mixin) seq &optional start end)
  63. (stream-write-sequence s seq (or start 0) (or end (length seq)))))
  64. #+lispworks
  65. (progn
  66. (defmethod stream:stream-read-sequence
  67. ((s trivial-gray-stream-mixin) seq start end)
  68. (stream-read-sequence s seq start end))
  69. (defmethod stream:stream-write-sequence
  70. ((s trivial-gray-stream-mixin) seq start end)
  71. (stream-write-sequence s seq start end))
  72. (defmethod stream:stream-file-position ((stream trivial-gray-stream-mixin))
  73. (stream-file-position stream))
  74. (defmethod (setf stream:stream-file-position)
  75. (newval (stream trivial-gray-stream-mixin))
  76. (setf (stream-file-position stream) newval)))
  77. #+openmcl
  78. (progn
  79. (defmethod ccl:stream-read-vector
  80. ((s trivial-gray-stream-mixin) seq start end)
  81. (stream-read-sequence s seq start end))
  82. (defmethod ccl:stream-write-vector
  83. ((s trivial-gray-stream-mixin) seq start end)
  84. (stream-write-sequence s seq start end)))
  85. ;; up to version 2.43 there were no
  86. ;; stream-read-sequence, stream-write-sequence
  87. ;; functions in CLISP
  88. #+clisp
  89. (eval-when (:compile-toplevel :load-toplevel :execute)
  90. (when (find-symbol "STREAM-READ-SEQUENCE" "GRAY")
  91. (pushnew :clisp-has-stream-read/write-sequence *features*)))
  92. #+clisp
  93. (progn
  94. #+clisp-has-stream-read/write-sequence
  95. (defmethod gray:stream-read-sequence
  96. (seq (s trivial-gray-stream-mixin) &key start end)
  97. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  98. #+clisp-has-stream-read/write-sequence
  99. (defmethod gray:stream-write-sequence
  100. (seq (s trivial-gray-stream-mixin) &key start end)
  101. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  102. ;; Even despite the stream-read/write-sequence are present in newer
  103. ;; CLISP, it's better to provide stream-(read/write)-(byte/char)-sequence
  104. ;; methods too.
  105. ;; Example: if fundamental-binary-input-stream comes in the
  106. ;; class precedence list of your user-defined stream before
  107. ;; the trivial-gray-steam-mixin, the default CLISP's implementation
  108. ;; of the gray:stream-read-sequence will be used; and this default
  109. ;; implementation calls the gray:stream-read-byte-sequence.
  110. ;; Therefore we override gray:stream-read-byte-sequence and call
  111. ;; our stream-read-sequence.
  112. (defmethod gray:stream-read-byte-sequence
  113. ((s trivial-gray-stream-mixin)
  114. seq
  115. &optional start end no-hang interactive)
  116. (when no-hang
  117. (error "this stream does not support the NO-HANG argument"))
  118. (when interactive
  119. (error "this stream does not support the INTERACTIVE argument"))
  120. (stream-read-sequence s seq start end))
  121. (defmethod gray:stream-write-byte-sequence
  122. ((s trivial-gray-stream-mixin)
  123. seq
  124. &optional start end no-hang interactive)
  125. (when no-hang
  126. (error "this stream does not support the NO-HANG argument"))
  127. (when interactive
  128. (error "this stream does not support the INTERACTIVE argument"))
  129. (stream-write-sequence s seq start end))
  130. (defmethod gray:stream-read-char-sequence
  131. ((s trivial-gray-stream-mixin) seq &optional start end)
  132. (stream-read-sequence s seq start end))
  133. (defmethod gray:stream-write-char-sequence
  134. ((s trivial-gray-stream-mixin) seq &optional start end)
  135. (stream-write-sequence s seq start end))
  136. (defmethod gray:stream-position ((stream trivial-gray-stream-mixin) position)
  137. (if position
  138. (setf (stream-file-position stream) position)
  139. (stream-file-position stream))))
  140. #+sbcl
  141. (progn
  142. (defmethod sb-gray:stream-read-sequence
  143. ((s trivial-gray-stream-mixin) seq &optional start end)
  144. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  145. (defmethod sb-gray:stream-write-sequence
  146. ((s trivial-gray-stream-mixin) seq &optional start end)
  147. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  148. (defmethod sb-gray:stream-file-position
  149. ((stream trivial-gray-stream-mixin) &optional position)
  150. (if position
  151. (setf (stream-file-position stream) position)
  152. (stream-file-position stream)))
  153. ;; SBCL extension:
  154. (defmethod sb-gray:stream-line-length ((stream trivial-gray-stream-mixin))
  155. 80))
  156. #+ecl
  157. (progn
  158. (defmethod gray:stream-read-sequence
  159. ((s trivial-gray-stream-mixin) seq &optional start end)
  160. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  161. (defmethod gray:stream-write-sequence
  162. ((s trivial-gray-stream-mixin) seq &optional start end)
  163. (stream-write-sequence s seq (or start 0) (or end (length seq)))))