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)
27 .ToArray();
28 }
29
30 return parts;
31 }
32
33 public static bool IsAbsolutePath(string path)
34 {
35 return path.StartsWith('/') || path.StartsWith('\\');
36 }
37
38 public static bool IsRootPath(string path)
39 {
40 return path is "/" or "\\";
41 }
42
43 public static string ResolvePath(string path, string currentDir)
44 {
45 string[] parts = SplitPath(path);
46
47 if (parts.Length == 0)
48 {
49 return currentDir;
50 }
51
52 List<string> result;
53
54 if (IsAbsolutePath(path) || currentDir == "/" || currentDir == "\\")
55 {
56 result = new List<string>();
57 }
58 else
59 {
60 result = new List<string>(currentDir.Split('/', '\\')
61 .Skip(1)
62 );
63 }
64
65 foreach (string t in parts)
66 {
67 switch (t)
68 {
69 case ".":
70 continue;
71 case ".." when result.Count == 0:
72 throw new Exception("Can't go back from root");
73 case "..":
74 result.RemoveAt(result.Count - 1);
75
76 break;
77 default:
78 result.Add(t);
79
80 break;
81 }
82 }
83
84 if (result.Count != 0 && string.IsNullOrEmpty(result[result.Count - 1]))
85 {
86 result.RemoveAt(result.Count - 1);
87 }
88
89 return "/" + string.Join("/", result);
90 }
91}
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