input-stream, with-input-stream, output-stream, with-output-stream, io-stream, with-io-stream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
diff --git a/fd-gray.lisp b/fd-gray.lisp
index 92bc1e5..5594b98 100644
--- a/fd-gray.lisp
+++ b/fd-gray.lisp
@@ -42,6 +42,8 @@ fd gray streams."))
(:documentation "An error that is signalled when someone is trying
to read from or write to a closed fd gray stream."))
+(defgeneric check-if-open (stream))
+
(defmethod check-if-open ((stream stream))
"Checks if STREAM is open and signals an error otherwise."
(unless (open-stream-p stream)
@@ -75,6 +77,8 @@ to read from or write to a closed fd gray stream."))
(:documentation "An error that is signalled when an input error happens
on a fd gray stream."))
+(defgeneric stream-input (stream))
+
(defmethod stream-input ((stream input-stream))
"Fill buffer with file data.
Tries to read once from input-length to input-max (end of buffer)."
@@ -130,6 +134,16 @@ Tries to read once from input-length to input-max (end of buffer)."
(setf (input-buffer stream) nil)
(unistd:close (stream-fd stream)))
+(defun input-stream (fd)
+ (make-instance 'input-stream :fd fd))
+
+(defmacro with-input-stream ((var fd) &body body)
+ (let ((stream (gensym "STREAM-")))
+ `(let ((,stream (input-stream ,fd)))
+ (unwind-protect (let ((,var ,stream))
+ ,@body)
+ (close ,stream)))))
+
(defclass output-stream (stream fundamental-binary-output-stream)
((output-buffer :initform (cffi:foreign-alloc :unsigned-char
:count *buffer-size*)
@@ -144,6 +158,8 @@ Tries to read once from input-length to input-max (end of buffer)."
:reader output-max
:type fixnum+)))
+(defgeneric stream-output (stream))
+
(defmethod stream-output ((stream output-stream))
"Send buffer data to the file.
Calls write with data ranging from output-index to output-length."
@@ -198,6 +214,16 @@ Calls write with data ranging from output-index to output-length."
(setf (output-buffer stream) nil)
(unistd:close (stream-fd stream)))
+(defun output-stream (fd)
+ (make-instance 'output-stream :fd fd))
+
+(defmacro with-output-stream ((var fd) &body body)
+ (let ((stream (gensym "STREAM-")))
+ `(let ((,stream (output-stream ,fd)))
+ (unwind-protect (let ((,var ,stream))
+ ,@body)
+ (close ,stream)))))
+
(defclass io-stream (input-stream output-stream)
())
@@ -210,10 +236,12 @@ Calls write with data ranging from output-index to output-length."
(setf (output-buffer stream) nil)
(unistd:close (stream-fd stream)))
-(defun make-stream (fd)
+(defun io-stream (fd)
(make-instance 'io-stream :fd fd))
-(defmacro with-stream ((var fd) &body body)
- `(let ((,var (make-stream ,fd)))
- (unwind-protect (progn ,@body)
- (close ,var))))
+(defmacro with-io-stream ((var fd) &body body)
+ (let ((stream (gensym "STREAM-")))
+ `(let ((,stream (io-stream ,fd)))
+ (unwind-protect (let ((,var ,stream))
+ ,@body)
+ (close ,stream)))))
diff --git a/package.lisp b/package.lisp
index e670495..69426c5 100644
--- a/package.lisp
+++ b/package.lisp
@@ -27,7 +27,13 @@
#:stream-error)
(:export
#:stream
- #:with-stream
- #:stream-error
+ #:input-stream
+ #:with-input-stream
#:stream-input
- #:stream-output))
+ #:output-stream
+ #:with-output-stream
+ #:stream-output
+ #:io-stream
+ #:with-io-stream
+ #:stream-error
+ #:stream-closed-error))