with-open-files

A useful macro for opening multiple files simultaneously. Takes a list of filenames (can be pathnames), returns a function that takes a filename and provides the (open) stream for the associated file, and wraps the whole thing in an unwind-protect for safety. All the files have to take the same open args (for instance, they’re all going to be input or all going to be output).

(defmacro with-open-files ((file-seq
                            &optional open-args (getter 'get-handle))
                           &body body)
  (let ((failed? (gensym "failed?"))
        (handle-seq (gensym "handle-seq")))
    `(let ((,handle-seq (mapcar
                         (lambda (file-name)
                           (cons file-name (open file-name ,@open-args)))
                         ,file-seq))
           (,failed? t))
      (flet ((,getter (file-name)
               (cdr (assoc file-name ,handle-seq))))
        (unwind-protect
             (multiple-value-prog1
                 (progn ,@body)
               (setq ,failed? nil))
          (mapcar (lambda (handle)
                    (close (cdr handle) :abort ,failed?))
                  ,handle-seq)))))))

Apr 19, 10:12 pm | common-lisp |

Comments

commenting closed for this article

Links

RSS Feed