84 lines
1.9 KiB
C#
84 lines
1.9 KiB
C#
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;
|
|
} |