summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlavio Cruz <flaviocruz@gmail.com>2008-09-05 13:06:25 +0000
committerFlavio Cruz <flaviocruz@gmail.com>2008-09-05 13:06:25 +0000
commit96efb8bf8e3b99ccc203bc6e55f1e047972e24e4 (patch)
treed60d8be17bf8797c5bf0ba7dae9817e85f163912
parentb68bc36ebd82f82b82b7a58008f0e8cb8c14d1ea (diff)
Add base stream class: hurd-stream.
-rw-r--r--hurd-streams.asd4
-rw-r--r--streams/input.lisp58
-rw-r--r--streams/package.lisp3
-rw-r--r--streams/stream.lisp56
4 files changed, 73 insertions, 48 deletions
diff --git a/hurd-streams.asd b/hurd-streams.asd
index 9c4938242..10da9a0c7 100644
--- a/hurd-streams.asd
+++ b/hurd-streams.asd
@@ -19,6 +19,8 @@
:trivial-gray-streams)
:components ((:module streams
:components ((:file "package")
+ (:file "stream"
+ :depends-on ("package"))
(:file "input"
- :depends-on ("package"))))))
+ :depends-on ("stream"))))))
diff --git a/streams/input.lisp b/streams/input.lisp
index 2b733dda1..12a6539c8 100644
--- a/streams/input.lisp
+++ b/streams/input.lisp
@@ -9,77 +9,43 @@
:adjustable t
:element-type '(unsigned-byte 8)))
-(defclass hurd-input-stream (trivial-gray-stream-mixin fundamental-binary-input-stream)
- ((port :initform nil
- :initarg :port
- :accessor port)
- (cache :initform (%create-adjustable-array +default-read-ahead+)
+(defclass hurd-input-stream (hurd-stream fundamental-binary-input-stream)
+ ((cache :initform (%create-adjustable-array +default-read-ahead+)
:accessor cache)
(last-byte :initform nil
:accessor last-byte)
(cache-pos :initform 0
- :accessor cache-pos)
- (offset :initform 0
- :accessor offset)))
+ :accessor cache-pos)))
(defun read-stream-cache (stream)
(multiple-value-bind (data total)
(io-read (port stream)
:offset (offset stream)
:amount +default-read-ahead+)
- (setf (fill-pointer (cache stream)) total)
+ (unless data
+ (setf total 0))
(setf (cache-pos stream) 0)
- (replace (cache stream) data)
+ (setf (fill-pointer (cache stream)) total)
+ (when data
+ (replace (cache stream) data))
t))
(defmethod initialize-instance :after ((stream hurd-input-stream) &rest initargs)
(declare (ignore initargs))
- (with-accessors ((port port) (cache cache) (cache-pos cache-pos))
- stream
- (unless (port-valid-p port)
- (error "Port not valid: ~A" port))
- (read-stream-cache stream)))
-
-(defmethod print-object ((istream hurd-input-stream) stream)
- (format stream "#<HURD-INPUT-STREAM port=~a offset=~a>"
- (port istream)
- (offset istream)))
-
-(defmethod open-stream-p ((stream hurd-input-stream))
- "Returns a true value if STREAM is open."
- (port-valid-p (port stream)))
+ (read-stream-cache stream))
(defmethod close ((stream hurd-input-stream) &key abort)
"Closes the stream STREAM."
(declare (ignore abort))
(when (open-stream-p stream)
- (port-deallocate (port stream))
(setf (cache stream) nil)
- (setf (port stream) nil)))
-
-(defmethod stream-element-type ((stream hurd-input-stream))
- "The element type is always unsigned-byte 8."
- '(unsigned-byte 8))
-
-(defmethod stream-file-position ((stream hurd-input-stream))
- (offset stream))
+ (call-next-method)))
(defmethod (setf stream-file-position) (position (stream hurd-input-stream))
"Sets the file offfset."
+ (declare (ignore position))
(with-cleanup (read-stream-cache stream)
- (case position
- (:end
- (setf (offset stream)
- (io-seek (port stream)
- :offset 0
- :whence :seek-end)))
- (otherwise
- (when (eq position :start)
- (setf position 0))
- (let ((new-offset (io-seek (port stream)
- :offset position
- :whence :seek-set)))
- (setf (offset stream) new-offset))))))
+ (call-next-method)))
(defun %hurd-eof-reached-p (cache)
(not (and cache
diff --git a/streams/package.lisp b/streams/package.lisp
index 6c097c5f9..0dfb119f3 100644
--- a/streams/package.lisp
+++ b/streams/package.lisp
@@ -4,7 +4,8 @@
(defpackage :cl-hurd.streams
(:nicknames :hurd-streams)
(:use :cl :hurd-common :mach :hurd :trivial-gray-streams)
- (:export :hurd-input-stream
+ (:export :hurd-stream
+ :hurd-input-stream
:make-hurd-input-stream
:with-hurd-input-stream))
diff --git a/streams/stream.lisp b/streams/stream.lisp
new file mode 100644
index 000000000..d59a3317d
--- /dev/null
+++ b/streams/stream.lisp
@@ -0,0 +1,56 @@
+
+(in-package :hurd-streams)
+
+(defclass hurd-stream (trivial-gray-stream-mixin)
+ ((port :initform nil
+ :initarg :port
+ :accessor port)
+ (offset :initform 0
+ :accessor offset)))
+
+(defmethod initialize-instance :after ((stream hurd-stream) &rest initargs)
+ (declare (ignore initargs))
+ (with-accessors ((port port))
+ stream
+ (unless (port-valid-p port)
+ (error "Port not valid: ~A" port))))
+
+(defmethod print-object ((istream hurd-stream) stream)
+ (format stream "#<HURD-STREAM port=~a offset=~a>"
+ (port istream)
+ (offset istream)))
+
+(defmethod open-stream-p ((stream hurd-stream))
+ "Returns a true value if STREAM is open."
+ (port-valid-p (port stream)))
+
+(defmethod close ((stream hurd-stream) &key abort)
+ "Closes the stream STREAM."
+ (declare (ignore abort))
+ (when (open-stream-p stream)
+ (port-deallocate (port stream))
+ (setf (port stream) nil)))
+
+(defmethod stream-element-type ((stream hurd-stream))
+ "The element type is always unsigned-byte 8."
+ '(unsigned-byte 8))
+
+(defmethod stream-file-position ((stream hurd-stream))
+ (offset stream))
+
+(defmethod (setf stream-file-position) (position (stream hurd-stream))
+ "Sets the file offfset."
+ (case position
+ (:end
+ (setf (offset stream)
+ (io-seek (port stream)
+ :offset 0
+ :whence :seek-end)))
+ (otherwise
+ (when (eq position :start)
+ (setf position 0))
+ (let ((new-offset (io-seek (port stream)
+ :offset position
+ :whence :seek-set)))
+ (setf (offset stream) new-offset)))))
+