CGC_Codes

FileSystemPath

Mar 13th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.99 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.Contracts;
  5. using System.Linq;
  6.  
  7. namespace SharpFileSystem
  8. {
  9.     public struct FileSystemPath: IEquatable<FileSystemPath>, IComparable<FileSystemPath>
  10.     {
  11.         public static readonly char DirectorySeparator = '/';
  12.         public static FileSystemPath Root { get; private set; }
  13.  
  14.         private readonly string _path;
  15.        
  16.         public string Path
  17.         {
  18.             get { return _path ?? "/"; }
  19.         }
  20.  
  21.         public bool IsDirectory
  22.         {
  23.             get { return Path[Path.Length - 1] == DirectorySeparator; }
  24.         }
  25.  
  26.         public bool IsFile
  27.         {
  28.             get { return !IsDirectory; }
  29.         }
  30.  
  31.         public bool IsRoot
  32.         {
  33.             get { return Path.Length == 1; }
  34.         }
  35.  
  36.         public string EntityName
  37.         {
  38.             get
  39.             {
  40.                 string name = Path;
  41.                 if (IsRoot)
  42.                     return null;
  43.                 int endOfName = name.Length;
  44.                 if (IsDirectory)
  45.                     endOfName--;
  46.                 int startOfName = name.LastIndexOf(DirectorySeparator, endOfName - 1, endOfName) + 1;
  47.                 return name.Substring(startOfName, endOfName - startOfName);
  48.             }
  49.         }
  50.  
  51.         public FileSystemPath ParentPath
  52.         {
  53.             get
  54.             {
  55.                 string parentPath = Path;
  56.                 if (IsRoot)
  57.                     throw new InvalidOperationException("There is no parent of root.");
  58.                 int lookaheadCount = parentPath.Length;
  59.                 if (IsDirectory)
  60.                     lookaheadCount--;
  61.                 int index = parentPath.LastIndexOf(DirectorySeparator, lookaheadCount - 1, lookaheadCount);
  62.                 Debug.Assert(index >= 0);
  63.                 parentPath = parentPath.Remove(index + 1);
  64.                 return new FileSystemPath(parentPath);
  65.             }
  66.         }
  67.  
  68.         static FileSystemPath()
  69.         {
  70.             Root = new FileSystemPath(DirectorySeparator.ToString());
  71.         }
  72.  
  73.         private FileSystemPath(string path)
  74.         {
  75.             _path = path;
  76.         }
  77.  
  78.         public static bool IsRooted(string s)
  79.         {
  80.             if (s.Length == 0)
  81.                 return false;
  82.             return s[0] == DirectorySeparator;
  83.         }
  84.  
  85.         public static FileSystemPath Parse(string s)
  86.         {
  87.             if (s == null)
  88.                 throw new ArgumentNullException("s");
  89.             if (!IsRooted(s))
  90.                 throw new ParseException(s, "Path is not rooted");
  91.             if (s.Contains(string.Concat(DirectorySeparator, DirectorySeparator)))
  92.                 throw new ParseException(s, "Path contains double directory-separators.");
  93.             return new FileSystemPath(s);
  94.         }
  95.  
  96.         public FileSystemPath AppendPath(string relativePath)
  97.         {
  98.             if (IsRooted(relativePath))
  99.                 throw new ArgumentException("The specified path should be relative.", "relativePath");
  100.             if (!IsDirectory)
  101.                 throw new InvalidOperationException("This FileSystemPath is not a directory.");
  102.             return new FileSystemPath(Path + relativePath);
  103.         }
  104.  
  105.         [Pure]
  106.         public FileSystemPath AppendPath(FileSystemPath path)
  107.         {
  108.             if (!IsDirectory)
  109.                 throw new InvalidOperationException("This FileSystemPath is not a directory.");
  110.             return new FileSystemPath(Path + path.Path.Substring(1));
  111.         }
  112.  
  113.         [Pure]
  114.         public FileSystemPath AppendDirectory(string directoryName)
  115.         {
  116.             if (directoryName.Contains(DirectorySeparator.ToString()))
  117.                 throw new ArgumentException("The specified name includes directory-separator(s).", "directoryName");
  118.             if (!IsDirectory)
  119.                 throw new InvalidOperationException("The specified FileSystemPath is not a directory.");
  120.             return new FileSystemPath(Path + directoryName + DirectorySeparator);
  121.         }
  122.  
  123.         [Pure]
  124.         public FileSystemPath AppendFile(string fileName)
  125.         {
  126.             if (fileName.Contains(DirectorySeparator.ToString()))
  127.                 throw new ArgumentException("The specified name includes directory-separator(s).", "fileName");
  128.             if (!IsDirectory)
  129.                 throw new InvalidOperationException("The specified FileSystemPath is not a directory.");
  130.             return new FileSystemPath(Path + fileName);
  131.         }
  132.  
  133.         [Pure]
  134.         public bool IsParentOf(FileSystemPath path)
  135.         {
  136.             return IsDirectory && Path.Length != path.Path.Length && path.Path.StartsWith(Path);
  137.         }
  138.  
  139.         [Pure]
  140.         public bool IsChildOf(FileSystemPath path)
  141.         {
  142.             return path.IsParentOf(this);
  143.         }
  144.  
  145.         [Pure]
  146.         public FileSystemPath RemoveParent(FileSystemPath parent)
  147.         {
  148.             if (!parent.IsDirectory)
  149.                 throw new ArgumentException("The specified path can not be the parent of this path: it is not a directory.");
  150.             if (!Path.StartsWith(parent.Path))
  151.                 throw new ArgumentException("The specified path is not a parent of this path.");
  152.             return new FileSystemPath(Path.Remove(0, parent.Path.Length - 1));
  153.         }
  154.  
  155.         [Pure]
  156.         public FileSystemPath RemoveChild(FileSystemPath child)
  157.         {
  158.             if (!Path.EndsWith(child.Path))
  159.                 throw new ArgumentException("The specified path is not a child of this path.");
  160.             return new FileSystemPath(Path.Substring(0, Path.Length - child.Path.Length + 1));
  161.         }
  162.  
  163.         [Pure]
  164.         public string GetExtension()
  165.         {
  166.             if (!IsFile)
  167.                 throw new ArgumentException("The specified FileSystemPath is not a file.");
  168.             string name = EntityName;
  169.             int extensionIndex = name.LastIndexOf('.');
  170.             if (extensionIndex < 0)
  171.                 return "";
  172.             return name.Substring(extensionIndex);
  173.         }
  174.  
  175.         [Pure]
  176.         public FileSystemPath ChangeExtension(string extension)
  177.         {
  178.             if (!IsFile)
  179.                 throw new ArgumentException("The specified FileSystemPath is not a file.");
  180.             string name = EntityName;
  181.             int extensionIndex = name.LastIndexOf('.');
  182.             if (extensionIndex >= 0)
  183.                 return ParentPath.AppendFile(name.Substring(0, extensionIndex) + extension);
  184.             return FileSystemPath.Parse(Path + extension);
  185.         }
  186.  
  187.         [Pure]
  188.         public string[] GetDirectorySegments()
  189.         {
  190.             FileSystemPath path = this;
  191.             if (IsFile)
  192.                 path = path.ParentPath;
  193.             var segments = new LinkedList<string>();
  194.             while(!path.IsRoot)
  195.             {
  196.                 segments.AddFirst(path.EntityName);
  197.                 path = path.ParentPath;
  198.             }
  199.             return segments.ToArray();
  200.         }
  201.  
  202.         [Pure]
  203.         public int CompareTo(FileSystemPath other)
  204.         {
  205.             return Path.CompareTo(other.Path);
  206.         }
  207.  
  208.         [Pure]
  209.         public override string ToString()
  210.         {
  211.             return Path;
  212.         }
  213.  
  214.         [Pure]
  215.         public override bool Equals(object obj)
  216.         {
  217.             if (obj is FileSystemPath)
  218.                 return Equals((FileSystemPath) obj);
  219.             return false;
  220.         }
  221.  
  222.         [Pure]
  223.         public bool Equals(FileSystemPath other)
  224.         {
  225.             return other.Path.Equals(Path);
  226.         }
  227.  
  228.         [Pure]
  229.         public override int GetHashCode()
  230.         {
  231.             return Path.GetHashCode();
  232.         }
  233.  
  234.         public static bool operator ==(FileSystemPath pathA, FileSystemPath pathB)
  235.         {
  236.             return pathA.Equals(pathB);
  237.         }
  238.  
  239.         public static bool operator !=(FileSystemPath pathA, FileSystemPath pathB)
  240.         {
  241.             return !(pathA == pathB);
  242.         }
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment