diff options
author | Ranjit Mathew <rmathew@hotmail.com> | 2003-04-19 19:08:49 +0000 |
---|---|---|
committer | Tom Tromey <tromey@gcc.gnu.org> | 2003-04-19 19:08:49 +0000 |
commit | f1a1591b9ef91e138109a510a92c60ee153b8555 (patch) | |
tree | fecad7245f02a88a53f8de92df88e2c776e68ba8 /libjava | |
parent | 30f45f509fb75efdad29f9f033057af8a6ef8d15 (diff) | |
download | gcc-f1a1591b9ef91e138109a510a92c60ee153b8555.zip gcc-f1a1591b9ef91e138109a510a92c60ee153b8555.tar.gz gcc-f1a1591b9ef91e138109a510a92c60ee153b8555.tar.bz2 |
File.java (getAbsolutePath): On Windows, take care of paths like "C:", "G:foo\bar", etc.
2003-04-19 Ranjit Mathew <rmathew@hotmail.com>
* java/io/File.java (getAbsolutePath): On Windows, take care
of paths like "C:", "G:foo\bar", etc.
(getName): Make it work correctly on Windows.
(getParent): Make it work correctly on Windows. For UNIX,
fix bug that causes "/" to be returned as the parent of "/",
instead of null as returned by Sun's JRE.
* java/io/natFileWin32.cc: Change copyright owner to FSF.
From-SVN: r65823
Diffstat (limited to 'libjava')
-rw-r--r-- | libjava/ChangeLog | 11 | ||||
-rw-r--r-- | libjava/java/io/File.java | 108 | ||||
-rw-r--r-- | libjava/java/io/natFileWin32.cc | 4 |
3 files changed, 113 insertions, 10 deletions
diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 498a6b0..d7d7bd3 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,14 @@ +2003-04-19 Ranjit Mathew <rmathew@hotmail.com> + + * java/io/File.java (getAbsolutePath): On Windows, take care + of paths like "C:", "G:foo\bar", etc. + (getName): Make it work correctly on Windows. + (getParent): Make it work correctly on Windows. For UNIX, + fix bug that causes "/" to be returned as the parent of "/", + instead of null as returned by Sun's JRE. + + * java/io/natFileWin32.cc: Change copyright owner to FSF. + 2003-04-19 Scott Gilbertson <scottg@mantatest.com> * gnu/awt/xlib/XGraphicsConfiguration.java (FontMetricsCache): New diff --git a/libjava/java/io/File.java b/libjava/java/io/File.java index 83e4c2e..56d3a63 100644 --- a/libjava/java/io/File.java +++ b/libjava/java/io/File.java @@ -396,6 +396,33 @@ public class File implements Serializable, Comparable // the current working directory to it. return System.getProperty ("user.dir").substring (0, 2) + path; } + else if (separatorChar == '\\' + && path.length () > 1 && path.charAt (1) == ':' + && ((path.charAt (0) >= 'a' && path.charAt (0) <= 'z') + || (path.charAt (0) >= 'A' && path.charAt (0) <= 'Z'))) + { + // On Windows, a process has a current working directory for + // each drive and a path like "G:foo\bar" would mean the + // absolute path "G:\wombat\foo\bar" if "\wombat" is the + // working directory on the G drive. + String drvDir = null; + try + { + drvDir = new File (path.substring (0, 2)).getCanonicalPath (); + } + catch (IOException e) + { + drvDir = path.substring (0, 2) + "\\"; + } + + // Note: this would return "C:\\." for the path "C:.", if "\" + // is the working folder on the C drive, but this is + // consistent with what Sun's JRE 1.4.1.01 actually returns! + if (path.length () > 2) + return drvDir + '\\' + path.substring (2, path.length ()); + else + return drvDir; + } else return System.getProperty ("user.dir") + separatorChar + path; } @@ -453,8 +480,30 @@ public class File implements Serializable, Comparable */ public String getName () { - int last = path.lastIndexOf(separatorChar); - return path.substring(last + 1); + int nameSeqIndex = 0; + + if (separatorChar == '\\' && path.length () > 1) + { + // On Windows, ignore the drive specifier or the leading '\\' + // of a UNC network path, if any (a.k.a. the "prefix"). + if ((path.charAt (0) == '\\' && path.charAt (1) == '\\') + || (((path.charAt (0) >= 'a' && path.charAt (0) <= 'z') + || (path.charAt (0) >= 'A' && path.charAt (0) <= 'Z')) + && path.charAt (1) == ':')) + { + if (path.length () > 2) + nameSeqIndex = 2; + else + return ""; + } + } + + String nameSeq + = (nameSeqIndex > 0 ? path.substring (nameSeqIndex) : path); + + int last = nameSeq.lastIndexOf (separatorChar); + + return nameSeq.substring (last + 1); } /** @@ -466,13 +515,56 @@ public class File implements Serializable, Comparable */ public String getParent () { - int last = path.lastIndexOf(separatorChar); - if (last == -1) + String prefix = null; + int nameSeqIndex = 0; + + // The "prefix", if present, is the leading "/" on UNIX and + // either the drive specifier (e.g. "C:") or the leading "\\" + // of a UNC network path on Windows. + if (separatorChar == '/' && path.charAt (0) == '/') + { + prefix = "/"; + nameSeqIndex = 1; + } + else if (separatorChar == '\\' && path.length () > 1) + { + if ((path.charAt (0) == '\\' && path.charAt (1) == '\\') + || (((path.charAt (0) >= 'a' && path.charAt (0) <= 'z') + || (path.charAt (0) >= 'A' && path.charAt (0) <= 'Z')) + && path.charAt (1) == ':')) + { + prefix = path.substring (0, 2); + nameSeqIndex = 2; + } + } + + // According to the JDK docs, the returned parent path is the + // portion of the name sequence before the last separator + // character, if found, prefixed by the prefix, otherwise null. + if (nameSeqIndex < path.length ()) + { + String nameSeq = path.substring (nameSeqIndex, path.length ()); + int last = nameSeq.lastIndexOf (separatorChar); + if (last == -1) + return prefix; + else if (last == (nameSeq.length () - 1)) + // Note: The path would not have a trailing separator + // except for cases like "C:\" on Windows (see + // normalizePath( )), where Sun's JRE 1.4 returns null. + return null; + else if (last == 0) + last++; + + if (prefix != null) + return prefix + nameSeq.substring (0, last); + else + return nameSeq.substring (0, last); + } + else + // Sun's JRE 1.4 returns null if the prefix is the only + // component of the path - so "/" gives null on UNIX and + // "C:", "\\", etc. return null on Windows. return null; - // FIXME: POSIX assumption. - if (last == 0 && path.charAt (0) == '/') - ++last; - return path.substring(0, last); } /** diff --git a/libjava/java/io/natFileWin32.cc b/libjava/java/io/natFileWin32.cc index af12f5b..1e06832 100644 --- a/libjava/java/io/natFileWin32.cc +++ b/libjava/java/io/natFileWin32.cc @@ -1,6 +1,6 @@ -// natFileWin32.cc - Native part of File class. +// natFileWin32.cc - Native part of File class for Win32. -/* Copyright (C) 1998, 1999, 2002, 2003 Red Hat, Inc. +/* Copyright (C) 1998, 1999, 2002, 2003 Free Software Foundation, Inc. This file is part of libgcj. |