faster WHICHEVER
[alexandria.git] / io.lisp
blob5df161902d6d3fc607b1a4f4f1bec37df3a04186
1 ;; Copyright (c) 2002-2006, Edward Marco Baringer
2 ;; All rights reserved.
4 (in-package :alexandria)
6 (defmacro with-open-file* ((stream filespec &key direction element-type
7 if-exists if-does-not-exist external-format)
8 &body body)
9 "Just like WITH-OPEN-FILE, but NIL values in the keyword arguments mean to use
10 the default value specified for OPEN."
11 (once-only (direction element-type if-exists if-does-not-exist external-format)
12 `(with-open-stream
13 (,stream (apply #'open ,filespec
14 (append
15 (when ,direction
16 (list :direction ,direction))
17 (when ,element-type
18 (list :element-type ,element-type))
19 (when ,if-exists
20 (list :if-exists ,if-exists))
21 (when ,if-does-not-exist
22 (list :if-does-not-exist ,if-does-not-exist))
23 (when ,external-format
24 (list :external-format ,external-format)))))
25 ,@body)))
27 (defmacro with-input-from-file ((stream-name file-name &rest args
28 &key (direction nil direction-p)
29 &allow-other-keys)
30 &body body)
31 "Evaluate BODY with STREAM-NAME to an input stream on the file
32 FILE-NAME. ARGS is sent as is to the call to OPEN except EXTERNAL-FORMAT,
33 which is only sent to WITH-OPEN-FILE when it's not NIL."
34 (declare (ignore direction))
35 (when direction-p
36 (error "Can't specifiy :DIRECTION for WITH-INPUT-FROM-FILE."))
37 `(with-open-file* (,stream-name ,file-name :direction :input ,@args)
38 ,@body))
40 (defmacro with-output-to-file ((stream-name file-name &rest args
41 &key (direction nil direction-p)
42 &allow-other-keys)
43 &body body)
44 "Evaluate BODY with STREAM-NAME to an output stream on the file
45 FILE-NAME. ARGS is sent as is to the call to OPEN except EXTERNAL-FORMAT,
46 which is only sent to WITH-OPEN-FILE when it's not NIL."
47 (declare (ignore direction))
48 (when direction-p
49 (error "Can't specifiy :DIRECTION for WITH-OUTPUT-TO-FILE."))
50 `(with-open-file* (,stream-name ,file-name :direction :output ,@args)
51 ,@body))
53 (defun read-file-into-string (pathname &key (buffer-size 4096) external-format)
54 "Return the contents of the file denoted by PATHNAME as a fresh string.
56 The EXTERNAL-FORMAT parameter will be passed directly to WITH-OPEN-FILE
57 unless it's NIL, which means the system default."
58 (with-input-from-file
59 (file-stream pathname :external-format external-format)
60 (let ((*print-pretty* nil))
61 (with-output-to-string (datum)
62 (let ((buffer (make-array buffer-size :element-type 'character)))
63 (loop
64 :for bytes-read = (read-sequence buffer file-stream)
65 :do (write-sequence buffer datum :start 0 :end bytes-read)
66 :while (= bytes-read buffer-size)))))))
68 (defun write-string-into-file (string pathname &key (if-exists :error)
69 if-does-not-exist
70 external-format)
71 "Write STRING to PATHNAME.
73 The EXTERNAL-FORMAT parameter will be passed directly to WITH-OPEN-FILE
74 unless it's NIL, which means the system default."
75 (with-output-to-file (file-stream pathname :if-exists if-exists
76 :if-does-not-exist if-does-not-exist
77 :external-format external-format)
78 (write-sequence string file-stream)))
80 (defun read-file-into-byte-vector (pathname)
81 "Read PATHNAME into a freshly allocated (unsigned-byte 8) vector."
82 (with-input-from-file (stream pathname :element-type '(unsigned-byte 8))
83 (let ((length (file-length stream)))
84 (assert length)
85 (let ((result (make-array length :element-type '(unsigned-byte 8))))
86 (read-sequence result stream)
87 result))))
89 (defun write-byte-vector-into-file (bytes pathname &key (if-exists :error)
90 if-does-not-exist)
91 "Write BYTES to PATHNAME."
92 (check-type bytes (vector (unsigned-byte 8)))
93 (with-output-to-file (stream pathname :if-exists if-exists
94 :if-does-not-exist if-does-not-exist
95 :element-type '(unsigned-byte 8))
96 (write-sequence bytes stream)))
98 (defun copy-file (from to &key (if-to-exists :supersede)
99 (element-type '(unsigned-byte 8)) finish-output)
100 (with-input-from-file (input from :element-type element-type)
101 (with-output-to-file (output to :element-type element-type
102 :if-exists if-to-exists)
103 (copy-stream input output
104 :element-type element-type
105 :finish-output finish-output))))
107 (defun copy-stream (input output &key (element-type (stream-element-type input))
108 (buffer-size 4096)
109 (buffer (make-array buffer-size :element-type element-type))
110 finish-output)
111 "Reads data from INPUT and writes it to OUTPUT. Both INPUT and OUTPUT must
112 be streams, they will be passed to READ-SEQUENCE and WRITE-SEQUENCE and must have
113 compatible element-types."
114 (loop
115 :for bytes-read = (read-sequence buffer input)
116 :while (= bytes-read buffer-size)
117 :do (write-sequence buffer output)
118 :finally (progn
119 (write-sequence buffer output :end bytes-read)
120 (when finish-output
121 (finish-output output)))))