1 | /* |
---|
2 | * probe_file.java |
---|
3 | * |
---|
4 | * Copyright (C) 2003-2005 Peter Graves |
---|
5 | * $Id: probe_file.java 15569 2022-03-19 12:50:18Z mevenson $ |
---|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | import java.io.File; |
---|
39 | |
---|
40 | public final class probe_file |
---|
41 | { |
---|
42 | public static final Primitive PROBE_FILE |
---|
43 | = new pf_probe_file(); |
---|
44 | @DocString(name="probe-file", |
---|
45 | args="pathspec", |
---|
46 | returns="truename") |
---|
47 | private static final class pf_probe_file extends Primitive { |
---|
48 | pf_probe_file() { |
---|
49 | super(Symbol.PROBE_FILE, "pathspec"); |
---|
50 | } |
---|
51 | @Override |
---|
52 | public LispObject execute(LispObject arg) |
---|
53 | { |
---|
54 | if (arg == null || arg.equals(NIL)) { |
---|
55 | return NIL; |
---|
56 | } |
---|
57 | Pathname p = coerceToPathname(arg); |
---|
58 | if (p.isWild()) { |
---|
59 | return error(new FileError("Cannot find the TRUENAME for a wild pathname.", |
---|
60 | p)); |
---|
61 | } |
---|
62 | // TODO: refactor Pathname{,Jar,URL}.truename() to be non-static? |
---|
63 | if (p instanceof JarPathname) { |
---|
64 | return JarPathname.truename(p, false); |
---|
65 | } else if (p instanceof URLPathname) { |
---|
66 | return URLPathname.truename((URLPathname)p, false); |
---|
67 | } else { |
---|
68 | return Pathname.truename(p, false); |
---|
69 | } |
---|
70 | } |
---|
71 | }; |
---|
72 | |
---|
73 | public static final Primitive TRUENAME |
---|
74 | = new pf_truename(); |
---|
75 | @DocString(name="truename", |
---|
76 | args="pathspec", |
---|
77 | returns="pathname") |
---|
78 | private static class pf_truename extends Primitive { |
---|
79 | pf_truename() { |
---|
80 | super(Symbol.TRUENAME, "filespec"); |
---|
81 | } |
---|
82 | @Override |
---|
83 | public LispObject execute(LispObject arg) |
---|
84 | { |
---|
85 | Pathname p = coerceToPathname(arg); |
---|
86 | if (p.isWild()) { |
---|
87 | return error(new FileError("Cannot find the TRUENAME for a wild pathname.", |
---|
88 | p)); |
---|
89 | } |
---|
90 | |
---|
91 | // TODO: refactor Pathname{,Jar,URL}.truename() to be non-static? |
---|
92 | if (p instanceof JarPathname) { |
---|
93 | return JarPathname.truename(p, true); |
---|
94 | } else if (p instanceof URLPathname) { |
---|
95 | return URLPathname.truename((URLPathname)p, true); |
---|
96 | } else { |
---|
97 | return Pathname.truename(p, true); |
---|
98 | } |
---|
99 | } |
---|
100 | }; |
---|
101 | |
---|
102 | public static final Primitive PROBE_DIRECTORY |
---|
103 | = new pf_probe_directory(); |
---|
104 | @DocString(name="probe-directory", |
---|
105 | args="pathspec", |
---|
106 | returns="truename") |
---|
107 | private static final class pf_probe_directory extends Primitive { |
---|
108 | pf_probe_directory() { |
---|
109 | super("probe-directory", PACKAGE_EXT, true); |
---|
110 | } |
---|
111 | @Override |
---|
112 | public LispObject execute(LispObject arg) |
---|
113 | { |
---|
114 | Pathname pathname = coerceToPathname(arg); |
---|
115 | if (pathname.isWild()) { |
---|
116 | error(new FileError("Cannot probe a wild pathname as a directory.", pathname)); |
---|
117 | } |
---|
118 | Pathname defaultedPathname = (Pathname)Pathname.MERGE_PATHNAMES.execute(pathname); |
---|
119 | if (defaultedPathname instanceof JarPathname) { |
---|
120 | if (defaultedPathname.getName().equals(NIL) |
---|
121 | && defaultedPathname.getType().equals(NIL)) { |
---|
122 | return Symbol.PROBE_FILE.execute(defaultedPathname); |
---|
123 | } |
---|
124 | SimpleString lastDirectory = (SimpleString)Symbol.FILE_NAMESTRING.execute(defaultedPathname); |
---|
125 | LispObject appendedDirectory |
---|
126 | = defaultedPathname.getDirectory().reverse().push(lastDirectory).reverse(); |
---|
127 | defaultedPathname.setDirectory(appendedDirectory); |
---|
128 | return Symbol.PROBE_FILE.execute(defaultedPathname); |
---|
129 | } |
---|
130 | |
---|
131 | File file = defaultedPathname.getFile(); |
---|
132 | if (file == null || !file.isDirectory()) { |
---|
133 | return NIL; |
---|
134 | } |
---|
135 | |
---|
136 | if (defaultedPathname.getName().equals(NIL) |
---|
137 | && defaultedPathname.getType().equals(NIL)) { |
---|
138 | return Symbol.PROBE_FILE.execute(defaultedPathname); |
---|
139 | } |
---|
140 | SimpleString lastDirectory = (SimpleString)Symbol.FILE_NAMESTRING.execute(defaultedPathname); |
---|
141 | LispObject appendedDirectory |
---|
142 | = defaultedPathname.getDirectory().reverse().push(lastDirectory).reverse(); |
---|
143 | defaultedPathname.setDirectory(appendedDirectory); |
---|
144 | return Symbol.PROBE_FILE.execute(defaultedPathname); |
---|
145 | } |
---|
146 | }; |
---|
147 | |
---|
148 | public static final Primitive FILE_DIRECTORY_P |
---|
149 | = new pf_file_directory_p(); |
---|
150 | @DocString(name="file-directory-p", |
---|
151 | args="pathspec &key (wild-error-p t)", |
---|
152 | returns="generalized-boolean") |
---|
153 | private static final class pf_file_directory_p extends Primitive { |
---|
154 | pf_file_directory_p() { |
---|
155 | super("file-directory-p", PACKAGE_EXT, true); |
---|
156 | } |
---|
157 | |
---|
158 | private LispObject isDirectory(Pathname p) { |
---|
159 | LispObject result = PROBE_DIRECTORY.execute(p); |
---|
160 | return result.equals(NIL) ? NIL : T; |
---|
161 | } |
---|
162 | |
---|
163 | @Override |
---|
164 | public LispObject execute(LispObject arg) // XXX Should this merge with defaults? |
---|
165 | { |
---|
166 | Pathname pathname = coerceToPathname(arg); |
---|
167 | if (pathname.isWild()) { |
---|
168 | error(new FileError("Fundamentally unable to determine whether a wild pathname is a directory.", |
---|
169 | pathname)); |
---|
170 | } |
---|
171 | return isDirectory(pathname); |
---|
172 | } |
---|
173 | |
---|
174 | @Override |
---|
175 | public LispObject execute(LispObject arg, LispObject wildErrorPKeyword, LispObject wildErrorP) |
---|
176 | { |
---|
177 | if (!(wildErrorPKeyword.equals(Keyword.WILD_ERROR_P))) { |
---|
178 | type_error(wildErrorPKeyword, Keyword.WILD_ERROR_P); |
---|
179 | } |
---|
180 | Pathname pathname = coerceToPathname(arg); |
---|
181 | if (wildErrorP != NIL) { |
---|
182 | if (pathname.isWild()) { |
---|
183 | error(new FileError("Fundamentally to determine whether a wild pathname is a directory.", |
---|
184 | pathname)); |
---|
185 | } |
---|
186 | } |
---|
187 | return isDirectory(pathname); |
---|
188 | } |
---|
189 | }; |
---|
190 | } |
---|