mixin.lisp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 excl: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. (defmethod ccl:stream-read-list ((s trivial-gray-stream-mixin) list count)
  86. (stream-read-sequence s list 0 count))
  87. (defmethod ccl:stream-write-list ((s trivial-gray-stream-mixin) list count)
  88. (stream-write-sequence s list 0 count))
  89. (defmethod ccl::stream-position ((stream trivial-gray-stream-mixin) &optional new-position)
  90. (if new-position
  91. (setf (stream-file-position stream) new-position)
  92. (stream-file-position stream))))
  93. ;; up to version 2.43 there were no
  94. ;; stream-read-sequence, stream-write-sequence
  95. ;; functions in CLISP
  96. #+clisp
  97. (eval-when (:compile-toplevel :load-toplevel :execute)
  98. (when (find-symbol "STREAM-READ-SEQUENCE" "GRAY")
  99. (pushnew :clisp-has-stream-read/write-sequence *features*)))
  100. #+clisp
  101. (progn
  102. #+clisp-has-stream-read/write-sequence
  103. (defmethod gray:stream-read-sequence
  104. (seq (s trivial-gray-stream-mixin) &key start end)
  105. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  106. #+clisp-has-stream-read/write-sequence
  107. (defmethod gray:stream-write-sequence
  108. (seq (s trivial-gray-stream-mixin) &key start end)
  109. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  110. ;; Even despite the stream-read/write-sequence are present in newer
  111. ;; CLISP, it's better to provide stream-(read/write)-(byte/char)-sequence
  112. ;; methods too.
  113. ;; Example: if fundamental-binary-input-stream comes in the
  114. ;; class precedence list of your user-defined stream before
  115. ;; the trivial-gray-steam-mixin, the default CLISP's implementation
  116. ;; of the gray:stream-read-sequence will be used; and this default
  117. ;; implementation calls the gray:stream-read-byte-sequence.
  118. ;; Therefore we override gray:stream-read-byte-sequence and call
  119. ;; our stream-read-sequence.
  120. (defmethod gray:stream-read-byte-sequence
  121. ((s trivial-gray-stream-mixin)
  122. seq
  123. &optional start end no-hang interactive)
  124. (when no-hang
  125. (error "this stream does not support the NO-HANG argument"))
  126. (when interactive
  127. (error "this stream does not support the INTERACTIVE argument"))
  128. (stream-read-sequence s seq start end))
  129. (defmethod gray:stream-write-byte-sequence
  130. ((s trivial-gray-stream-mixin)
  131. seq
  132. &optional start end no-hang interactive)
  133. (when no-hang
  134. (error "this stream does not support the NO-HANG argument"))
  135. (when interactive
  136. (error "this stream does not support the INTERACTIVE argument"))
  137. (stream-write-sequence s seq start end))
  138. (defmethod gray:stream-read-char-sequence
  139. ((s trivial-gray-stream-mixin) seq &optional start end)
  140. (stream-read-sequence s seq start end))
  141. (defmethod gray:stream-write-char-sequence
  142. ((s trivial-gray-stream-mixin) seq &optional start end)
  143. (stream-write-sequence s seq start end))
  144. (defmethod gray:stream-position ((stream trivial-gray-stream-mixin) position)
  145. (if position
  146. (setf (stream-file-position stream) position)
  147. (stream-file-position stream))))
  148. #+sbcl
  149. (progn
  150. (defmethod sb-gray:stream-read-sequence
  151. ((s trivial-gray-stream-mixin) seq &optional start end)
  152. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  153. (defmethod sb-gray:stream-write-sequence
  154. ((s trivial-gray-stream-mixin) seq &optional start end)
  155. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  156. (defmethod sb-gray:stream-file-position
  157. ((stream trivial-gray-stream-mixin) &optional position)
  158. (if position
  159. (setf (stream-file-position stream) position)
  160. (stream-file-position stream)))
  161. ;; SBCL extension:
  162. (defmethod sb-gray:stream-line-length ((stream trivial-gray-stream-mixin))
  163. 80))
  164. #+ecl
  165. (progn
  166. (defmethod gray:stream-read-sequence
  167. ((s trivial-gray-stream-mixin) seq &optional start end)
  168. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  169. (defmethod gray:stream-write-sequence
  170. ((s trivial-gray-stream-mixin) seq &optional start end)
  171. (stream-write-sequence s seq (or start 0) (or end (length seq)))))
  172. #+mocl
  173. (progn
  174. (defmethod gray:stream-read-sequence
  175. ((s trivial-gray-stream-mixin) seq &optional start end)
  176. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  177. (defmethod gray:stream-write-sequence
  178. ((s trivial-gray-stream-mixin) seq &optional start end)
  179. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  180. (defmethod gray:stream-file-position
  181. ((stream trivial-gray-stream-mixin) &optional position)
  182. (if position
  183. (setf (stream-file-position stream) position)
  184. (stream-file-position stream))))