[Cl-darcs-cvs] r13 - cl-darcs/trunk

mhenoch at common-lisp.net mhenoch at common-lisp.net
Sat Jun 24 15:59:29 UTC 2006


Author: mhenoch
Date: Sat Jun 24 11:59:17 2006
New Revision: 13

Modified:
   cl-darcs/trunk/apply-patch.lisp
   cl-darcs/trunk/unreadable-stream.lisp
Log:
Read lines the same way darcs does.


Modified: cl-darcs/trunk/apply-patch.lisp
==============================================================================
--- cl-darcs/trunk/apply-patch.lisp	(original)
+++ cl-darcs/trunk/apply-patch.lisp	Sat Jun 24 11:59:17 2006
@@ -33,7 +33,8 @@
 					     (open ,filename-gensym
 						   :direction :input
 						   :if-does-not-exist :error
-						   :element-type '(unsigned-byte 8))))
+						   :element-type '(unsigned-byte 8))
+					     :haskellish-lines t))
 	     ;; Open a temporary file for writing.
 	     (with-temp-file (,outstreamvar :element-type '(unsigned-byte 8))
 	       (progn , at body)

Modified: cl-darcs/trunk/unreadable-stream.lisp
==============================================================================
--- cl-darcs/trunk/unreadable-stream.lisp	(original)
+++ cl-darcs/trunk/unreadable-stream.lisp	Sat Jun 24 11:59:17 2006
@@ -19,8 +19,20 @@
 (defclass unreadable-stream
     (trivial-gray-streams:trivial-gray-stream-mixin
      trivial-gray-streams:fundamental-binary-input-stream)
-  ((stream :initarg :base-stream)
-   (buffer :initform nil))
+  ((stream
+    :initarg :base-stream
+    :documentation "The stream wrapped by this unreadable-stream.")
+   (haskellish-lines
+    :initarg :haskellish-lines :initform nil
+    :documentation "If true, read lines as Haskell would read them.
+That is, a line is a (possibly empty) list of characters delimited
+by either newlines or end-of-file.  In particular, if the file ends
+with a newline, it has an extra empty last line in Haskell mode.
+
+This flag affects only `read-binary-line'.")
+   
+   (buffer :initform nil)
+   (at-end-of-file :initform nil))
   (:documentation "A wrapper for a binary input stream.
 Unlimited \"unreading\" is allowed through UNREAD-BYTE and
 UNREAD-SEQUENCE."))
@@ -101,6 +113,30 @@
 	;; Otherwise, report that the sequence is full.
 	end)))
 
+(defmethod read-binary-line ((stream unreadable-stream) &optional (eof-error-p t) eof-value)
+  "If stream is in \"Haskell mode\", treat newlines at end of file accordingly."
+  (if (not (slot-value stream 'haskellish-lines))
+      (call-next-method)
+      ;; The delimiter between lines is a newline or end-of-file.
+      ;; Thus, if we have just returned the last newline-terminated
+      ;; line and stand before EOF, we can't just return EOF since
+      ;; there is an zero-length line between the last newline and the
+      ;; EOF.
+      (if (null (slot-value stream 'at-end-of-file))
+	  ;; So we haven't read EOF yet.  That means that we can
+	  ;; return at least one more line (though it may be
+	  ;; zero-length).
+	  (multiple-value-bind (line delim)
+	      (read-until 10 stream nil :eof)
+	    ;; If EOF follows after that line, note it.
+	    (when (eql delim :eof)
+	      (setf (slot-value stream 'at-end-of-file) t))
+	    line)
+	  ;; If we have already set the EOF flag, act accordingly.
+	  (if eof-error-p
+	      (error 'end-of-file :stream stream)
+	      eof-value))))
+
 ;; This method is meant as an optimization, but it actually makes
 ;; things slower.  Need to investigate why...
 #+nil 



More information about the Cl-darcs-cvs mailing list