Import from internal git
This commit is contained in:
84
Core/Location.cs
Normal file
84
Core/Location.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Core;
|
||||
|
||||
public class Location
|
||||
{
|
||||
public string Path { get; private set; }
|
||||
|
||||
public Location(string path)
|
||||
{
|
||||
Path = path;
|
||||
}
|
||||
|
||||
public Location(string[] pathItems)
|
||||
{
|
||||
Path = new StringBuilder().AppendJoin("/", pathItems).ToString();
|
||||
Clean();
|
||||
}
|
||||
|
||||
public Location(Location[] pathItems)
|
||||
{
|
||||
Path = new StringBuilder().AppendJoin("/", pathItems.Select(pi => pi.ToString())).ToString();
|
||||
Clean();
|
||||
}
|
||||
|
||||
public string LastElement()
|
||||
{
|
||||
return Path.Split(['/', '\\'])[^1];
|
||||
}
|
||||
|
||||
public bool HasExtension(string extension)
|
||||
{
|
||||
return LastElement().Split('.')[^1].Contains(extension);
|
||||
}
|
||||
|
||||
public bool IsInSameFolder(Location loc)
|
||||
{
|
||||
loc.Clean();
|
||||
var folder = GetFolder();
|
||||
var locFolder = loc.GetFolder();
|
||||
return folder.Equals(locFolder);
|
||||
}
|
||||
|
||||
public string GetFolder() => Path.Split(['/', '\\'])[^2];
|
||||
|
||||
public string GetFileName() => LastElement().Split('.')[0];
|
||||
|
||||
public Location ConcatenateWith(string[] relatives)
|
||||
{
|
||||
var data = new StringBuilder().AppendJoin("/", relatives).ToString();
|
||||
var path = new Location(Add(data));
|
||||
path.Clean();
|
||||
return path;
|
||||
}
|
||||
|
||||
public Location ConcatenateWith(string relative)
|
||||
{
|
||||
var path = new Location(Add(relative));
|
||||
path.Clean();
|
||||
return path;
|
||||
}
|
||||
|
||||
public Location ConcatenateWith(Location relative)
|
||||
{
|
||||
return ConcatenateWith(relative.ToString());
|
||||
}
|
||||
|
||||
private string Add(string value)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
var newPath = builder.AppendJoin("/", [Path, value]);
|
||||
return newPath.ToString();
|
||||
}
|
||||
|
||||
private void Clean()
|
||||
{
|
||||
Path = Path
|
||||
.Replace(@"\", "/")
|
||||
.Replace("/./", "/")
|
||||
.Replace("//", "/");
|
||||
}
|
||||
|
||||
public override string ToString() => Path;
|
||||
}
|
||||
Reference in New Issue
Block a user