/[classpath]/classpath/java/io/File.java
ViewVC logotype

Diff of /classpath/java/io/File.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.16 by mark, Mon Mar 4 21:48:43 2002 UTC revision 1.17 by cbj, Mon Mar 25 05:12:18 2002 UTC
# Line 43  import java.net.URL; Line 43  import java.net.URL;
43  import java.net.MalformedURLException;  import java.net.MalformedURLException;
44    
45  import gnu.classpath.Configuration;  import gnu.classpath.Configuration;
46    import gnu.java.io.PlatformHelper;
47    
48  /**  /**
49    * This class represents a file or directory on a local disk.  It provides    * This class represents a file or directory on a local disk.  It provides
# Line 311  File(File directory, String name) Line 312  File(File directory, String name)
312  public  public
313  File(String dirname, String name)  File(String dirname, String name)
314  {  {
315    this((name == null || dirname == null) ? name : dirname + separator + name);    this (name); //set path field & check null
316      if (!isAbsolute ())
317        {
318          if (dirname != null)
319            {
320              if (PlatformHelper.endWithSeparator (dirname))
321                path = dirname + name;
322              else
323                path = dirname + separator + name;
324            }
325        }
326  }  }
327    
328  /*************************************************************************/  /*************************************************************************/
# Line 348  File(String name) Line 359  File(String name)
359  public String  public String
360  getName()  getName()
361  {  {
362    int pos = path.lastIndexOf(separator);    int pos = PlatformHelper.lastIndexOfSeparator (path);
363    if (pos == -1)    if (pos == -1)
364      return(path);      return(path);
365    
366    if (path.endsWith(separator))    if (PlatformHelper.endWithSeparator (path))
367      return("");      return("");
368    
369    return(path.substring(pos + separator.length()));    return(path.substring(pos + separator.length()));
# Line 386  getPath() Line 397  getPath()
397  public String  public String
398  getAbsolutePath()  getAbsolutePath()
399  {  {
400    if (path.startsWith(separator))    if (isAbsolute ())
401      return(path);      return path;
402      
403      String dir = System.getProperty ("user.dir");
404      if (dir == null)
405        return path;
406    
407    return(System.getProperty("user.dir") + separator + path);    if (PlatformHelper.endWithSeparator (dir))
408        return dir + path;
409    
410      return dir + separator + path;
411  }  }
412    
413  /*************************************************************************/  /*************************************************************************/
# Line 425  public String Line 443  public String
443  getCanonicalPath() throws IOException  getCanonicalPath() throws IOException
444  {  {
445    String abspath = getAbsolutePath();    String abspath = getAbsolutePath();
446    StringBuffer canonpath = new StringBuffer(separator);    return PlatformHelper.toCanonicalForm(abspath);
   StringTokenizer st = new StringTokenizer(abspath, separator);  
   
   // Traverse each element of the path, handling "." and ".."  
   // Should be handle "~" too?  
   if (st.hasMoreTokens())  
     do  
       {  
         String s = st.nextToken();  
     
         // Handle "." or an empty element.    
         if (s.equals(".") || s.equals(""))  
           continue;  
     
         // Handle ".." by deleting the last element from the path  
         if (s.equals(".."))  
           {  
             if (canonpath.equals(separator))  
               continue;  
     
             // Strip of trailing separator  
             String tmpstr = canonpath.toString().substring(0,  
                               canonpath.length() - separator.length());  
             int idx = tmpstr.lastIndexOf(separator);  
             if ((idx == -1) || ((idx + separator.length()) > tmpstr.length()))  
               throw new IOException("Can't happen error"); // Shouldn't happen  
     
             tmpstr = tmpstr.substring(0, idx + separator.length());  
             canonpath = new StringBuffer(tmpstr);  
             continue;  
           }        
     
         canonpath.append(s);  
         if (st.hasMoreTokens())  
           canonpath.append(separator);  
       }  
     while(st.hasMoreTokens());  
   
   String tmpstr = canonpath.toString();  
   if (tmpstr.endsWith(separator) && !tmpstr.equals(separator))  
     tmpstr = tmpstr.substring(0, tmpstr.length() - 1);  
   
   return(tmpstr);  
447  }  }
448    
449  /*************************************************************************/  /*************************************************************************/
# Line 499  getCanonicalFile() throws IOException Line 475  getCanonicalFile() throws IOException
475  public String  public String
476  getParent()  getParent()
477  {  {
478    if (path.equals("/"))    if (PlatformHelper.isRootDirectory(path))
479      return(null);      return null;
   
   String par_path;  
   if (path.endsWith(separator))  
     par_path = path.substring(0, path.length() - 1);  
   else  
     par_path = path;  
480    
481    int pos = par_path.lastIndexOf(separator);    String par_path = PlatformHelper.removeTailSeparator(path);
482      int pos = PlatformHelper.lastIndexOfSeparator(path);
483    if (pos == -1)    if (pos == -1)
484      return(null);      return null;
485    
486    return(par_path.substring(0, pos));    return(par_path.substring(0, pos));
487  }  }
# Line 547  getParentFile() Line 518  getParentFile()
518  public boolean  public boolean
519  isAbsolute()  isAbsolute()
520  {  {
521    if (path.startsWith(separator))    if (PlatformHelper.beginWithRootPathPrefix (path) > 0)
522      return(true);      return(true);
523    else    else
524      return(false);      return(false);
# Line 1073  mkdir() throws SecurityException Line 1044  mkdir() throws SecurityException
1044      }      }
1045    
1046    String mk_path;    String mk_path;
1047    if (path.endsWith(separator) && !path.equals(separator))    mk_path = PlatformHelper.removeTailSeparator(path);
     mk_path = path.substring(0, path.length() - 1);  
   else  
     mk_path = path;  
1048        
1049    return(mkdirInternal(mk_path));    return(mkdirInternal(mk_path));
1050  }  }
# Line 1229  list(FilenameFilter filter) Line 1197  list(FilenameFilter filter)
1197      }      }
1198    
1199    // Get the list of files    // Get the list of files
1200    String list_path;    String list_path = PlatformHelper.removeTailSeparator(path);
1201    if (path.endsWith(separator) && !path.equals(separator))  
     list_path = path.substring(0, path.length() - 1);  
   else  
     list_path = path;  
1202        
1203    String files[] = listInternal(list_path);    String files[] = listInternal(list_path);
1204    if (files == null)    if (files == null)
# Line 1510  toString() Line 1475  toString()
1475  public URL  public URL
1476  toURL() throws MalformedURLException  toURL() throws MalformedURLException
1477  {  {
1478    String url_string = "file://" + getAbsolutePath();    String abspath = getAbsolutePath();
1479      try
1480        {
1481          if(new File(abspath).isDirectory())
1482            abspath = abspath + separator;
1483        }
1484      catch(Exception _) { }
1485      
1486      String url_string = "file://" + abspath;
1487      
1488    return(new URL(url_string));    return(new URL(url_string));
1489  }  }
1490    

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.17

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26