Commit 7c85782eef131b0a0c9bab78c3438f0b6da9b476

Thomas de Grivel 2015-06-29T21:11:29

DO-LINES and DO-STREAM-LINES.

diff --git a/files.lisp b/files.lisp
index f8818b5..ffd2882 100644
--- a/files.lisp
+++ b/files.lisp
@@ -35,6 +35,8 @@
    #:unlink-file
    #:read-into-string
    #:readlink
+   #:do-lines
+   #:do-stream-lines
    #:regex-stream-lines
    #:regex-lines))
 
@@ -165,6 +167,26 @@ Return NIL otherwise."
   (ignore-errors (sb-posix:readlink path)))
 
 
+;;  Lines
+
+(defmacro do-lines ((var input) &body body)
+  (let ((g!in (gensym "IN-")))
+    `(let ((,g!in ,input))
+       (if (streamp ,g!in)
+           (do-stream-lines (,var ,g!in) ,@body)
+           (with-input-from-file/utf-8 (,g!in ,g!in)
+             (do-stream-lines (,var ,g!in) ,@body))))))
+
+(defmacro do-stream-lines ((var stream) &body body)
+  (let ((g!line (gensym "LINE-"))
+        (g!stream (gensym "STREAM-")))
+    `(let ((,g!stream ,stream))
+       (do ((,g!line (read-line ,g!stream nil) (read-line ,g!stream nil)))
+           ((null ,g!line))
+         (let ((,var ,g!line))
+           ,@body)))))
+
+
 ;;  Regex
 
 (defun regex-do-matches (regex string fn)
@@ -190,8 +212,7 @@ Return NIL otherwise."
     (t (warn "Neither replace or match were given to regex."))))
 
 (defun regex-stream-lines (regex input replace match output)
-  (do ((line #1=(read-line input nil) #1#))
-      ((null line))
+  (do-stream-lines (line input)
     (regex% regex line replace match output)))
 
 (defun regex-lines (regex input &key replace match output)