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,28 +3,30 @@ ...@@ -3,28 +3,30 @@
(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)
&allow-other-keys) &allow-other-keys)
&body body) &body body)
"Evaluate BODY with STREAM-NAME to an input stream on the file "Evaluate BODY with STREAM-NAME to an input stream on the file
FILE-NAME. ARGS is sent as is to the call to OPEN except EXTERNAL-FORMAT, FILE-NAME. ARGS is sent as is to the call to OPEN except EXTERNAL-FORMAT,
...@@ -32,11 +34,12 @@ which is only sent to WITH-OPEN-FILE when it's not NIL." ...@@ -32,11 +34,12 @@ 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)
&allow-other-keys) &allow-other-keys)
&body body) &body body)
"Evaluate BODY with STREAM-NAME to an output stream on the file "Evaluate BODY with STREAM-NAME to an output stream on the file
FILE-NAME. ARGS is sent as is to the call to OPEN except EXTERNAL-FORMAT, FILE-NAME. ARGS is sent as is to the call to OPEN except EXTERNAL-FORMAT,
...@@ -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,15 +66,15 @@ unless it's NIL, which means the system default." ...@@ -62,15 +66,15 @@ 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.
The EXTERNAL-FORMAT parameter will be passed directly to WITH-OPEN-FILE The EXTERNAL-FORMAT parameter will be passed directly to WITH-OPEN-FILE
unless it's NIL, which means the system default." unless it's NIL, which means the system default."
(with-output-to-file (file-stream pathname :if-exists if-exists (with-output-to-file (file-stream pathname :if-exists if-exists
:if-does-not-exist if-does-not-exist :if-does-not-exist if-does-not-exist
:external-format external-format) :external-format external-format)
(write-sequence string file-stream))) (write-sequence string file-stream)))
(defun copy-file (from to &key (if-to-exists :supersede) (defun copy-file (from to &key (if-to-exists :supersede)
......
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