diff options
author | Boutade <thegoofist@protonmail.com> | 2019-05-01 10:03:01 -0500 |
---|---|---|
committer | Boutade <thegoofist@protonmail.com> | 2019-05-01 10:03:01 -0500 |
commit | aa715e3e68966fc3b0a09c7a1047f724613b2961 (patch) | |
tree | 6422ea336127e6203c7ed8cac08c42f9d149c6f9 | |
parent | 755ae5198e14d75bcf728c8ca3cd2b1b934e720e (diff) |
added static text replay streams
-rw-r--r-- | package.lisp | 3 | ||||
-rw-r--r-- | replay-streams.lisp | 33 |
2 files changed, 36 insertions, 0 deletions
diff --git a/package.lisp b/package.lisp index 9f5b0a8..38050de 100644 --- a/package.lisp +++ b/package.lisp @@ -3,10 +3,13 @@ (defpackage #:replay-streams (:use #:cl #:trivial-gray-streams) (:export + #:checkpoint #:recover-source #:replay-character-stream #:replay-finished-p #:replay-on #:rewind + #:rewind-to #:rewound-p + #:static-text-replay-stream )) diff --git a/replay-streams.lisp b/replay-streams.lisp index 346ad35..66a3eaf 100644 --- a/replay-streams.lisp +++ b/replay-streams.lisp @@ -8,6 +8,39 @@ (replay-mode :initform nil) (replay-stream :initform nil))) +(defclass static-text-replay-stream (fundamental-character-input-stream) + ((text :initarg :text) + (head :initform 0))) + +(defmethod stream-read-char ((stream static-text-replay-stream)) + (with-slots (text head) stream + (if (>= head (length text)) + :eof + (progn + (incf head) + (aref text (- head 1)))))) + +(defmethod stream-unread-char ((stream static-text-replay-stream) char) + (with-slots (head) stream + (when (> head 0) (decf head)) + nil)) + +(defgeneric checkpoint (stream) + (:documentation "Creates a reference that can be used to rewind the stream at a later time.")) + +(defmethod checkpoint ((stream static-text-replay-stream)) + (with-slots (head) stream + head)) + ;(slot-value stream 'head)) + +(defgeneric rewind-to (stream checkpoint) + (:documentation "Rewinds the stream to the checkpoint")) + +(defmethod rewind-to ((stream static-text-replay-stream) checkpoint) + (with-slots (head) stream + (setf head checkpoint))) + ;;(setf (slot-value stream 'head) checkpoint)) + (defun replay-on (stream) (make-instance 'replay-character-stream :source stream)) |