Updated NormFile.Name to use stackalloc (#37)

pull/85/head
mullerj 2024-08-25 13:25:13 -04:00 committed by GitHub
parent bc8ab6b8d0
commit f04faca08a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 16 deletions

View File

@ -25,28 +25,19 @@ namespace Mil.Navy.Nrl.Norm
/// The name of the file.
/// </summary>
/// <exception cref="IOException">Thrown when failed to get file name.</exception>
public string Name
public unsafe string Name
{
get
{
var buffer = new byte[FILENAME_MAX];
var bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
string? name;
var buffer = stackalloc byte[FILENAME_MAX];
var bufferPtr = (nint)buffer;
try
if (!NormFileGetName(_handle, bufferPtr, FILENAME_MAX))
{
var bufferPtr = bufferHandle.AddrOfPinnedObject();
if (!NormFileGetName(_handle, bufferPtr, FILENAME_MAX))
{
throw new IOException("Failed to get file name");
}
name = Marshal.PtrToStringAnsi(bufferPtr);
}
finally
{
bufferHandle.Free();
throw new IOException("Failed to get file name");
}
var name = Marshal.PtrToStringAnsi(bufferPtr);
return name ?? string.Empty;
}
}