Commit 1135a9067247ff00ab827076873d477c79090736

Thomas de Grivel 2017-05-28T12:09:30

input-stream, with-input-stream, output-stream, with-output-stream, io-stream, with-io-stream

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))