-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringExtensions.cs
120 lines (106 loc) · 3.02 KB
/
StringExtensions.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Venera.Shell;
using Venera.stasi;
using static System.Net.Mime.MediaTypeNames;
namespace Venera
{
public static class StringExtensions
{
public static string Pad(this string str, int padding)
{
int freeSpace = padding - str.Length;
if (freeSpace < 0)
{
str = str.Substring(0, padding - 3);
str += "...";
}
else
{
for (int i = 0; i < freeSpace; i++)
{
str += " ";
}
}
return str;
}
public static string EnsureBackslash(this string str)
{
if (!str.EndsWith(@"\"))
{
str += @"\";
}
return str;
}
public static string AbsoluteOrRelativePath(this string str)
{
string path;
string drive = str.Split(":").First();
if (drive != str)
{
//if it is an absolute path
path = $"{drive}:{str.Split(":").Last()}";
}
else
{
//if it is a relative path
//convert it into the corresponding absolute path
path = $"{Kernel.GlobalEnvironment.GetFirst(DefaultEnvironments.CurrentWorkingDirectory).EnsureBackslash()}{str}";
}
return path;
}
public static bool IsDriveId(this string str)
{
// The string must be exactly 3 characters long to match the pattern "[0-9]:\"
if (str.Length != 3)
{
return false;
}
// Check if the first character is a digit (0-9)
if (str[0] < '0' || str[0] > '9')
{
return false;
}
// Check if the second character is a colon ':'
if (str[1] != ':')
{
return false;
}
return true;
}
//do only use this when you have paths... otherwise it would be **undefined behaviour**
public static bool isAccessible(this string str)
{
//PANIC
if(Login.curUser == null)
{
//PANIC
return true;
}
//yes. I know this is a bad way to do it. But I am lazy and this works.
if (Login.curUser.Username == "root")
{
return true;
}
if (str.Contains("0:\\Users\\"))
{
if (!str.Contains("0:\\Users\\" + Login.curUser.Username))
{
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
}
}