source: trunk/abcl/src/org/armedbear/lisp/directory.lisp @ 12985

Last change on this file since 12985 was 12985, checked in by vvoutilainen, 11 years ago

Add WILD-INFERIORS support for DIRECTORY.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1;;; directory.lisp
2;;;
3;;; Copyright (C) 2004-2007 Peter Graves
4;;; Copyright (C) 2008 Ville Voutilainen
5;;; $Id: directory.lisp 12985 2010-10-30 17:53:45Z vvoutilainen $
6;;;
7;;; This program is free software; you can redistribute it and/or
8;;; modify it under the terms of the GNU General Public License
9;;; as published by the Free Software Foundation; either version 2
10;;; of the License, or (at your option) any later version.
11;;;
12;;; This program is distributed in the hope that it will be useful,
13;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with this program; if not, write to the Free Software
19;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20;;;
21;;; As a special exception, the copyright holders of this library give you
22;;; permission to link this library with independent modules to produce an
23;;; executable, regardless of the license terms of these independent
24;;; modules, and to copy and distribute the resulting executable under
25;;; terms of your choice, provided that you also meet, for each linked
26;;; independent module, the terms and conditions of the license of that
27;;; module.  An independent module is a module which is not derived from
28;;; or based on this library.  If you modify this library, you may extend
29;;; this exception to your version of the library, but you are not
30;;; obligated to do so.  If you do not wish to do so, delete this
31;;; exception statement from your version.
32
33(in-package "SYSTEM")
34
35(defun pathname-as-file (pathname)
36  (let ((directory (pathname-directory pathname)))
37    (make-pathname :host nil
38                   :device (pathname-device pathname)
39                   :directory (butlast directory)
40                   :name (car (last directory))
41                   :type nil
42                   :version nil)))
43
44(defun wild-inferiors-p (component)
45  (eq component :wild-inferiors))
46
47(defun list-directories-with-wildcards (pathname 
48          &optional (wild-inferiors-found nil))
49  (let* ((directory (pathname-directory pathname))
50   (first-wild-inferior (and (not wild-inferiors-found) 
51           (position-if #'wild-inferiors-p directory)))
52   (first-wild (position-if #'wild-p directory))
53   (wild (when (or first-wild-inferior first-wild)
54     (nthcdr (or first-wild-inferior first-wild) directory)))
55   (non-wild (if (or first-wild-inferior first-wild)
56           (nbutlast directory
57         (- (length directory) 
58            (or first-wild-inferior first-wild)))
59         directory))
60   (newpath (make-pathname :directory non-wild
61         :name nil :type nil :defaults pathname))
62   (entries (list-directory newpath)))
63    (if (not (or wild wild-inferiors-found))
64  entries
65  (let ((inferior-entries (when (or wild-inferiors-found first-wild-inferior) entries)))
66    (nconc 
67     (mapcan (lambda (entry) 
68         (when (pathname-match-p (pathname entry) pathname)
69           (list entry)))
70       inferior-entries)
71     (mapcan (lambda (entry)
72         (let* ((pathname (pathname entry))
73          (directory (pathname-directory pathname))
74          (rest-wild (cdr wild)))
75           (unless (pathname-name pathname)
76       (when (pathname-match-p (first (last directory))
77            (cond ((eql (car wild) :wild)
78             "*")
79            ((eql (car wild) :wild-inferiors)
80             "*")
81            (wild 
82             (car wild))
83            (t "")))
84         (when rest-wild
85           (setf directory (nconc directory rest-wild)))
86         (list-directories-with-wildcards
87          (make-pathname :directory directory
88             :defaults newpath)
89          (or first-wild-inferior wild-inferiors-found))))))
90       entries))))))
91
92
93(defun directory (pathspec &key)
94  (let ((pathname (merge-pathnames pathspec)))
95    (when (logical-pathname-p pathname)
96      (setq pathname (translate-logical-pathname pathname)))
97    (if (or (position #\* (namestring pathname))
98      (wild-pathname-p pathname))
99        (if (pathname-jar-p pathname)
100            (match-wild-jar-pathname pathname)
101            (let ((namestring (directory-namestring pathname)))
102              (when (and namestring (> (length namestring) 0))
103                (when (featurep :windows)
104                  (let ((device (pathname-device pathname)))
105                    (when device
106                      (setq namestring (concatenate 'string device ":" namestring)))))
107                (let ((entries (list-directories-with-wildcards namestring))
108                      (matching-entries ()))
109                  (dolist (entry entries)
110                    (cond ((file-directory-p entry)
111                           (when (pathname-match-p (file-namestring (pathname-as-file entry)) (file-namestring pathname))
112                             (push entry matching-entries)))
113                          ((pathname-match-p (file-namestring entry) (file-namestring pathname))
114                           (push entry matching-entries))))
115                  matching-entries))))
116        ;; Not wild.
117        (let ((truename (probe-file pathname)))
118          (if truename
119              (list (pathname truename))
120              nil)))))
Note: See TracBrowser for help on using the repository browser.