From 405bbeff9e6fefb7537a17df09c81de72a925c1e Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Thu, 2 Apr 2020 21:54:26 -0700 Subject: [PATCH] Add some FspPathPrintf functionality and change a constant name --- src/LibHac/Common/U8StringBuilder.cs | 13 +++++---- src/LibHac/Fs/FileStorage2.cs | 8 +++--- src/LibHac/Fs/FileStorageBasedFileSystem.cs | 2 +- src/LibHac/Fs/PathUtility.cs | 31 ++++++++++++++++++++- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/LibHac/Common/U8StringBuilder.cs b/src/LibHac/Common/U8StringBuilder.cs index ba78f569..e4ae004d 100644 --- a/src/LibHac/Common/U8StringBuilder.cs +++ b/src/LibHac/Common/U8StringBuilder.cs @@ -12,8 +12,9 @@ namespace LibHac.Common private int _length; public bool Overflowed { get; private set; } - public int Length => _length; - public int Capacity => _buffer.Length - NullTerminatorLength; + public readonly int Length => _length; + public readonly int Capacity => _buffer.Length - NullTerminatorLength; + public readonly Span Buffer => _buffer; public U8StringBuilder(Span buffer) { @@ -62,12 +63,12 @@ namespace LibHac.Common return this; } - private bool HasCapacity(int requiredCapacity) + private readonly bool HasCapacity(int requiredCapacity) { return requiredCapacity <= Capacity; } - private bool HasAdditionalCapacity(int requiredAdditionalCapacity) + private readonly bool HasAdditionalCapacity(int requiredAdditionalCapacity) { return HasCapacity(_length + requiredAdditionalCapacity); } @@ -77,11 +78,11 @@ namespace LibHac.Common _buffer[_length] = 0; } - private void ThrowIfBufferLengthIsZero() + private readonly void ThrowIfBufferLengthIsZero() { if (_buffer.Length == 0) throw new ArgumentException("Buffer length must be greater than 0."); } - public override string ToString() => StringUtils.Utf8ZToString(_buffer); + public override readonly string ToString() => StringUtils.Utf8ZToString(_buffer); } } diff --git a/src/LibHac/Fs/FileStorage2.cs b/src/LibHac/Fs/FileStorage2.cs index 1d242583..0a5514b5 100644 --- a/src/LibHac/Fs/FileStorage2.cs +++ b/src/LibHac/Fs/FileStorage2.cs @@ -6,7 +6,7 @@ namespace LibHac.Fs { public class FileStorage2 : StorageBase { - protected const long InvalidSize = -1; + protected const long SizeNotInitialized = -1; private IFile BaseFile { get; set; } protected long FileSize { get; set; } @@ -14,7 +14,7 @@ namespace LibHac.Fs public FileStorage2(IFile baseFile) { BaseFile = baseFile; - FileSize = InvalidSize; + FileSize = SizeNotInitialized; } protected FileStorage2() { } @@ -29,7 +29,7 @@ namespace LibHac.Fs private Result UpdateSize() { - if (FileSize != InvalidSize) + if (FileSize != SizeNotInitialized) return Result.Success; Result rc = BaseFile.GetSize(out long fileSize); @@ -87,7 +87,7 @@ namespace LibHac.Fs protected override Result SetSizeImpl(long size) { - FileSize = InvalidSize; + FileSize = SizeNotInitialized; return BaseFile.SetSize(size); } diff --git a/src/LibHac/Fs/FileStorageBasedFileSystem.cs b/src/LibHac/Fs/FileStorageBasedFileSystem.cs index ce43fa53..35150ad3 100644 --- a/src/LibHac/Fs/FileStorageBasedFileSystem.cs +++ b/src/LibHac/Fs/FileStorageBasedFileSystem.cs @@ -11,7 +11,7 @@ namespace LibHac.Fs private FileStorageBasedFileSystem() { - FileSize = InvalidSize; + FileSize = SizeNotInitialized; } public static Result CreateNew(out FileStorageBasedFileSystem created, IFileSystem baseFileSystem, U8Span path, diff --git a/src/LibHac/Fs/PathUtility.cs b/src/LibHac/Fs/PathUtility.cs index 949d343a..93af9103 100644 --- a/src/LibHac/Fs/PathUtility.cs +++ b/src/LibHac/Fs/PathUtility.cs @@ -1,4 +1,5 @@ -using System.Diagnostics; +using System; +using System.Diagnostics; using System.Runtime.CompilerServices; using LibHac.Common; using static LibHac.Fs.PathTool; @@ -77,5 +78,33 @@ namespace LibHac.Fs return ResultFs.TooLongPath.Log(); } + + public static void Replace(Span buffer, byte oldChar, byte newChar) + { + for (int i = 0; i < buffer.Length; i++) + { + if (buffer[i] == oldChar) + { + buffer[i] = newChar; + } + } + } + + /// + /// Performs the extra functions that nn::fs::FspPathPrintf does on the string buffer. + /// + /// The string builder to process. + /// The of the operation. + public static Result ToSfPath(in this U8StringBuilder builder) + { + if (builder.Overflowed) + return ResultFs.TooLongPath.Log(); + + Replace(builder.Buffer.Slice(builder.Capacity), + StringTraits.AltDirectorySeparator, + StringTraits.DirectorySeparator); + + return Result.Success; + } } }