dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/IO Directory.cs, 1.18,


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/IO Directory.cs, 1.18, 1.19 Path.cs, 1.16, 1.17
Date: Mon, 18 Aug 2003 02:42:50 -0400

Update of /cvsroot/dotgnu-pnet/pnetlib/runtime/System/IO
In directory subversions:/tmp/cvs-serv24933/runtime/System/IO

Modified Files:
        Directory.cs Path.cs 
Log Message:


Rewrite the "System.IO.Path" class and add a large number of test cases for it.


Index: Directory.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/runtime/System/IO/Directory.cs,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -r1.18 -r1.19
*** Directory.cs        6 Aug 2003 05:48:29 -0000       1.18
--- Directory.cs        18 Aug 2003 06:42:46 -0000      1.19
***************
*** 213,216 ****
--- 213,240 ----
                        }
  
+       // Get the current working directory for a particular drive.
+       internal static String GetCurrentDirectory(char drive)
+                       {
+                               if(drive >= 'a' && drive <= 'z')
+                               {
+                                       drive = (char)(drive - 'a' + 'A');
+                               }
+                               String current = GetCurrentDirectory();
+                               if(current.Length >= 2 && 
Path.IsVolumeSeparator(current[1]))
+                               {
+                                       char d = current[0];
+                                       if(d >= 'a' && d <= 'z')
+                                       {
+                                               d = (char)(d - 'a' + 'A');
+                                       }
+                                       if(d == drive)
+                                       {
+                                               return current;
+                                       }
+                               }
+                               return drive.ToString() + ":" +
+                                          
Path.DirectorySeparatorChar.ToString();
+                       }
+ 
        // Directory scan types.
        internal enum ScanType

Index: Path.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/runtime/System/IO/Path.cs,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -r1.16 -r1.17
*** Path.cs     5 Aug 2003 05:13:03 -0000       1.16
--- Path.cs     18 Aug 2003 06:42:46 -0000      1.17
***************
*** 2,9 ****
   * Path.cs - Implementation of the "System.IO.Path" class.
   *
!  * Copyright (C) 2001, 2003  Southern Storm Software, Pty Ltd.
!  *
!  * Contributions from Charlie Carnow <address@hidden>
!  * Contributions from Gopal V <address@hidden>
   *
   * This program is free software; you can redistribute it and/or modify
--- 2,6 ----
   * Path.cs - Implementation of the "System.IO.Path" class.
   *
!  * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
   *
   * This program is free software; you can redistribute it and/or modify
***************
*** 22,312 ****
   */
  
