001: /*
002: * Utilities.java
003: *
004: * Copyright (C) 2003 Peter Graves
005: * $Id: Utilities.java,v 1.6 2003/11/15 11:03:33 beedlem 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:
026: public final class Utilities extends Lisp {
027: private static final boolean isPlatformUnix;
028: private static final boolean isPlatformWindows;
029:
030: static {
031: String osName = System.getProperty("os.name");
032: isPlatformUnix = osName.startsWith("Linux")
033: || osName.startsWith("Mac OS X")
034: || osName.startsWith("Solaris")
035: || osName.startsWith("SunOS")
036: || osName.startsWith("AIX");
037: isPlatformWindows = osName.startsWith("Windows");
038: }
039:
040: public static boolean isPlatformUnix() {
041: return isPlatformUnix;
042: }
043:
044: public static boolean isPlatformWindows() {
045: return isPlatformWindows;
046: }
047:
048: public static boolean isFilenameAbsolute(String filename) {
049: final int length = filename.length();
050: if (length > 0) {
051: char c0 = filename.charAt(0);
052: if (c0 == '\\' || c0 == '/')
053: return true;
054: if (length > 2) {
055: if (isPlatformWindows()) {
056: // Check for drive letter.
057: char c1 = filename.charAt(1);
058: if (c1 == ':') {
059: if (c0 >= 'a' && c0 <= 'z')
060: return true;
061: if (c0 >= 'A' && c0 <= 'Z')
062: return true;
063: }
064: } else {
065: // Unix.
066: if (filename.equals("~")
067: || filename.startsWith("~/"))
068: return true;
069: }
070: }
071: }
072: return false;
073: }
074:
075: public static File getFile(LispObject pathspec)
076: throws ConditionThrowable {
077: String namestring;
078: if (pathspec instanceof LispString)
079: namestring = ((LispString) pathspec).getValue();
080: else if (pathspec instanceof Pathname)
081: namestring = ((Pathname) pathspec).getNamestring();
082: else
083: throw new ConditionThrowable(new TypeError(pathspec,
084: "pathname designator"));
085: if (isFilenameAbsolute(namestring)) {
086: if (isPlatformUnix()) {
087: if (namestring.length() > 0
088: && namestring.charAt(0) == '~') {
089: namestring = System.getProperty("user.home")
090: .concat(namestring.substring(1));
091: }
092: }
093: return new File(namestring);
094: }
095: return new File(LispString.getValue(_DEFAULT_PATHNAME_DEFAULTS_
096: .symbolValue()), namestring);
097: }
098:
099: public static final char toUpperCase(char c) {
100: if (c < 128)
101: return UPPER_CASE_CHARS[c];
102: return Character.toUpperCase(c);
103: }
104:
105: private static final char[] UPPER_CASE_CHARS = new char[128];
106:
107: static {
108: for (int i = UPPER_CASE_CHARS.length; i-- > 0;)
109: UPPER_CASE_CHARS[i] = Character.toUpperCase((char) i);
110: }
111:
112: public static final char toLowerCase(char c) {
113: if (c < 128)
114: return LOWER_CASE_CHARS[c];
115: return Character.toLowerCase(c);
116: }
117:
118: private static final char[] LOWER_CASE_CHARS = new char[128];
119:
120: static {
121: for (int i = LOWER_CASE_CHARS.length; i-- > 0;)
122: LOWER_CASE_CHARS[i] = Character.toLowerCase((char) i);
123: }
124: }
|