1 | /* |
---|
2 | * Utilities.java |
---|
3 | * |
---|
4 | * Copyright (C) 2003-2007 Peter Graves |
---|
5 | * $Id: Utilities.java 11700 2009-03-06 20:21:01Z ehuelsmann $ |
---|
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 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import java.io.File; |
---|
37 | import java.io.IOException; |
---|
38 | |
---|
39 | public final class Utilities extends Lisp |
---|
40 | { |
---|
41 | public static final boolean isPlatformUnix; |
---|
42 | public static final boolean isPlatformWindows; |
---|
43 | |
---|
44 | static { |
---|
45 | String osName = System.getProperty("os.name"); |
---|
46 | isPlatformUnix = osName.startsWith("Linux") || |
---|
47 | osName.startsWith("Mac OS X") || osName.startsWith("Solaris") || |
---|
48 | osName.startsWith("SunOS") || osName.startsWith("AIX") || |
---|
49 | osName.startsWith("FreeBSD") || osName.startsWith("OpenBSD") || |
---|
50 | osName.startsWith("NetBSD"); |
---|
51 | isPlatformWindows = osName.startsWith("Windows"); |
---|
52 | } |
---|
53 | |
---|
54 | public static boolean isFilenameAbsolute(String filename) |
---|
55 | { |
---|
56 | final int length = filename.length(); |
---|
57 | if (length > 0) { |
---|
58 | char c0 = filename.charAt(0); |
---|
59 | if (c0 == '\\' || c0 == '/') |
---|
60 | return true; |
---|
61 | if (length > 2) { |
---|
62 | if (isPlatformWindows) { |
---|
63 | // Check for drive letter. |
---|
64 | char c1 = filename.charAt(1); |
---|
65 | if (c1 == ':') { |
---|
66 | if (c0 >= 'a' && c0 <= 'z') |
---|
67 | return true; |
---|
68 | if (c0 >= 'A' && c0 <= 'Z') |
---|
69 | return true; |
---|
70 | } |
---|
71 | } else { |
---|
72 | // Unix. |
---|
73 | if (filename.equals("~") || filename.startsWith("~/")) |
---|
74 | return true; |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | return false; |
---|
79 | } |
---|
80 | |
---|
81 | public static File getFile(Pathname pathname) throws ConditionThrowable |
---|
82 | { |
---|
83 | return getFile(pathname, |
---|
84 | coerceToPathname(Symbol.DEFAULT_PATHNAME_DEFAULTS.symbolValue())); |
---|
85 | } |
---|
86 | |
---|
87 | public static File getFile(Pathname pathname, Pathname defaultPathname) |
---|
88 | throws ConditionThrowable |
---|
89 | { |
---|
90 | Pathname merged = |
---|
91 | Pathname.mergePathnames(pathname, defaultPathname, NIL); |
---|
92 | String namestring = merged.getNamestring(); |
---|
93 | if (namestring != null) |
---|
94 | return new File(namestring); |
---|
95 | error(new FileError("Pathname has no namestring: " + merged.writeToString(), |
---|
96 | merged)); |
---|
97 | // Not reached. |
---|
98 | return null; |
---|
99 | } |
---|
100 | |
---|
101 | public static Pathname getDirectoryPathname(File file) |
---|
102 | throws ConditionThrowable |
---|
103 | { |
---|
104 | try { |
---|
105 | String namestring = file.getCanonicalPath(); |
---|
106 | if (namestring != null && namestring.length() > 0) { |
---|
107 | if (namestring.charAt(namestring.length() - 1) != File.separatorChar) |
---|
108 | namestring = namestring.concat(File.separator); |
---|
109 | } |
---|
110 | return new Pathname(namestring); |
---|
111 | } |
---|
112 | catch (IOException e) { |
---|
113 | error(new LispError(e.getMessage())); |
---|
114 | // Not reached. |
---|
115 | return null; |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|