- 
  namespace System.IO
  {
!       using System;
!       using System.Text;
!       using Platform;
!       public sealed class Path 
!       {
!               // I know of the existence of System.Enviornment
!               // But I'm opting to just get the separators
!               // from DirMethods.GetPathInfo()
!               private static PathInfo p = DirMethods.GetPathInfo();
!               public static readonly char DirectorySeparatorChar = 
p.dirSeparator;
!               public static readonly char AltDirectorySeparatorChar = 
p.altDirSeparator;
!               public static readonly char PathSeparator = p.pathSeparator;
  #if ECMA_COMPAT
!               internal static readonly char VolumeSeparatorChar = 
p.volumeSeparator;
!               internal static readonly char[] InvalidPathChars = 
p.invalidPathChars;
  #else
!               public static readonly char VolumeSeparatorChar = 
p.volumeSeparator;
!               public static readonly char[] InvalidPathChars = 
p.invalidPathChars;
  #endif
  
!               // entropy for GetTempFilename()
!               static Random r=new Random();
  
!               // This class cannot be instantiated.
!               private Path() {}
  
!               public static String ChangeExtension(String path, String 
extension)
!               {
!                       if(!HasExtension(path))return path;
!                       String fname=path.Substring(0,path.LastIndexOf('.'));
!                       return fname+'.'+extension;
!               }
!               
!               
!               public static String Combine(String path1, String path2)
!               {
! 
!                       if (path1.Length == 0) 
!                       {
!                               return path2;
!                       }
!                       else if (path2.Length == 0)
!                       {
!                               return path1;
!                       }
!                       if (path1 == null || path2 == null)
!                       {
!                               throw new ArgumentNullException();
!                       }
!                       
!                       /* TODO:Check for implementation-defined invalid
!                        * chars
!                        */
!                       
!                       if (path1.EndsWith(new String(DirectorySeparatorChar, 
1)) == false 
!                           && path2.EndsWith(new 
String(AltDirectorySeparatorChar,1))
!                               == false) 
!                       {
!                                // Insert at starting at last element
!                                path1 = path1.Insert(path1.Length, new 
String(DirectorySeparatorChar, 1));
!                       }
! 
!                       return String.Concat(path1, path2);
!               }
!               
!               public static String GetDirectoryName(String path)
!               {
!                       if(path==null)return null;
!       
!                       bool isDir=path.EndsWith(""+DirectorySeparatorChar);
!                       String retval;
!                       if(!isDir)
!                       {
!                               int len=path.Length-GetFileName(path).Length;
!                               if(len >1 && 
path[len-1]==DirectorySeparatorChar)len--;
!                               // skip last slash for all dirs except '/file' 
=> '/'
!                               retval=path.Substring(0,len);
!                       }
!                       else
!                       {
!                               if(path.Length==1)return null; // Viz '/'
!                               if(path.Length==3 && 
path[1]==VolumeSeparatorChar)
!                                       return null; // C:\
!                               retval=path.Substring(0,path.Length-1);
!                       }
!                       return retval;
!               }
! 
!               public static String GetFileName(String path)
!               {
!                       if(path==null)return null;
!                       if(path.Length==0)return String.Empty;
!                   char[] separator = 
{DirectorySeparatorChar,AltDirectorySeparatorChar};
!                       /* Optimize: Replace with LastIndexOf */
!                   string[] dirs = path.Split(separator);
!                       return dirs[dirs.Length-1];
!               }
!               
!               public static String GetFileNameWithoutExtension(String path)
!               {
!                       String fname=GetFileName(path);
!                       
!                       if (fname == null) return null;
! 
!                       if(fname.IndexOf('.')==-1)return fname;
!                       return  fname.Substring(0,fname.LastIndexOf('.'));
!               }
! 
!               public static String GetExtension(String path)
!               {
!                       String fname = GetFileName( path );
! 
!                       if (fname == null) return null;
! 
!                       if (fname.IndexOf( '.' ) == -1) return String.Empty;
! 
!                       return fname.Substring( fname.LastIndexOf( '.' ) );
!               }
!               
!               public static String GetFullPath(String path)
!               {
!                       if(path==null)return null;
! 
!                       String dir=path;
!                       
if(!IsPathRooted(dir))dir=DirMethods.GetCurrentDirectory()+'/'+
!                               path;
!                       dir=Normalize(dir);     
!                       // TODO: Permission check
!                       return dir;
!               }
!               
!               private static String Normalize(String path)
!               {
!                       StringBuilder sb=new StringBuilder(path.Length);
!                       if(!IsPathRooted(path))return path;
!                       String []frags=path.Split(new 
char[]{VolumeSeparatorChar,
!                                                                       
DirectorySeparatorChar});
!                       for(int i=0;i<frags.Length;i++)
!                       {
!                               if(frags[i]=="..")
!                               {
!                                       if(i==1)
!                                       {
!                                               throw new 
ArgumentException("path");
!                                       }
!                                       
sb.Length=sb.Length-frags[i-1].Length-1; 
!                                       /* Hack : instead of 'removing' , I 
just trim it 
!                                          like this , faster this way */
!                               }
!                               else if(frags[i]==".")
!                               {
!                                       // do nothing
!                               }
!                               else if(frags[i].Length>0)
!                               {
!                                       sb.Append('/');
!                                       sb.Append(frags[i]);
!                               }
!                       }
!                       return sb.ToString();
!               }
!               
!               public static String GetPathRoot(String path)
!               {
!                       char[] pathChars;
!                       int i = 0;  /*  Look an index var for my for loop  */
!                       bool passedServerDelimeter = false;  
!  
!                       if(path == null)
!                       {
!                               throw new ArgumentNullException();
!                       }
!                       if(path == "")
!                       {
!                               throw new ArgumentException();
!                       }
!                       
!                       if(!IsPathRooted(path))path=Path.GetFullPath(path);
!  
!                       /*  Now that were done checking what path is, we can 
!                           devote time to busting it up into an array  */
!                       pathChars = path.ToCharArray();
!  
!                       if((DirectorySeparatorChar=='\\') 
!                               && (pathChars[0] == '\\')
!                               && (pathChars[1] == '\\'))
!                       {
!                               /*  Looks like we have a SMB/CIFS/Silly MS 
Drive Share thing 
!                                       happening  Starting at index 2 because 
we just checked 
!                                       indicies 0 and 1 above  */
!                               for(i = 2; i < path.length; i++)
!                               {
!                                       if(pathChars[i] == 
DirectorySeparatorChar
!                                               && passedServerDelimeter == 
false)
!                                       {
!                                               passedServerDelimeter = true;
!                                       }
!                                       else if(pathChars[i] == 
DirectorySeparatorChar
!                                               && passedServerDelimeter == 
true)
!                                       {
!                                               return new String(pathChars, 0, 
i);
!                                       }
!                               }
!                               
!                               if(passedServerDelimeter == true)
!                               {
!                                       /*  Have to assume the passed in string 
is actually 
!                                               the root  */
!                                       return path;
!                               }
!                               else
!                               {
!                                       /*  The passed in result is not a valid 
SMB resource 
!                                               locator  */
!                                       /*  The spec isn't too clear on this, 
but I'm throwing 
!                                               an argument exception  */
!                                       throw new ArgumentException();
!                               }
!                       }
!                       return new String(pathChars[0],1);
!               }
!               
!               /* generate /tmp/pnetXXXXXX */
!               public static String GetTempFileName()
!               {
!                       String dir = GetTempPath();
!                       String pre = "pnet";
!                       String file;
!                       do
!                       {       
!                               file = dir +DirectorySeparatorChar+ pre + 
UniqName();
!                       }
!                       while (File.Exists(file));
!                       return file;
!               }
! 
!               private static String UniqName()
!               {
!                       String 
index="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz_";
!                       int num=r.Next(); // Take potluck
!                       String retval="";
!                       int radix=index.Length;
!                       for(int i=0;i<6;i++)
!                       {
!                               retval = retval + index[num%radix];
!                               num=num/radix;
!                       }
!                       return retval;
!               }               
!               
!               public static String GetTempPath()
!               {
!                       String 
tmp=Environment.GetEnvironmentVariable("TMPDIR"); // *nix
!                       if(tmp!=null)return tmp;
!                       tmp=Environment.GetEnvironmentVariable("TEMP"); // Win32
!                       if(tmp!=null)return tmp;
!                       return "/tmp"; 
!                       /* TODO : might just think about returning P_tmpdir 
from stdio.h */
!               }
!               
!               public static bool HasExtension(String path)
!               {
!                       if(path==null)return false;
!                       String [] elems=path.Split(DirectorySeparatorChar);
!                       String filename=elems[elems.Length-1];
!                       if(filename.IndexOf('.')!=-1)
!                       {
!                               return true;
!                       }
!                       return false;
!               }
!               
!               
!               public static bool IsPathRooted(String path)
!               {
!                       if(path==null)return false;
!                       if(path.Length==0)return false;
!                       if(path.Length==1)return 
(path[0]==DirectorySeparatorChar);     
!                       return
!                               (
!                               (path[0]==DirectorySeparatorChar) || // Unix -> 
"/foo"
!                               (path[1]==VolumeSeparatorChar)    // Win32 -> 
"C:/foo"
!                               );
!               }
!       }
! }
--- 19,657 ----
   */
  
  namespace System.IO
  {
! 
! using System;
! using System.Text;
! using Platform;
! 
! public sealed class Path
! {
!       // Cannot instantiate this class.
!       private Path() {}
! 
!       // Special operating system characters.
!       public static readonly char AltDirectorySeparatorChar;
!       public static readonly char DirectorySeparatorChar;
!       public static readonly char PathSeparator;
  #if ECMA_COMPAT
!       internal static readonly char[] InvalidPathChars;
!       internal static readonly char VolumeSeparatorChar;
  #else
!       public static readonly char[] InvalidPathChars;
!       public static readonly char VolumeSeparatorChar;
  #endif
  
!       // Initialize the special operating system characters.
!       static Path()
!                       {
!                               PathInfo info = DirMethods.GetPathInfo();
!                               AltDirectorySeparatorChar = 
info.altDirSeparator;
!                               DirectorySeparatorChar = info.dirSeparator;
!                               PathSeparator = info.pathSeparator;
!                               VolumeSeparatorChar = info.volumeSeparator;
!                               InvalidPathChars = info.invalidPathChars;
!                       }
! 
!       // Determine if a character is a directory separator.
!       private static bool IsSeparator(char ch)
!                       {
!                               // We always check for '/' and '\\' because 
Windows
!                               // programmers have been known to hard-wire them
!                               // into their code.
!                               if(ch == '/' || ch == '\\' ||
!                                  ch == DirectorySeparatorChar ||
!                                  (AltDirectorySeparatorChar != '\0' &&
!                                   ch == AltDirectorySeparatorChar))
!                               {
!                                       return true;
!                               }
!                               else
!                               {
!                                       return false;
!                               }
!                       }
! 
!       // Determine if a character is a directory or volume separator.
!       private static bool IsDirectoryOrVolumeSeparator(char ch)
!                       {
!                               // We always check for '/', '\\', and ':' 
because Windows
!                               // programmers have been known to hard-wire them
!                               // into their code.
!                               if(ch == '/' || ch == '\\' || ch == ':' ||
!                                  ch == DirectorySeparatorChar ||
!                                  (AltDirectorySeparatorChar != '\0' &&
!                                   ch == AltDirectorySeparatorChar) ||
!                                  (VolumeSeparatorChar != '\0' &&
!                                   ch == VolumeSeparatorChar))
!                               {
!                                       return true;
!                               }
!                               else
!                               {
!                                       return false;
!                               }
!                       }
! 
!       // Determine if a character is a volume separator.
!       internal static bool IsVolumeSeparator(char ch)
!                       {
!                               if(ch == ':' ||
!                                  (VolumeSeparatorChar != '\0' &&
!                                   ch == VolumeSeparatorChar))
!                               {
!                                       return true;
!                               }
!                               else
!                               {
!                                       return false;
!                               }
!                       }
! 
!       // Normalize a pathname to only contain valid directory separators.
!       internal static String NormalizeSeparators(String path)
!                       {
!                               // See if the path needs modification or not.
!                               if(path == null)
!                               {
!                                       return null;
!                               }
!                               if(DirectorySeparatorChar == '/')
!                               {
!                                       // Convert backslashes into forward 
slashes.
!                                       if(path.IndexOf('\\') == -1)
!                                       {
!                                               return path;
!                                       }
!                               }
!                               else if(DirectorySeparatorChar == '\\')
!                               {
!                                       // Convert forward slashes into 
backslashes.
!                                       if(path.IndexOf('/') == -1)
!                                       {
!                                               return path;
!                                       }
!                               }
!                               else
!                               {
!                                       // Convert both forward and backslashes.
!                                       if(path.IndexOf('/') == -1 && 
path.IndexOf('\\') == -1)
!                                       {
!                                               return path;
!                                       }
!                               }
! 
!                               // Normalize the path to only use 
"DirectorySeparatorChar".
!                               StringBuilder builder = new 
StringBuilder(path.Length);
!                               foreach(char ch in path)
!                               {
!                                       if(IsSeparator(ch))
!                                       {
!                                               
builder.Append(DirectorySeparatorChar);
!                                       }
!                                       else
!                                       {
!                                               builder.Append(ch);
!                                       }
!                               }
!                               return builder.ToString();
!                       }
! 
!       // Change the extension on a file.
!       public static String ChangeExtension(String path, String extension)
!                       {
!                               if(path == null)
!                               {
!                                       return null;
!                               }
!                               int posn = path.Length;
!                               char ch;
!                               while(posn > 0)
!                               {
!                                       ch = path[--posn];
!                                       if(ch == '.')
!                                       {
!                                               return path.Substring(0, posn + 
1) + extension;
!                                       }
!                                       else 
if(IsDirectoryOrVolumeSeparator(ch))
!                                       {
!                                               return path + "." + extension;
!                                       }
!                               }
!                               return path + "." + extension;
!                       }
! 
!       // Combine two paths into one.
!       public static String Combine(String path1, String path2)
!                       {
!                               if(path1 == null)
!                               {
!                                       throw new 
ArgumentNullException("path1");
!                               }
!                               else if(path2 == null)
!                               {
!                                       throw new 
ArgumentNullException("path2");
!                               }
!                               else if(path1.Length == 0)
!                               {
!                                       return NormalizeSeparators(path2);
!                               }
!                               else if(path2.Length == 0)
!                               {
!                                       return NormalizeSeparators(path1);
!                               }
!                               else if(IsPathRooted(path2))
!                               {
!                                       return NormalizeSeparators(path2);
!                               }
!                               else 
if(IsDirectoryOrVolumeSeparator(path1[path1.Length - 1]))
!                               {
!                                       return NormalizeSeparators(path1 + 
path2);
!                               }
!                               else
!                               {
!                                       return NormalizeSeparators
!                                               (path1 + 
DirectorySeparatorChar.ToString() + path2);
!                               }
!                       }
! 
!       // Determine if a character is a drive letter.
!       private static bool IsDriveLetter(char ch)
!                       {
!                               if(ch >= 'A' && ch <= 'Z')
!                               {
!                                       return true;
!                               }
!                               if(ch >= 'a' && ch <= 'z')
!                               {
!                                       return true;
!                               }
!                               return false;
!                       }
! 
!       // Determine if a path is a root directory specification.
!       // Also validates network share names to be of the form
!       // "\\host\\share".  Returns the length of the root prefix
!       // or -1 if it does not start with a root prefix.
!       private static int GetRootPrefixLength(String path)
!                       {
!                               if(path.Length == 0)
!                               {
!                                       throw new 
ArgumentException(_("IO_InvalidPathname"));
!                               }
!                               if(IsSeparator(path[0]))
!                               {
!                                       // Check for "/" and "\\...".
!                                       if(path.Length == 1)
!                                       {
!                                               return 1;
!                                       }
!                                       if(!IsSeparator(path[1]))
!                                       {
!                                               return 1;
!                                       }
! 
!                                       // Validate the network share 
specification.
!                                       int posn = 2;
!                                       while(posn < path.Length && 
!IsSeparator(path[posn]))
!                                       {
!                                               ++posn;
!                                       }
!                                       if((posn + 1) >= path.Length)
!                                       {
!                                               // Network shares must be 
"\\foo\bar", not "\\foo".
!                                               throw new 
ArgumentException(_("IO_InvalidPathname"));
!                                       }
!                                       ++posn;
!                                       while(posn < path.Length && 
!IsSeparator(path[posn]))
!                                       {
!                                               ++posn;
!                                       }
!                                       return posn;
!                               }
!                               else if(IsDriveLetter(path[0]) && path.Length > 
1 &&
!                                               IsVolumeSeparator(path[1]))
!                               {
!                                       if(path.Length > 2 && 
IsSeparator(path[2]))
!                                       {
!                                               return 3;
!                                       }
!                                       return 2;
!                               }
!                               else
!                               {
!                                       return -1;
!                               }
!                       }
! 
!       // Extract the directory name from a path.
!       public static String GetDirectoryName(String path)
!                       {
!                               if(path == null)
!                               {
!                                       return null;
!                               }
!                               int rootLen = GetRootPrefixLength(path);
!                               if(rootLen == path.Length)
!                               {
!                                       return null;
!                               }
!                               int posn = path.Length;
!                               char ch;
!                               while(posn > 0)
!                               {
!                                       ch = path[--posn];
!                                       if(IsSeparator(ch))
!                                       {
!                                               if(posn == 0)
!                                               {
!                                                       // Return a path of the 
form "/".
!                                                       return new 
String(DirectorySeparatorChar, 1);
!                                               }
!                                               else if(posn > 0 && 
IsVolumeSeparator(path[posn - 1]))
!                                               {
!                                                       // Return a path of the 
form "X:\".
!                                                       return 
NormalizeSeparators
!                                                               
(path.Substring(0, posn + 1));
!                                               }
!                                               else
!                                               {
!                                                       return 
NormalizeSeparators
!                                                               
(path.Substring(0, posn));
!                                               }
!                                       }
!                                       else if(IsVolumeSeparator(ch))
!                                       {
!                                               // Return a path of the form 
"X:\".
!                                               path = path.Substring(0, posn + 
1);
!                                               path += 
DirectorySeparatorChar.ToString();
!                                               return 
NormalizeSeparators(path);
!                                       }
!                               }
!                               return String.Empty;
!                       }
! 
!       // Extract the file name from a path.
!       public static String GetFileName(String path)
!                       {
!                               if(path == null)
!                               {
!                                       return null;
!                               }
!                               int posn = path.Length;
!                               char ch;
!                               while(posn > 0)
!                               {
!                                       ch = path[--posn];
!                                       if(IsDirectoryOrVolumeSeparator(ch))
!                                       {
!                                               return path.Substring(posn + 1);
!                                       }
!                               }
!                               return path;
!                       }
! 
!       // Extract the file name from a path, without the extension.
!       public static String GetFileNameWithoutExtension(String path)
!                       {
!                               if(path == null)
!                               {
!                                       return null;
!                               }
!                               int posn = path.Length;
!                               int dot = -1;
!                               char ch;
!                               while(posn > 0)
!                               {
!                                       ch = path[--posn];
!                                       if(ch == '.')
!                                       {
!                                               // If there are multiple 
extensions, then only
!                                               // strip the last one in the 
filename.
!                                               if(dot == -1)
!                                               {
!                                                       dot = posn;
!                                               }
!                                       }
!                                       else 
if(IsDirectoryOrVolumeSeparator(ch))
!                                       {
!                                               if(dot != -1)
!                                               {
!                                                       return 
path.Substring(posn + 1, dot - (posn + 1));
!                                               }
!                                               else
!                                               {
!                                                       return 
path.Substring(posn + 1);
!                                               }
!                                       }
!                               }
!                               if(dot != -1)
!                               {
!                                       return path.Substring(0, dot);
!                               }
!                               else
!                               {
!                                       return path;
!                               }
!                       }
! 
!       // Extract the extension from a path.
!       public static String GetExtension(String path)
!                       {
!                               if(path == null)
!                               {
!                                       return null;
!                               }
!                               int posn = path.Length;
!                               char ch;
!                               while(posn > 0)
!                               {
!                                       ch = path[--posn];
!                                       if(ch == '.')
!                                       {
!                                               return path.Substring(posn + 1);
!                                       }
!                                       else 
if(IsDirectoryOrVolumeSeparator(ch))
!                                       {
!                                               break;
!                                       }
!                               }
!                               return String.Empty;
!                       }
! 
!       // Combine a parent path and a child path.
!       private static String Combine(String path1, int rootLen, String path2)
!                       {
!                               if(path2.Length == 0)
!                               {
!                                       return path1;
!                               }
!                               StringBuilder builder = new 
StringBuilder(path1);
!                               int posn = 0;
!                               int len;
!                               char ch;
!                               if(path1.Length == 0 || 
!IsSeparator(path1[path1.Length - 1]))
!                               {
!                                       builder.Append(DirectorySeparatorChar);
!                               }
!                               while(posn < path2.Length)
!                               {
!                                       ch = path2[posn];
!                                       if(IsDirectoryOrVolumeSeparator(ch))
!                                       {
!                                               
if(!IsSeparator(builder[builder.Length - 1]))
!                                               {
!                                                       
builder.Append(DirectorySeparatorChar);
!                                               }
!                                               ++posn;
!                                       }
!                                       else if(ch == '.')
!                                       {
!                                               ++posn;
!                                               if(posn >= path2.Length || 
IsSeparator(path2[posn]))
!                                               {
!                                                       // Remove "." 
components from the pathname.
!                                                       ++posn;
!                                               }
!                                               else if(path2[posn] == '.' &&
!                                                       ((posn + 1) >= 
path2.Length ||
!                                                                
IsSeparator(path2[posn + 1])))
!                                               {
!                                                       // Strip the builder 
down to the parent directory.
!                                                       ++posn;
!                                                       len = builder.Length;
!                                                       if(len > rootLen && 
IsSeparator(builder[len - 1]))
!                                                       {
!                                                               --len;
!                                                       }
!                                                       while(len > rootLen &&
!                                                             
!IsSeparator(builder[len - 1]))
!                                                       {
!                                                               --len;
!                                                       }
!                                                       if(len > rootLen && 
IsSeparator(builder[len - 1]))
!                                                       {
!                                                               --len;
!                                                       }
!                                                       builder.Length = len;
!                                               }
!                                               else
!                                               {
!                                                       // Filename component 
beginning with a dot.
!                                                       builder.Append(ch);
!                                               }
!                                       }
!                                       else
!                                       {
!                                               builder.Append(ch);
!                                               ++posn;
!                                       }
!                               }
!                               return builder.ToString();
!                       }
! 
!       // Get a full absolute pathname.
!       public static String GetFullPath(String path)
!                       {
!                               // Check the parameter for null.
!                               if(path == null)
!                               {
!                                       throw new ArgumentNullException("path");
!                               }
!                               path = path.Trim();
! 
!                               // Validate zero-length and network share paths.
!                               int rootLen = GetRootPrefixLength(path);
! 
!                               // Split into parent path and suffix.
!                               String parentPath;
!                               if(rootLen != -1)
!                               {
!                                       if(IsSeparator(path[0]))
!                                       {
!                                               // Unix root or network share 
specification.
!                                               parentPath = path.Substring(0, 
rootLen);
!                                               path = path.Substring(rootLen);
!                                               if(path.Length > 0 && 
IsSeparator(path[0]))
!                                               {
!                                                       path = 
path.Substring(1);
!                                               }
!                                       }
!                                       else if(rootLen == 2)
!                                       {
!                                               // Drive letter with relative 
path specification.
!                                               parentPath = 
Directory.GetCurrentDirectory(path[0]);
!                                               path = path.Substring(2);
!                                       }
!                                       else
!                                       {
!                                               // Drive letter with absolute 
path specification.
!                                               parentPath = path.Substring(0, 
3);
!                                               path = path.Substring(3);
!                                       }
!                               }
!                               else
!                               {
!                                       parentPath = 
Directory.GetCurrentDirectory();
!                               }
! 
!                               // Normalize the parent path.
!                               parentPath = NormalizeSeparators(parentPath);
! 
!                               // Determine the length of the parent path's 
root portion.
!                               rootLen = GetRootPrefixLength(parentPath);
! 
!                               // Combine the parent and actual path.
!                               return Combine(parentPath, rootLen, path);
!                       }
! 
!       // Get the root directory for a path.
!       public static String GetPathRoot(String path)
!                       {
!                               // Convert the path into its full form.
!                               path = GetFullPath(path);
! 
!                               // Extract the root information.
!                               int rootLen = GetRootPrefixLength(path);
!                               if(rootLen != -1)
!                               {
!                                       return path.Substring(0, rootLen);
!                               }
!                               else
!                               {
!                                       return NormalizeSeparators("/");
!                               }
!                       }
! 
!       // Number to start searching from for temporary files.
!       private static int start;
! 
!       // Get the name of a unique temporary file.
!       public static String GetTempFileName()
!                       {
!                               String dir = GetTempPath();
!                               String full;
!                               lock(typeof(Path))
!                               {
!                                       if(start == 0)
!                                       {
!                                               start = 
((int)(DateTime.UtcNow.Ticks)) & 0x0000FFFF;
!                                       }
!                                       do
!                                       {
!                                               full = dir + 
DirectorySeparatorChar + "cli" +
!                                                          start.ToString("X4");
!                                               ++start;
!                                       }
!                                       while(File.Exists(full));
!                               }
!                               return full;
!                       }
! 
!       // Get the path to use to store temporary files.
!       public static String GetTempPath()
!                       {
!                               String env;
!                               env = 
Environment.GetEnvironmentVariable("TMPDIR");
!                               if(env != null && env.Length > 0)
!                               {
!                                       return env;
!                               }
!                               env = 
Environment.GetEnvironmentVariable("TEMP");
!                               if(env != null && env.Length > 0)
!                               {
!                                       return env;
!                               }
!                               return NormalizeSeparators("/tmp");
!                       }
! 
!       // Determine if a path has an extension.
!       public static bool HasExtension(String path)
!                       {
!                               if(path == null)
!                               {
!                                       return false;
!                               }
!                               int posn = path.Length;
!                               char ch;
!                               while(posn > 0)
!                               {
!                                       ch = path[--posn];
!                                       if(ch == '.')
!                                       {
!                                               return (posn < (path.Length - 
1));
!                                       }
!                                       else 
if(IsDirectoryOrVolumeSeparator(ch))
!                                       {
!                                               return false;
!                                       }
!                               }
!                               return false;
!                       }
! 
!       // Determine if a path is absolute.
!       public static bool IsPathRooted(String path)
!                       {
!                               if(path == null || path.Length == 0)
!                               {
!                                       return false;
!                               }
!                               else if(IsSeparator(path[0]))
!                               {
!                                       // Unix style ("/bar") or network share 
("\\foo").
!                                       return true;
!                               }
!                               else if(path.Length >= 2 && 
IsVolumeSeparator(path[1]))
!                               {
!                                       // Windows style ("c:\bar").
!                                       return true;
!                               }
!                               else
!                               {
!                                       return false;
!                               }
!                       }
  
! }; // class Path
  
! }; // namespace System.IO





reply via email to

[Prev in Thread] Current Thread [Next in Thread]