mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
Add FileBase
This commit is contained in:
parent
b606724fbc
commit
23088aab02
4 changed files with 36 additions and 10 deletions
23
src/LibHac/IO/FileBase.cs
Normal file
23
src/LibHac/IO/FileBase.cs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace LibHac.IO
|
||||||
|
{
|
||||||
|
public abstract class FileBase : IFile
|
||||||
|
{
|
||||||
|
public abstract int Read(Span<byte> destination, long offset);
|
||||||
|
public abstract void Write(ReadOnlySpan<byte> source, long offset);
|
||||||
|
public abstract void Flush();
|
||||||
|
public abstract long GetSize();
|
||||||
|
public abstract long SetSize();
|
||||||
|
|
||||||
|
protected int GetAvailableSizeAndValidate(ReadOnlySpan<byte> span, long offset)
|
||||||
|
{
|
||||||
|
long fileLength = GetSize();
|
||||||
|
|
||||||
|
if (span == null) throw new ArgumentNullException(nameof(span));
|
||||||
|
if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset), "Argument must be non-negative.");
|
||||||
|
|
||||||
|
return (int)Math.Min(fileLength - offset, span.Length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ namespace LibHac.IO
|
||||||
{
|
{
|
||||||
public interface IFile
|
public interface IFile
|
||||||
{
|
{
|
||||||
void Read(Span<byte> destination, long offset);
|
int Read(Span<byte> destination, long offset);
|
||||||
void Write(ReadOnlySpan<byte> source, long offset);
|
void Write(ReadOnlySpan<byte> source, long offset);
|
||||||
void Flush();
|
void Flush();
|
||||||
long GetSize();
|
long GetSize();
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace LibHac.IO
|
namespace LibHac.IO
|
||||||
{
|
{
|
||||||
public class RomFsFile : IFile
|
public class RomFsFile : FileBase
|
||||||
{
|
{
|
||||||
private IStorage BaseStorage { get; }
|
private IStorage BaseStorage { get; }
|
||||||
private long Offset { get; }
|
private long Offset { get; }
|
||||||
|
@ -15,29 +15,32 @@ namespace LibHac.IO
|
||||||
Size = size;
|
Size = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Read(Span<byte> destination, long offset)
|
public override int Read(Span<byte> destination, long offset)
|
||||||
{
|
{
|
||||||
long storageOffset = Offset + offset;
|
long storageOffset = Offset + offset;
|
||||||
|
int toRead = GetAvailableSizeAndValidate(destination, offset);
|
||||||
|
|
||||||
BaseStorage.Read(destination, storageOffset);
|
BaseStorage.Read(destination.Slice(0, toRead), storageOffset);
|
||||||
|
|
||||||
|
return toRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Write(ReadOnlySpan<byte> source, long offset)
|
public override void Write(ReadOnlySpan<byte> source, long offset)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Flush()
|
public override void Flush()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public long GetSize()
|
public override long GetSize()
|
||||||
{
|
{
|
||||||
return Size;
|
return Size;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long SetSize()
|
public override long SetSize()
|
||||||
{
|
{
|
||||||
throw new NotSupportedException();
|
throw new NotSupportedException();
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,7 +95,7 @@ namespace LibHac.IO
|
||||||
|
|
||||||
if (Length != -1)
|
if (Length != -1)
|
||||||
{
|
{
|
||||||
if (offset + count > Length) throw new ArgumentException();
|
if (offset + count > Length) throw new ArgumentException("The given offset and count exceed the length of the Storage");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,7 +107,7 @@ namespace LibHac.IO
|
||||||
|
|
||||||
if (Length != -1)
|
if (Length != -1)
|
||||||
{
|
{
|
||||||
if (offset + destination.Length > Length) throw new ArgumentException("Storage");
|
if (offset + destination.Length > Length) throw new ArgumentException("The given offset and count exceed the length of the Storage");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue