-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathnames.lisp
261 lines (220 loc) · 10.7 KB
/
pathnames.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
(eval-when (:compile-toplevel :load-toplevel :execute)
(load "/home/slytobias/lisp/packages/core.lisp"))
(defpackage :pathnames
(:use :common-lisp :core)
; #+allegro (:shadow :pathname-as-directory :pathname-as-file) ?
(:export :directory-p :directory-pathname-p
:file-exists-p :file-p :file-pathname-p
:list-directory
:pathname-as-directory :pathname-as-file
:walk-directory))
;; (:export
;; :list-directory
;; :file-exists-p
;; :directory-pathname-p
;; :file-pathname-p
;; :pathname-as-directory
;; :pathname-as-file
;; :walk-directory
;; :directory-p
;; :file-p))
(in-package :pathnames)
;(in-package #:com.gigamonkeys.pathnames)
(defun list-directory (dirname)
"Return a list of the contents of the directory named by dirname.
Names of subdirectories will be returned in `directory normal
form'. Unlike CL:DIRECTORY, LIST-DIRECTORY does not accept
wildcard pathnames; `dirname' should simply be a pathname that
names a directory. It can be in either file or directory form."
(sort (list-directory-aux dirname)
#'string<
:key #'(lambda (pathname)
(if (directory-pathname-p pathname)
(last1 (pathname-directory pathname))
(file-namestring pathname)))) )
(defun list-directory-aux (dirname)
(when (wild-pathname-p dirname)
(error "Can only list concrete directory names."))
(let ((wildcard (directory-wildcard dirname)))
#+(or sbcl cmu lispworks)
;; SBCL, CMUCL, and Lispworks return subdirectories in directory
;; form just the way we want.
(directory wildcard)
;(directory wildcard #+openmcl :directories #+openmcl t
; #+allegro :directories-are-files #+allegro nil)
#+openmcl
;; OpenMCl by default doesn't return subdirectories at all. But
;; when prodded to do so with the special argument :directories,
;; it returns them in directory form.
(directory wildcard :directories t)
#+allegro
;; Allegro normally return directories in file form but we can
;; change that with the :directories-are-files argument.
(directory wildcard :directories-are-files nil)
#+clisp
;; CLISP has a particularly idiosyncratic view of things. But we
;; can bludgeon even it into doing what we want.
(nconc
;; CLISP won't list files without an extension when :type is
;; wild so we make a special wildcard for it.
(directory wildcard)
;; And CLISP doesn't consider subdirectories to match unless
;; there is a :wild in the directory component.
(directory (clisp-subdirectories-wildcard wildcard)))
#-(or sbcl cmu lispworks openmcl allegro clisp)
(error "list-directory not implemented")))
(defun file-exists-p (pathname)
"Similar to CL:PROBE-FILE except it always returns directory names
in `directory normal form'. Returns truename which will be in
`directory form' if file named is, in fact, a directory."
#+(or sbcl lispworks openmcl)
;; These implementations do "The Right Thing" as far as we are
;; concerned. They return a truename of the file or directory if it
;; exists and the truename of a directory is in directory normal
;; form.
(probe-file pathname)
#+(or allegro cmu)
;; These implementations accept the name of a directory in either
;; form and return the name in the form given. However the name of a
;; file must be given in file form. So we try first with a directory
;; name which will return NIL if either the file doesn't exist at
;; all or exists and is not a directory. Then we try with a file
;; form name.
(or (probe-file (pathname-as-directory pathname))
(probe-file pathname))
#+clisp
;; Once again CLISP takes a particularly unforgiving approach,
;; signalling ERRORs at the slightest provocation.
;; pathname in file form and actually a file -- (probe-file file) ==> truename
;; pathname in file form and doesn't exist -- (probe-file file) ==> NIL
;; pathname in dir form and actually a directory -- (probe-directory file) ==> truename
;; pathname in dir form and doesn't exist -- (probe-directory file) ==> NIL
;; pathname in file form and actually a directory -- (probe-file file) ==> ERROR
;; pathname in dir form and actually a file -- (probe-directory file) ==> ERROR
(or (ignore-errors
;; PROBE-FILE will return the truename if file exists and is a
;; file or NIL if it doesn't exist at all. If it exists but is
;; a directory PROBE-FILE will signal an error which we
;; ignore.
(probe-file (pathname-as-file pathname)))
(ignore-errors
;; PROBE-DIRECTORY returns T if the file exists and is a
;; directory or NIL if it doesn't exist at all. If it exists
;; but is a file, PROBE-DIRECTORY will signal an error.
(let ((directory-form (pathname-as-directory pathname)))
(when (ext:probe-directory directory-form)
directory-form))))
#-(or sbcl cmu lispworks openmcl allegro clisp)
(error "file-exists-p not implemented"))
(defun directory-wildcard (dirname)
(make-pathname :name :wild
:type #-clisp :wild #+clisp nil
:defaults (pathname-as-directory dirname)))
#+clisp
(defun clisp-subdirectories-wildcard (wildcard)
(make-pathname :directory (append (pathname-directory wildcard) (list :wild))
:name nil
:type nil
:defaults wildcard))
;;
;; I've changed the semantics slightly. Seibel returns P whereas
;; I merely return T/NIL.
;;
(defun directory-pathname-p (p)
"Is the given pathname the name of a directory? This function can
usefully be used to test whether a name returned by LIST-DIRECTORIES
or passed to the function in WALK-DIRECTORY is the name of a directory
in the file system since they always return names in `directory normal
form'."
(flet ((component-present-p (value)
(not (member value '(nil :unspecific)))) )
(not (or (component-present-p (pathname-name p))
(component-present-p (pathname-type p)))) ))
;(defun file-pathname-p (p)
; (unless (directory-pathname-p p) p))
(defun file-pathname-p (p)
(not (directory-pathname-p p)))
;; (defun file-pathname-p (p)
;; (if (not (directory-pathname-p p))
;; p
;; nil))
(defun pathname-as-directory (name)
"Return a pathname reperesenting the given pathname in
`directory normal form', i.e. with all the name elements in the
directory component and NIL in the name and type components. Can
not be used on wild pathnames because there's no portable way to
convert wildcards in the name and type into a single directory
component. Returns its argument if name and type are both nil or
:unspecific."
(let ((pathname (pathname name)))
(cond ((wild-pathname-p pathname) (error "Can't reliably convert wild pathnames."))
((directory-pathname-p name) pathname)
(t (make-pathname :directory (append-name-to-directory pathname)
:name nil
:type nil
:defaults pathname)))) )
;; (let ((pathname (pathname name)))
;; (when (wild-pathname-p pathname)
;; (error "Can't reliably convert wild pathnames."))
;; (if (not (directory-pathname-p name))
;; (make-pathname :directory (append-name-to-directory pathname)
;; :name nil
;; :type nil
;; :defaults pathname)
;; pathname)))
(defun append-name-to-directory (pathname)
(let ((directory (pathname-directory pathname)))
(if directory
(append directory (list (file-namestring pathname)))
(list :relative (file-namestring pathname)))) )
(defun pathname-as-file (name)
"Return a pathname reperesenting the given pathname in `file form',
i.e. with the name elements in the name and type component. Can't
convert wild pathnames because of problems mapping wild directory
component into name and type components. Returns its argument if
it is already in file form."
(let ((pathname (pathname name)))
(cond ((wild-pathname-p pathname) (error "Can't reliably convert wild pathnames."))
((file-pathname-p name) pathname)
(t (let* ((directory (pathname-directory pathname))
(name-and-type (pathname (last1 directory))))
(make-pathname :directory (butlast directory)
:name (pathname-name name-and-type)
:type (pathname-type name-and-type)
:defaults pathname)))) ))
;; (when (wild-pathname-p pathname)
;; (error "Can't reliably convert wild pathnames."))
;; (if (directory-pathname-p name)
;; (let* ((directory (pathname-directory pathname))
;; (name-and-type (pathname (first (last directory)))))
;; (make-pathname :directory (butlast directory)
;; :name (pathname-name name-and-type)
;; :type (pathname-type name-and-type)
;; :defaults pathname))
;; pathname)))
;; (walk-directory "/Users/dsletten/lisp/books/Slade/ch15/" #'(lambda (pathname) (print (string-upcase (file-namestring pathname)))))
;; (walk-directory "/Users/dsletten/lisp/books/Slade/ch15/" #'(lambda (pathname) (with-open-file (in pathname :element-type '(unsigned-byte 8)) (print (file-length in)))))
;; (walk-directory "/Users/dsletten/lisp/books/Slade/ch15/" #'(lambda (pathname) (print (string-upcase (file-namestring pathname)))) :test #'(lambda (pathname) (not (ends-with (file-namestring pathname) "~"))))
(defun walk-directory (dirname fn &key directories (test (constantly t)))
"Walk a directory invoking `fn' on each pathname found. If `test' is
supplied fn is invoked only on pathnames for which `test' returns
true. If `directories' is t invokes `test' and `fn' on directory
pathnames as well."
(labels ((walk (name)
(cond ((directory-pathname-p name)
(when (and directories (funcall test name))
(funcall fn name))
(dolist (child (list-directory name))
(walk child)))
((funcall test name) (funcall fn name)))))
(walk (pathname-as-directory dirname))))
(defun directory-p (name)
"Is `name' the name of an existing directory."
(let ((truename (file-exists-p name)))
(and truename (directory-pathname-p truename))))
; (and truename (directory-pathname-p name))))
(defun file-p (name)
"Is `name' the name of an existing file, i.e. not a directory."
(let ((truename (file-exists-p name)))
(and truename (file-pathname-p truename))))
; (and truename (file-pathname-p name))))