001: /*
002: * Utilities.java
003: *
004: * Copyright (C) 2003-2004 Peter Graves
005: * $Id: Utilities.java,v 1.8 2004/01/26 00:30:12 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: import java.io.File;
025: import java.io.IOException;
026:
027: public final class Utilities extends Lisp {
028: private static final boolean isPlatformUnix;
029: private static final boolean isPlatformWindows;
030:
031: static {
032: String osName = System.getProperty("os.name");
033: isPlatformUnix = osName.startsWith("Linux")
034: || osName.startsWith("Mac OS X")
035: || osName.startsWith("Solaris")
036: || osName.startsWith("SunOS")
037: || osName.startsWith("AIX");
038: isPlatformWindows = osName.startsWith("Windows");
039: }
040:
041: public static boolean isPlatformUnix() {
042: return isPlatformUnix;
043: }
044:
045: public static boolean isPlatformWindows() {
046: return isPlatformWindows;
047: }
048:
049: public static boolean isFilenameAbsolute(String filename) {
050: final int length = filename.length();
051: if (length > 0) {
052: char c0 = filename.charAt(0);
053: if (c0 == '\\' || c0 == '/')
054: return true;
055: if (length > 2) {
056: if (isPlatformWindows()) {
057: // Check for drive letter.
058: char c1 = filename.charAt(1);
059: if (c1 == ':') {
060: if (c0 >= 'a' && c0 <= 'z')
061: return true;
062: if (c0 >= 'A' && c0 <= 'Z')
063: return true;
064: }
065: } else {
066: // Unix.
067: if (filename.equals("~")
068: || filename.startsWith("~/"))
069: return true;
070: }
071: }
072: }
073: return false;
074: }
075:
076: public static File getFile(Pathname pathname)
077: throws ConditionThrowable {
078: Pathname defaultPathname = Pathname
079: .coerceToPathname(_DEFAULT_PATHNAME_DEFAULTS_
080: .symbolValue());
081: Pathname merged = Pathname.mergePathnames(pathname,
082: defaultPathname, NIL);
083: String namestring = merged.getNamestring();
084: if (namestring != null)
085: return new File(namestring);
086: signal(new SimpleError("Pathname has no namestring: " + merged));
087: // Not reached.
088: return null;
089: }
090:
091: public static Pathname getDirectoryPathname(File file)
092: throws ConditionThrowable {
093: try {
094: String namestring = file.getCanonicalPath();
095: if (namestring != null && namestring.length() > 0) {
096: if (namestring.charAt(namestring.length() - 1) != File.separatorChar)
097: namestring = namestring.concat(File.separator);
098: }
099: return new Pathname(namestring);
100: } catch (IOException e) {
101: signal(new LispError(e.getMessage()));
102: // Not reached.
103: return null;
104: }
105: }
106:
107: public static final char toUpperCase(char c) {
108: if (c < 128)
109: return UPPER_CASE_CHARS[c];
110: return Character.toUpperCase(c);
111: }
112:
113: private static final char[] UPPER_CASE_CHARS = new char[128];
114:
115: static {
116: for (int i = UPPER_CASE_CHARS.length; i-- > 0;)
117: UPPER_CASE_CHARS[i] = Character.toUpperCase((char) i);
118: }
119:
120: public static final char toLowerCase(char c) {
121: if (c < 128)
122: return LOWER_CASE_CHARS[c];
123: return Character.toLowerCase(c);
124: }
125:
126: private static final char[] LOWER_CASE_CHARS = new char[128];
127:
128: static {
129: for (int i = LOWER_CASE_CHARS.length; i-- > 0;)
130: LOWER_CASE_CHARS[i] = Character.toLowerCase((char) i);
131: }
132: }
|