-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
189 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,63 @@ | ||
using System.IO.Abstractions; | ||
using System.Text; | ||
|
||
namespace Automata.IO; | ||
|
||
public interface IO | ||
{ | ||
public string Path { get; } | ||
} | ||
|
||
public static class IOShared | ||
{ | ||
static IOShared() | ||
{ | ||
FileSystem = null; | ||
} | ||
string Path { get; } | ||
|
||
private static IFileSystem _fileSystem = null!; | ||
public static IFileSystem FileSystem | ||
static string CorrectSlash(string path) | ||
{ | ||
get => _fileSystem; | ||
set | ||
var builder = new StringBuilder(path.Length); | ||
for (var i = 0; i < path.Length; i++) | ||
{ | ||
if (value == null) | ||
var c = path[i]; | ||
switch (c) | ||
{ | ||
value = new FileSystem(); | ||
case '/': | ||
Slash(ref i, path, builder); break; | ||
case '\\': | ||
Slash(ref i, path, builder); break; | ||
default: builder.Append(c); | ||
break; | ||
} | ||
_fileSystem = value; | ||
} | ||
void Slash(ref int index, string source, StringBuilder builder) | ||
{ | ||
builder.Append('/'); | ||
index++; | ||
for (;index < source.Length; index++) | ||
{ | ||
var c = source[index]; | ||
if (c == '/' || c == '\\') | ||
continue; | ||
index--; | ||
return; | ||
} | ||
} | ||
return builder.ToString(); | ||
} | ||
} | ||
|
||
// ReSharper disable once InconsistentNaming | ||
public interface RelativeIO : IO | ||
{ | ||
public static string SubtractLeft(string leftPath, string fullPath) | ||
=> IO.CorrectSlash(fullPath).Substring(IO.CorrectSlash(leftPath).Length); | ||
|
||
public static IRelativeDirectory RelativeDirectory( | ||
string leftPath, string fullPath) | ||
{ | ||
return new RelativeDirectory(SubtractLeft(leftPath, fullPath)); | ||
} | ||
|
||
public static IRelativeFile RelativeFile(string leftPath, string fullPath) | ||
{ | ||
var relativePath = SubtractLeft(leftPath, fullPath); | ||
var directoryPath = IOShared.FileSystem.Path.GetDirectoryName(relativePath); | ||
var filename = IOShared.FileSystem.Path.GetFileName(relativePath); | ||
|
||
return new RelativeFile(new RelativeDirectory(directoryPath), filename); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.IO.Abstractions; | ||
|
||
namespace Automata.IO; | ||
// ReSharper disable once InconsistentNaming | ||
public static class IOShared | ||
{ | ||
static IOShared() | ||
{ | ||
FileSystem = null; | ||
} | ||
|
||
private static IFileSystem _fileSystem = null!; | ||
public static IFileSystem FileSystem | ||
{ | ||
get => _fileSystem; | ||
set | ||
{ | ||
if (value == null) | ||
{ | ||
value = new FileSystem(); | ||
} | ||
_fileSystem = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace Automata.IO; | ||
|
||
public interface IRelativeDirectory : IO | ||
{ | ||
|
||
} | ||
|
||
public class RelativeDirectory : IRelativeDirectory | ||
{ | ||
public string Path { get; } | ||
|
||
public RelativeDirectory(string path) | ||
{ | ||
Path = IO.CorrectSlash(path); | ||
} | ||
|
||
public RelativeDirectory(IRelativeDirectory root, string name) | ||
{ | ||
Path = IO.CorrectSlash(root.Path + "/" + name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Automata.IO; | ||
|
||
public interface IRelativeFile : IO | ||
{ | ||
IRelativeDirectory RelativeRoot { get; } | ||
string Name { get; } | ||
} | ||
|
||
public class RelativeFile : IRelativeFile | ||
{ | ||
public string Path { get; } | ||
|
||
public IRelativeDirectory RelativeRoot { get; } | ||
public string Name { get; } | ||
|
||
public RelativeFile(IRelativeDirectory root, string name) | ||
{ | ||
RelativeRoot = root; | ||
Name = name; | ||
Path = IO.CorrectSlash(RelativeRoot.Path + name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Automata.IO; | ||
|
||
public static class RelativeDirectoryExtensions | ||
{ | ||
public static IRelativeDirectory Directory(this IRelativeDirectory root, string name) | ||
=> new RelativeDirectory(root, name); | ||
|
||
public static IRelativeFile File(this IRelativeDirectory dir, string file) | ||
=> new RelativeFile(dir, file); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Automata.IO; | ||
|
||
public static class RelativeFileExtensions | ||
{ | ||
public static IRelativeFile RelativeFile(this IFile file, IDirectory excludeDirectoryPath) | ||
{ | ||
return RelativeIO.RelativeFile(excludeDirectoryPath.Path, file.Path); | ||
} | ||
} |