-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath.cs
More file actions
157 lines (123 loc) · 4.43 KB
/
Path.cs
File metadata and controls
157 lines (123 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
namespace cli_bot
{
public class Path
{
private readonly string _path;
public static char GoodSep { get { return System.OperatingSystem.IsWindows() ? '\\' : '/'; } }
public static char BadSep { get { return System.OperatingSystem.IsWindows() ? '/' : '\\'; } }
public Path(string path)
{
if (GoodSep != '/')
_path = path.Replace(BadSep, GoodSep).TrimStart('\\', '/').TrimEnd('\\', '/');
else
_path = path.Replace(BadSep, GoodSep).TrimEnd('\\', '/');
}
public Path()
{
_path = string.Empty;
}
public static implicit operator string(Path p) => p._path;
public static implicit operator Path(string s) => new Path(s);
public static Path operator /(Path left, Path right)
{
return new Path((left._path.TrimEnd('\\', '/') + GoodSep + right._path.TrimStart('\\', '/')).Replace(BadSep, GoodSep));
}
public override string ToString()
{
return _path;
}
public Path? ParentPath
{
get
{
string? parent = GetDirectoryName(_path);
return parent != null ? new Path(parent) : null;
}
}
public Path? DirectoryPath
{
get
{
if (Extension == string.Empty)
return new(_path);
string? parent = GetDirectoryName(_path);
return parent != null ? new Path(parent) : null;
}
}
public string Extension
{
get
{
return System.IO.Path.GetExtension(_path);
}
}
public static Path Assembly
{
get
{
return AppDomain.CurrentDomain.BaseDirectory;
}
}
public bool Contains(string path)
{
return _path.Contains(path);
}
public Path RelativeTo(Path basePath)
{
Uri baseUri = new Uri(basePath._path.EndsWith(GoodSep)
? basePath._path
: basePath._path + GoodSep);
Uri targetUri = new Uri(_path);
Uri relativeUri = baseUri.MakeRelativeUri(targetUri);
string relativePath = Uri.UnescapeDataString(relativeUri.ToString());
return new Path(relativePath.Replace(BadSep, GoodSep));
}
public string FileName => GetFileName(_path);
public static string? GetDirectoryName(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException("Path cannot be null or empty.", nameof(path));
}
path = path.Trim();
// Handle special cases for root or single-character paths.
if (path.Length == 1 && (path == "\\" || path == "/"))
{
return null;
}
// Normalize slashes for consistency.
path = path.Replace(BadSep, GoodSep);
int lastSlashIndex = path.LastIndexOf(GoodSep);
// If there are no slashes, the path has no directory component.
if (lastSlashIndex == -1)
{
return null;
}
// If the last slash is at the beginning (e.g., "C:\"), handle it specially.
if (lastSlashIndex == 0 || (lastSlashIndex == 2 && path[1] == ':'))
{
return path.Substring(0, lastSlashIndex + 1);
}
// Return the substring up to (but not including) the last slash.
return path.Substring(0, lastSlashIndex);
}
public static string GetFileName(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException("Path cannot be null or empty.", nameof(path));
}
path = path.Trim();
// Normalize slashes for consistency.
path = path.Replace(BadSep, GoodSep);
int lastSlashIndex = path.LastIndexOf(GoodSep);
// If there are no slashes, the entire path is the file name.
if (lastSlashIndex == -1)
{
return path;
}
// Return the substring after the last slash.
return path.Substring(lastSlashIndex + 1);
}
}
}