|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Globalization; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Text; |
| 9 | + |
| 10 | +namespace Abstract.FileSystem |
| 11 | +{ |
| 12 | + public class SystemPath |
| 13 | + { |
| 14 | + internal const char WinSeparator = '\\'; |
| 15 | + internal const char UncSeparator = '\\'; |
| 16 | + internal const char UnixSeparator = '/'; |
| 17 | + |
| 18 | + public static SystemPath Create(string path) |
| 19 | + { |
| 20 | + return new SystemPath(path); |
| 21 | + } |
| 22 | + |
| 23 | + static SystemPath() |
| 24 | + { |
| 25 | + OsType = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? OsType.Windows : OsType.Unix; |
| 26 | + } |
| 27 | + |
| 28 | + private readonly string _path; |
| 29 | + |
| 30 | + private SystemPath(string path) |
| 31 | + { |
| 32 | + _path = NormalizePath(path); |
| 33 | + } |
| 34 | + |
| 35 | + public static implicit operator SystemPath(string path) |
| 36 | + { |
| 37 | + if (path is null) |
| 38 | + { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + return new SystemPath(path); |
| 43 | + } |
| 44 | + |
| 45 | + public static implicit operator string(SystemPath path) |
| 46 | + { |
| 47 | + return path?.ToString(); |
| 48 | + } |
| 49 | + |
| 50 | + public static OsType OsType { get; } |
| 51 | + |
| 52 | + public static SystemPath operator /(SystemPath left, string right) |
| 53 | + { |
| 54 | + return new SystemPath(Combine(left, right)); |
| 55 | + } |
| 56 | + |
| 57 | + public static SystemPath operator +(SystemPath left, string right) |
| 58 | + { |
| 59 | + return new SystemPath(left.ToString() + right); |
| 60 | + } |
| 61 | + |
| 62 | + public static bool operator ==(SystemPath a, SystemPath b) |
| 63 | + { |
| 64 | + return EqualityComparer<SystemPath>.Default.Equals(a, b); |
| 65 | + } |
| 66 | + |
| 67 | + public static bool operator !=(SystemPath a, SystemPath b) |
| 68 | + { |
| 69 | + return !EqualityComparer<SystemPath>.Default.Equals(a, b); |
| 70 | + } |
| 71 | + |
| 72 | + protected bool Equals(SystemPath other) |
| 73 | + { |
| 74 | + var stringComparison = OsType == OsType.Unix ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; |
| 75 | + return string.Equals(_path, other._path, stringComparison); |
| 76 | + } |
| 77 | + |
| 78 | + public override bool Equals(object obj) |
| 79 | + { |
| 80 | + if (ReferenceEquals(objA: null, obj)) |
| 81 | + return false; |
| 82 | + if (ReferenceEquals(this, obj)) |
| 83 | + return true; |
| 84 | + if (obj.GetType() != GetType()) |
| 85 | + return false; |
| 86 | + return Equals((SystemPath)obj); |
| 87 | + } |
| 88 | + |
| 89 | + public override int GetHashCode() |
| 90 | + { |
| 91 | + return _path?.GetHashCode() ?? 0; |
| 92 | + } |
| 93 | + |
| 94 | + public override string ToString() |
| 95 | + { |
| 96 | + return ((IFormattable)this).ToString(format: null, formatProvider: null); |
| 97 | + } |
| 98 | + |
| 99 | + public static string NormalizePath(string path) |
| 100 | + { |
| 101 | + if (OsType == OsType.Unix) |
| 102 | + { |
| 103 | + return path.Replace(WinSeparator, UnixSeparator); |
| 104 | + } |
| 105 | + |
| 106 | + return path.Replace(UnixSeparator, WinSeparator); |
| 107 | + } |
| 108 | + |
| 109 | + public static string Combine(SystemPath path, params string[] elements) |
| 110 | + { |
| 111 | + throw new NotImplementedException(); |
| 112 | + } |
| 113 | + |
| 114 | + internal static bool IsWinRoot(string root) |
| 115 | + => root?.Length == 2 && |
| 116 | + char.IsLetter(root[index: 0]) && |
| 117 | + root[index: 1] == ':'; |
| 118 | + |
| 119 | + internal static bool IsUnixRoot(string root) |
| 120 | + => root?.Length == 1 && |
| 121 | + root[index: 0] == UnixSeparator; |
| 122 | + |
| 123 | + internal static bool IsUncRoot(string root) |
| 124 | + => root?.Length >= 3 && |
| 125 | + root[index: 0] == UncSeparator && |
| 126 | + root[index: 1] == UncSeparator && |
| 127 | + root.Skip(count: 2).All(char.IsLetterOrDigit); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments