Skip to content

Commit f0f3a06

Browse files
committed
Move up with-open-files so we can use it internally.
"Dogfooding."
1 parent c291448 commit f0f3a06

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

files.lisp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
(in-package :serapeum)
22

3+
(defmacro with-open-files ((&rest args) &body body)
4+
"A simple macro to open one or more files providing the streams for the BODY. The ARGS is a list of `(stream filespec options*)` as supplied to WITH-OPEN-FILE."
5+
(case (length args)
6+
((0)
7+
`(progn ,@body))
8+
((1)
9+
`(with-open-file ,(first args) ,@body))
10+
(t `(with-open-file ,(first args)
11+
(with-open-files
12+
,(rest args) ,@body)))))
13+
314
(defun path-join (&rest pathnames)
415
"Build a pathname by merging from right to left.
516
With `path-join' you can pass the elements of the pathname being built
@@ -93,19 +104,19 @@ as vectors."
93104
:element-type 'octet
94105
:initial-element 0)))
95106
(declare (inline make-buffer))
96-
(with-input-from-file (file1 file1 :element-type 'octet)
97-
(with-input-from-file (file2 file2 :element-type 'octet)
98-
(and (= (file-length file1)
99-
(file-length file2))
100-
(loop with buffer1 = (make-buffer)
101-
with buffer2 = (make-buffer)
102-
for end1 = (read-sequence buffer1 file1)
103-
for end2 = (read-sequence buffer2 file2)
104-
until (or (zerop end1) (zerop end2))
105-
always (and (= end1 end2)
106-
(octet-vector= buffer1 buffer2
107-
:end1 end1
108-
:end2 end2))))))))
107+
(with-open-files ((file1 file1 :element-type 'octet :direction :input)
108+
(file2 file2 :element-type 'octet :direction :input))
109+
(and (= (file-length file1)
110+
(file-length file2))
111+
(loop with buffer1 = (make-buffer)
112+
with buffer2 = (make-buffer)
113+
for end1 = (read-sequence buffer1 file1)
114+
for end2 = (read-sequence buffer2 file2)
115+
until (or (zerop end1) (zerop end2))
116+
always (and (= end1 end2)
117+
(octet-vector= buffer1 buffer2
118+
:end1 end1
119+
:end2 end2)))))))
109120

110121
(defun file-size (file &key (element-type '(unsigned-byte 8)))
111122
"The size of FILE, in units of ELEMENT-TYPE (defaults to bytes).
@@ -203,16 +214,3 @@ Inspired by the function of the same name in Emacs."
203214
:flavor flavor
204215
:suffix suffix
205216
:space space)))
206-
207-
(defmacro with-open-files ((&rest args) &body body)
208-
"A simple macro to open one or more files providing the streams for the BODY. The ARGS is a list of `(stream filespec options*)` as supplied to WITH-OPEN-FILE."
209-
(case (length args)
210-
((0)
211-
`(progn ,@body))
212-
((1)
213-
`(with-open-file ,(first args) ,@body))
214-
(t `(with-open-file ,(first args)
215-
(with-open-files
216-
,(rest args) ,@body)))))
217-
218-

0 commit comments

Comments
 (0)