mixin.lisp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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:find-symbol "STREAM-FILE-POSITION" :gray-streams) '(cl:and) '(cl:or))
  35. (defmethod gray-streams:stream-file-position
  36. ((s trivial-gray-stream-mixin) &optional position)
  37. (if position
  38. (setf (stream-file-position s) position)
  39. (stream-file-position s))))
  40. #+allegro
  41. (progn
  42. (defmethod excl:stream-read-sequence
  43. ((s trivial-gray-stream-mixin) seq &optional start end)
  44. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  45. (defmethod stream:stream-write-sequence
  46. ((s trivial-gray-stream-mixin) seq &optional start end)
  47. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  48. (defmethod excl::stream-file-position
  49. ((stream trivial-gray-stream-mixin) &optional position)
  50. (if position
  51. (setf (stream-file-position stream) position)
  52. (stream-file-position stream))))
  53. #+cmu
  54. (progn
  55. (defmethod ext:stream-read-sequence
  56. ((s trivial-gray-stream-mixin) seq &optional start end)
  57. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  58. (defmethod ext:stream-write-sequence
  59. ((s trivial-gray-stream-mixin) seq &optional start end)
  60. (stream-write-sequence s seq (or start 0) (or end (length seq)))))
  61. #+lispworks
  62. (progn
  63. (defmethod stream:stream-read-sequence
  64. ((s trivial-gray-stream-mixin) seq start end)
  65. (stream-read-sequence s seq start end))
  66. (defmethod stream:stream-write-sequence
  67. ((s trivial-gray-stream-mixin) seq start end)
  68. (stream-write-sequence s seq start end))
  69. (defmethod stream:stream-file-position ((stream trivial-gray-stream-mixin))
  70. (stream-file-position stream))
  71. (defmethod (setf stream:stream-file-position)
  72. (newval (stream trivial-gray-stream-mixin))
  73. (setf (stream-file-position stream) newval)))
  74. #+openmcl
  75. (progn
  76. (defmethod ccl:stream-read-vector
  77. ((s trivial-gray-stream-mixin) seq start end)
  78. (stream-read-sequence s seq start end))
  79. (defmethod ccl:stream-write-vector
  80. ((s trivial-gray-stream-mixin) seq start end)
  81. (stream-write-sequence s seq start end)))
  82. ;; up to version 2.43 there were no
  83. ;; stream-read-sequence, stream-write-sequence
  84. ;; functions in CLISP
  85. #+clisp
  86. (eval-when (:compile-toplevel :load-toplevel :execute)
  87. (when (find-symbol "STREAM-READ-SEQUENCE" "GRAY")
  88. (pushnew :clisp-has-stream-read/write-sequence *features*)))
  89. #+clisp
  90. (progn
  91. #+clisp-has-stream-read/write-sequence
  92. (defmethod gray:stream-read-sequence
  93. (seq (s trivial-gray-stream-mixin) &key start end)
  94. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  95. #+clisp-has-stream-read/write-sequence
  96. (defmethod gray:stream-write-sequence
  97. (seq (s trivial-gray-stream-mixin) &key start end)
  98. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  99. ;; Even despite the stream-read/write-sequence are present in newer
  100. ;; CLISP, it's better to provide stream-(read/write)-(byte/char)-sequence
  101. ;; methods too.
  102. ;; Example: if fundamental-binary-input-stream comes in the
  103. ;; class precedence list of your user-defined stream before
  104. ;; the trivial-gray-steam-mixin, the default CLISP's implementation
  105. ;; of the gray:stream-read-sequence will be used; and this default
  106. ;; implementation calls the gray:stream-read-byte-sequence.
  107. ;; Therefore we override gray:stream-read-byte-sequence and call
  108. ;; our stream-read-sequence.
  109. (defmethod gray:stream-read-byte-sequence
  110. ((s trivial-gray-stream-mixin)
  111. seq
  112. &optional start end no-hang interactive)
  113. (when no-hang
  114. (error "this stream does not support the NO-HANG argument"))
  115. (when interactive
  116. (error "this stream does not support the INTERACTIVE argument"))
  117. (stream-read-sequence s seq start end))
  118. (defmethod gray:stream-write-byte-sequence
  119. ((s trivial-gray-stream-mixin)
  120. seq
  121. &optional start end no-hang interactive)
  122. (when no-hang
  123. (error "this stream does not support the NO-HANG argument"))
  124. (when interactive
  125. (error "this stream does not support the INTERACTIVE argument"))
  126. (stream-write-sequence s seq start end))
  127. (defmethod gray:stream-read-char-sequence
  128. ((s trivial-gray-stream-mixin) seq &optional start end)
  129. (stream-read-sequence s seq start end))
  130. (defmethod gray:stream-write-char-sequence
  131. ((s trivial-gray-stream-mixin) seq &optional start end)
  132. (stream-write-sequence s seq start end))
  133. (defmethod gray:stream-position ((stream trivial-gray-stream-mixin) position)
  134. (if position
  135. (setf (stream-file-position stream) position)
  136. (stream-file-position stream))))
  137. #+sbcl
  138. (progn
  139. (defmethod sb-gray:stream-read-sequence
  140. ((s trivial-gray-stream-mixin) seq &optional start end)
  141. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  142. (defmethod sb-gray:stream-write-sequence
  143. ((s trivial-gray-stream-mixin) seq &optional start end)
  144. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  145. (defmethod sb-gray:stream-file-position
  146. ((stream trivial-gray-stream-mixin) &optional position)
  147. (if position
  148. (setf (stream-file-position stream) position)
  149. (stream-file-position stream)))
  150. ;; SBCL extension:
  151. (defmethod sb-gray:stream-line-length ((stream trivial-gray-stream-mixin))
  152. 80))
  153. #+ecl
  154. (progn
  155. (defmethod gray:stream-read-sequence
  156. ((s trivial-gray-stream-mixin) seq &optional start end)
  157. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  158. (defmethod gray:stream-write-sequence
  159. ((s trivial-gray-stream-mixin) seq &optional start end)
  160. (stream-write-sequence s seq (or start 0) (or end (length seq)))))