Commit 61cc0148 authored by Attila Lendvai's avatar Attila Lendvai
Browse files

added (unexported) with-open-file* to io.lisp, use there

parent 25759f0c
...@@ -3,24 +3,26 @@ ...@@ -3,24 +3,26 @@
(in-package :alexandria) (in-package :alexandria)
(eval-when (:compile-toplevel :load-toplevel :execute) (defmacro with-open-file* ((stream filespec &key direction element-type
(defun %expand-with-input/output-file (body direction stream-name file-name if-exists if-does-not-exist external-format)
&rest args &body body)
&key external-format "Just like WITH-OPEN-FILE, but NIL values in the keyword arguments mean to use
&allow-other-keys) the default value specified for OPEN."
(remove-from-plistf args :external-format) (once-only (direction element-type if-exists if-does-not-exist external-format)
(with-unique-names (body-fn) `(with-open-stream
(once-only (external-format) (,stream (apply #'open ,filespec
`(flet ((,body-fn (,stream-name) (append
,@body)) (when ,direction
;; this is needed not to screw up the default external-format when not specified (list :direction ,direction))
(if ,external-format (when ,element-type
(with-open-file (,stream-name ,file-name :direction ,direction (list :element-type ,element-type))
:external-format ,external-format (when ,if-exists
,@args) (list :if-exists ,if-exists))
(,body-fn ,stream-name)) (when ,if-does-not-exist
(with-open-file (,stream-name ,file-name :direction ,direction ,@args) (list :if-does-not-exist ,if-does-not-exist))
(,body-fn ,stream-name)))))))) (when ,external-format
(list :external-format ,external-format)))))
,@body)))
(defmacro with-input-from-file ((stream-name file-name &rest args (defmacro with-input-from-file ((stream-name file-name &rest args
&key (direction nil direction-p) &key (direction nil direction-p)
...@@ -32,7 +34,8 @@ which is only sent to WITH-OPEN-FILE when it's not NIL." ...@@ -32,7 +34,8 @@ which is only sent to WITH-OPEN-FILE when it's not NIL."
(declare (ignore direction)) (declare (ignore direction))
(when direction-p (when direction-p
(error "Can't specifiy :DIRECTION for WITH-INPUT-FROM-FILE.")) (error "Can't specifiy :DIRECTION for WITH-INPUT-FROM-FILE."))
(apply '%expand-with-input/output-file body :input stream-name file-name args)) `(with-open-file* (,stream-name ,file-name :direction :input ,@args)
,@body))
(defmacro with-output-to-file ((stream-name file-name &rest args (defmacro with-output-to-file ((stream-name file-name &rest args
&key (direction nil direction-p) &key (direction nil direction-p)
...@@ -44,7 +47,8 @@ which is only sent to WITH-OPEN-FILE when it's not NIL." ...@@ -44,7 +47,8 @@ which is only sent to WITH-OPEN-FILE when it's not NIL."
(declare (ignore direction)) (declare (ignore direction))
(when direction-p (when direction-p
(error "Can't specifiy :DIRECTION for WITH-OUTPUT-TO-FILE.")) (error "Can't specifiy :DIRECTION for WITH-OUTPUT-TO-FILE."))
(apply '%expand-with-input/output-file body :output stream-name file-name args)) `(with-open-file* (,stream-name ,file-name :direction :output ,@args)
,@body))
(defun read-file-into-string (pathname &key (buffer-size 4096) external-format) (defun read-file-into-string (pathname &key (buffer-size 4096) external-format)
"Return the contents of the file denoted by PATHNAME as a fresh string. "Return the contents of the file denoted by PATHNAME as a fresh string.
...@@ -62,7 +66,7 @@ unless it's NIL, which means the system default." ...@@ -62,7 +66,7 @@ unless it's NIL, which means the system default."
:while (= bytes-read buffer-size))))))) :while (= bytes-read buffer-size)))))))
(defun write-string-into-file (string pathname &key (if-exists :error) (defun write-string-into-file (string pathname &key (if-exists :error)
(if-does-not-exist :error) if-does-not-exist
external-format) external-format)
"Write STRING to PATHNAME. "Write STRING to PATHNAME.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment