streams.lisp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #+xcvb (module (:depends-on ("package")))
  2. (in-package :trivial-gray-streams)
  3. (defclass fundamental-stream (impl-specific-gray:fundamental-stream) ())
  4. (defclass fundamental-input-stream
  5. (fundamental-stream impl-specific-gray:fundamental-input-stream) ())
  6. (defclass fundamental-output-stream
  7. (fundamental-stream impl-specific-gray:fundamental-output-stream) ())
  8. (defclass fundamental-character-stream
  9. (fundamental-stream impl-specific-gray:fundamental-character-stream) ())
  10. (defclass fundamental-binary-stream
  11. (fundamental-stream impl-specific-gray:fundamental-binary-stream) ())
  12. (defclass fundamental-character-input-stream
  13. (fundamental-input-stream fundamental-character-stream
  14. impl-specific-gray:fundamental-character-input-stream) ())
  15. (defclass fundamental-character-output-stream
  16. (fundamental-output-stream fundamental-character-stream
  17. impl-specific-gray:fundamental-character-output-stream) ())
  18. (defclass fundamental-binary-input-stream
  19. (fundamental-input-stream fundamental-binary-stream
  20. impl-specific-gray:fundamental-binary-input-stream) ())
  21. (defclass fundamental-binary-output-stream
  22. (fundamental-output-stream fundamental-binary-stream
  23. impl-specific-gray:fundamental-binary-output-stream) ())
  24. (defgeneric stream-read-sequence
  25. (stream sequence start end &key &allow-other-keys))
  26. (defgeneric stream-write-sequence
  27. (stream sequence start end &key &allow-other-keys))
  28. (defgeneric stream-file-position (stream))
  29. (defgeneric (setf stream-file-position) (newval stream))
  30. (defmethod stream-write-string
  31. ((stream fundamental-output-stream) seq &optional start end)
  32. (stream-write-sequence stream seq (or start 0) (or end (length seq))))
  33. ;; Implementations should provide this default method, I believe, but
  34. ;; at least sbcl and allegro don't.
  35. (defmethod stream-terpri ((stream fundamental-output-stream))
  36. (write-char #\newline stream))
  37. (defmethod stream-file-position ((stream fundamental-stream))
  38. nil)
  39. (defmethod (setf stream-file-position)
  40. (newval (stream fundamental-stream))
  41. (declare (ignore newval))
  42. nil)
  43. #+abcl
  44. (progn
  45. (defmethod gray-streams:stream-read-sequence
  46. ((s fundamental-input-stream) seq &optional start end)
  47. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  48. (defmethod gray-streams:stream-write-sequence
  49. ((s fundamental-output-stream) seq &optional start end)
  50. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  51. (defmethod gray-streams:stream-write-string
  52. ((stream xp::xp-structure) string &optional (start 0) (end (length string)))
  53. (xp::write-string+ string stream start end))
  54. #+#.(cl:if (cl:and (cl:find-package :gray-streams)
  55. (cl:find-symbol "STREAM-FILE-POSITION" :gray-streams))
  56. '(:and)
  57. '(:or))
  58. (defmethod gray-streams:stream-file-position
  59. ((s fundamental-stream) &optional position)
  60. (if position
  61. (setf (stream-file-position s) position)
  62. (stream-file-position s))))
  63. #+allegro
  64. (progn
  65. (defmethod excl:stream-read-sequence
  66. ((s fundamental-input-stream) seq &optional start end)
  67. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  68. (defmethod excl:stream-write-sequence
  69. ((s fundamental-output-stream) seq &optional start end)
  70. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  71. (defmethod excl::stream-file-position
  72. ((stream fundamental-stream) &optional position)
  73. (if position
  74. (setf (stream-file-position stream) position)
  75. (stream-file-position stream))))
  76. #+cmu
  77. (progn
  78. (defmethod ext:stream-read-sequence
  79. ((s fundamental-input-stream) seq &optional start end)
  80. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  81. (defmethod ext:stream-write-sequence
  82. ((s fundamental-output-stream) seq &optional start end)
  83. (stream-write-sequence s seq (or start 0) (or end (length seq)))))
  84. #+lispworks
  85. (progn
  86. (defmethod stream:stream-read-sequence
  87. ((s fundamental-input-stream) seq start end)
  88. (stream-read-sequence s seq start end))
  89. (defmethod stream:stream-write-sequence
  90. ((s fundamental-output-stream) seq start end)
  91. (stream-write-sequence s seq start end))
  92. (defmethod stream:stream-file-position ((stream fundamental-stream))
  93. (stream-file-position stream))
  94. (defmethod (setf stream:stream-file-position)
  95. (newval (stream fundamental-stream))
  96. (setf (stream-file-position stream) newval)))
  97. #+openmcl
  98. (progn
  99. (defmethod ccl:stream-read-vector
  100. ((s fundamental-input-stream) seq start end)
  101. (stream-read-sequence s seq start end))
  102. (defmethod ccl:stream-write-vector
  103. ((s fundamental-output-stream) seq start end)
  104. (stream-write-sequence s seq start end))
  105. (defmethod ccl:stream-read-list ((s fundamental-input-stream) list count)
  106. (stream-read-sequence s list 0 count))
  107. (defmethod ccl:stream-write-list ((s fundamental-output-stream) list count)
  108. (stream-write-sequence s list 0 count))
  109. (defmethod ccl::stream-position ((stream fundamental-stream) &optional new-position)
  110. (if new-position
  111. (setf (stream-file-position stream) new-position)
  112. (stream-file-position stream))))
  113. ;; up to version 2.43 there were no
  114. ;; stream-read-sequence, stream-write-sequence
  115. ;; functions in CLISP
  116. #+clisp
  117. (eval-when (:compile-toplevel :load-toplevel :execute)
  118. (when (find-symbol (string #:stream-read-sequence) #:gray)
  119. (pushnew :clisp-has-stream-read/write-sequence *features*)))
  120. #+clisp
  121. (progn
  122. #+clisp-has-stream-read/write-sequence
  123. (defmethod gray:stream-read-sequence
  124. (seq (s fundamental-input-stream) &key start end)
  125. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  126. #+clisp-has-stream-read/write-sequence
  127. (defmethod gray:stream-write-sequence
  128. (seq (s fundamental-output-stream) &key start end)
  129. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  130. ;;; for old CLISP
  131. (defmethod gray:stream-read-byte-sequence
  132. ((s fundamental-input-stream)
  133. seq
  134. &optional start end no-hang interactive)
  135. (when no-hang
  136. (error "this stream does not support the NO-HANG argument"))
  137. (when interactive
  138. (error "this stream does not support the INTERACTIVE argument"))
  139. (stream-read-sequence s seq start end))
  140. (defmethod gray:stream-write-byte-sequence
  141. ((s fundamental-output-stream)
  142. seq
  143. &optional start end no-hang interactive)
  144. (when no-hang
  145. (error "this stream does not support the NO-HANG argument"))
  146. (when interactive
  147. (error "this stream does not support the INTERACTIVE argument"))
  148. (stream-write-sequence s seq start end))
  149. (defmethod gray:stream-read-char-sequence
  150. ((s fundamental-input-stream) seq &optional start end)
  151. (stream-read-sequence s seq start end))
  152. (defmethod gray:stream-write-char-sequence
  153. ((s fundamental-output-stream) seq &optional start end)
  154. (stream-write-sequence s seq start end))
  155. ;;; end of old CLISP read/write-sequence support
  156. (defmethod gray:stream-position ((stream fundamental-stream) position)
  157. (if position
  158. (setf (stream-file-position stream) position)
  159. (stream-file-position stream))))
  160. #+sbcl
  161. (progn
  162. (defmethod sb-gray:stream-read-sequence
  163. ((s fundamental-input-stream) seq &optional start end)
  164. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  165. (defmethod sb-gray:stream-write-sequence
  166. ((s fundamental-output-stream) seq &optional start end)
  167. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  168. (defmethod sb-gray:stream-file-position
  169. ((stream fundamental-stream) &optional position)
  170. (if position
  171. (setf (stream-file-position stream) position)
  172. (stream-file-position stream)))
  173. ;; SBCL extension:
  174. (defmethod sb-gray:stream-line-length ((stream fundamental-stream))
  175. 80))
  176. #+ecl
  177. (progn
  178. (defmethod gray:stream-read-sequence
  179. ((s fundamental-input-stream) seq &optional start end)
  180. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  181. (defmethod gray:stream-write-sequence
  182. ((s fundamental-output-stream) seq &optional start end)
  183. (stream-write-sequence s seq (or start 0) (or end (length seq)))))
  184. #+mocl
  185. (progn
  186. (defmethod gray:stream-read-sequence
  187. ((s fundamental-input-stream) seq &optional start end)
  188. (stream-read-sequence s seq (or start 0) (or end (length seq))))
  189. (defmethod gray:stream-write-sequence
  190. ((s fundamental-output-stream) seq &optional start end)
  191. (stream-write-sequence s seq (or start 0) (or end (length seq))))
  192. (defmethod gray:stream-file-position
  193. ((stream fundamental-stream) &optional position)
  194. (if position
  195. (setf (stream-file-position stream) position)
  196. (stream-file-position stream))))
  197. ;; deprecated
  198. (defclass trivial-gray-stream-mixin () ())