BadScript 2
Loading...
Searching...
No Matches
BadVirtualPathReader.cs
Go to the documentation of this file.
2
4
8public static class BadVirtualPathReader
9{
10 public static string JoinPath(IEnumerable<string> parts)
11 {
12 return '/' + string.Join("/", parts);
13 }
14
15 public static bool IsDirectory(string path)
16 {
17 return path.EndsWith('/') || path.EndsWith('\\');
18 }
19
20 public static string[] SplitPath(string path)
21 {
22 string[] parts = path.Split('/', '\\');
23
24 if (IsAbsolutePath(path))
25 {
26 parts = parts.Skip(1).ToArray();
27 }
28
29 return parts;
30 }
31
32 public static bool IsAbsolutePath(string path)
33 {
34 return path.StartsWith('/') || path.StartsWith('\\');
35 }
36
37 public static bool IsRootPath(string path)
38 {
39 return path is "/" or "\\";
40 }
41
42 public static string ResolvePath(string path, string currentDir)
43 {
44 string[] parts = SplitPath(path);
45
46 if (parts.Length == 0)
47 {
48 return currentDir;
49 }
50
51 List<string> result;
52
53 if (IsAbsolutePath(path) || currentDir == "/" || currentDir == "\\")
54 {
55 result = new List<string>();
56 }
57 else
58 {
59 result = new List<string>(currentDir.Split('/', '\\').Skip(1));
60 }
61
62 foreach (string t in parts)
63 {
64 switch (t)
65 {
66 case ".":
67 continue;
68 case ".." when result.Count == 0:
69 throw new Exception("Can't go back from root");
70 case "..":
71 result.RemoveAt(result.Count - 1);
72
73 break;
74 default:
75 result.Add(t);
76
77 break;
78 }
79 }
80
81 if (result.Count != 0 && string.IsNullOrEmpty(result[result.Count - 1]))
82 {
83 result.RemoveAt(result.Count - 1);
84 }
85
86 return "/" + string.Join("/", result);
87 }
88}
Implements Operations to read and manipulate File System Paths.
static string JoinPath(IEnumerable< string > parts)
static string ResolvePath(string path, string currentDir)
Contains the Implementation of the BadScript Virtual File System.
Contains Utility Functions and Classes.
Definition BadEnum.cs:5