--- /dev/null
+//------------------------------------------------------------------------------\r
+// \r
+// System.IO.Path.cs \r
+//\r
+// Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved\r
+// \r
+// Author: Jim Richardson, develop@wtfo-guru.com\r
+// Created: Saturday, August 11, 2001 \r
+//\r
+//------------------------------------------------------------------------------\r
+\r
+using System;\r
+\r
+namespace System.IO\r
+{\r
+ public class Path : Object\r
+ {\r
+ public static readonly char AltDirectorySeparatorChar = '\\'; // TODO: verify this\r
+ public static readonly char DirectorySeparatorChar = '/';\r
+ public static readonly char[] InvalidPathChars = { '\0' }; // TODO: research invalid chars\r
+ public static readonly char PathSeparator = ';'; // might be a space for unix/linux\r
+ public static readonly char VolumeSeparatorChar = '/';\r
+\r
+ private static readonly char[] PathSeparatorChars = { DirectorySeparatorChar, \r
+ AltDirectorySeparatorChar,\r
+ VolumeSeparatorChar };\r
+\r
+ // class methods\r
+ public static string ChangeExtension(string path, string extension)\r
+ {\r
+ if(path == null)\r
+ {\r
+ return null;\r
+ }\r
+\r
+ int iExt = findExtension(path);\r
+ \r
+ if(iExt < 0)\r
+ {\r
+ return extension == null ? path : path + extension;\r
+ }\r
+ else if(iExt > 0)\r
+ {\r
+ string temp = path.Substring(0, iExt);\r
+ if(extension != null)\r
+ {\r
+ return temp + extension;\r
+ }\r
+ return temp;\r
+ }\r
+\r
+ return extension;\r
+ }\r
+\r
+ public static string Combine(string path1, string path2)\r
+ {\r
+ if(path1 == null || path2 == null)\r
+ {\r
+ return null;\r
+ }\r
+\r
+ throwEmptyIf(path2);\r
+\r
+ // TODO: Check for invalid DirectoryInfo characters\r
+ // although I don't think it is necesary for linux\r
+\r
+ // TODO: Verify functionality further after NUnit tests written\r
+ // since the documentation was rather sketchy\r
+\r
+ if(IsPathRooted(path2))\r
+ {\r
+ if(path1.Equals(string.Empty))\r
+ {\r
+ return path2;\r
+ }\r
+ throw new ArgumentException("Rooted path");\r
+ }\r
+ \r
+ string dirSep = new string(DirectorySeparatorChar, 1);\r
+ string altSep = new string(AltDirectorySeparatorChar, 1);\r
+ \r
+ bool b1 = path1.EndsWith(dirSep) || path1.EndsWith(dirSep);\r
+ bool b2 = path2.StartsWith(dirSep) || path2.StartsWith(altSep);\r
+ if(b1 && b2)\r
+ {\r
+ throw new ArgumentException("Invalid combination");\r
+ }\r
+ \r
+ if(!b1 && !b2)\r
+ {\r
+ return path1 + dirSep + path2;\r
+ }\r
+\r
+ return path1 + path2;\r
+ }\r
+\r
+ public static string GetDirectoryName(string path)\r
+ {\r
+ if(path == null)\r
+ {\r
+ return null;\r
+ }\r
+ throwEmptyIf(path);\r
+ throwWhiteSpaceOnlyIf(path);\r
+ throwInvalidPathCharsIf(path);\r
+\r
+ if(path.Length > 2)\r
+ {\r
+ int nLast = path.LastIndexOfAny(PathSeparatorChars, path.Length - 2);\r
+\r
+ if(nLast > 0)\r
+ {\r
+ return path.Substring(0, nLast);\r
+ }\r
+ }\r
+ return path;\r
+ }\r
+\r
+ public static string GetExtension(string path)\r
+ {\r
+ if(path == null)\r
+ {\r
+ return string.Empty;\r
+ }\r
+\r
+ throwEmptyIf(path);\r
+ throwWhiteSpaceOnlyIf(path);\r
+ \r
+ int iExt = findExtension(path);\r
+ int iLastSep = path.LastIndexOfAny( PathSeparatorChars );\r
+\r
+ if(iExt > -1)\r
+ { // okay it has an extension\r
+ return path.Substring(iExt);\r
+ }\r
+ return string.Empty;\r
+ }\r
+\r
+ public static string GetFileName(string path)\r
+ {\r
+ if(path == null)\r
+ {\r
+ return string.Empty;\r
+ }\r
+\r
+ throwEmptyIf(path);\r
+ throwWhiteSpaceOnlyIf(path);\r
+\r
+ int nLast = path.LastIndexOfAny(PathSeparatorChars);\r
+\r
+ if(nLast > 0)\r
+ {\r
+ return path.Substring(nLast + 1);\r
+ }\r
+\r
+ return nLast == 0 ? null : path;\r
+ }\r
+\r
+ public static string GetFileNameWithoutExtension(string path)\r
+ {\r
+ return ChangeExtension(GetFileName(path), null);\r
+ }\r
+\r
+ public static string GetFullPath(string path)\r
+ {\r
+ if(path != null)\r
+ {\r
+ //TODO: figure out what the equivilent linux api to\r
+ // windoze ::GetCurrentDirectory() is and PInvoke it\r
+ return path;\r
+ }\r
+ return null;\r
+ }\r
+\r
+ public static string GetPathRoot(string path)\r
+ {\r
+ if(path != null || \r
+ (path.StartsWith(new string(DirectorySeparatorChar, 1)) ||\r
+ path.StartsWith(new string(AltDirectorySeparatorChar, 1))))\r
+ {\r
+ return path.Substring(0, 1);\r
+ }\r
+ return null;\r
+ }\r
+\r
+ public static string GetTempFileName()\r
+ {\r
+ //TODO: Implement method\r
+ return string.Empty;\r
+ }\r
+\r
+ /// <summary>\r
+ /// Returns the path of the current systems temp directory\r
+ /// </summary>\r
+ public static string GetTempPath()\r
+ { // TODO: This might vary with distribution and there\r
+ // might be an api to provide it. Research is needed\r
+ return "/tmp";\r
+ }\r
+\r
+ public static bool HasExtension(string path)\r
+ { \r
+ throwNullIf(path);\r
+ throwEmptyIf(path);\r
+ throwWhiteSpaceOnlyIf(path);\r
+ \r
+ return findExtension(path) > -1;\r
+ }\r
+\r
+ public static bool IsPathRooted(string path)\r
+ {\r
+ return path.StartsWith(new string(VolumeSeparatorChar,1));\r
+ }\r
+\r
+ // private class methods\r
+\r
+ private static int findExtension(string path)\r
+ { // method should return the index of the path extension\r
+ // start or -1 if no valid extension\r
+ if(path != null)\r
+ { \r
+ int iLastDot = path.LastIndexOf(".");\r
+ int iLastSep = path.LastIndexOfAny( PathSeparatorChars );\r
+\r
+ if(iLastDot > iLastSep)\r
+ {\r
+ return iLastDot;\r
+ }\r
+ }\r
+ return -1;\r
+ }\r
+\r
+ private static void throwNullIf(string path)\r
+ {\r
+ if(path == null)\r
+ {\r
+ throw new ArgumentNullException();\r
+ }\r
+ }\r
+\r
+ private static void throwEmptyIf(string path)\r
+ {\r
+ if(path != null && path.Length == 0)\r
+ {\r
+ throw new ArgumentException("Empty string");\r
+ }\r
+ }\r
+\r
+ private static void throwWhiteSpaceOnlyIf(string path)\r
+ {\r
+ if(path != null)\r
+ {\r
+ string temp = path;\r
+ temp.Trim();\r
+ if(temp.Length == 0)\r
+ {\r
+ throw new ArgumentException("Whitespace only string");\r
+ }\r
+ }\r
+ }\r
+ \r
+ private static void throwInvalidPathCharsIf(string path)\r
+ {\r
+ if(path != null && path.IndexOfAny(InvalidPathChars) > -1)\r
+ {\r
+ throw new ArgumentException("Invalid path characters");\r
+ }\r
+ }\r
+ }\r
+}\r