From 3ee6345fd0573e67173ad856e37c77aca2b39686 Mon Sep 17 00:00:00 2001 From: mullerj Date: Tue, 27 Aug 2024 22:18:45 -0400 Subject: [PATCH] NormFile Refactor * Updated NormFileGetName to use sbyte* to avoid casting * Updated design --- src/dotnet/design/Mil/Navy/Nrl/Norm/NormApi.puml | 2 +- src/dotnet/src/Mil.Navy.Nrl.Norm/NormApi.cs | 2 +- src/dotnet/src/Mil.Navy.Nrl.Norm/NormFile.cs | 15 +++++---------- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/dotnet/design/Mil/Navy/Nrl/Norm/NormApi.puml b/src/dotnet/design/Mil/Navy/Nrl/Norm/NormApi.puml index bddbac5..508dbe8 100644 --- a/src/dotnet/design/Mil/Navy/Nrl/Norm/NormApi.puml +++ b/src/dotnet/design/Mil/Navy/Nrl/Norm/NormApi.puml @@ -97,7 +97,7 @@ class NormApi <> { + {static} <> NormObjectCancel(objectHandle:long) : void + {static} <> NormObjectRetain(objectHandle:long) : void + {static} <> NormObjectRelease(objectHandle:long) : void - + {static} <> NormFileGetName(fileHandle:long, nameBuffer:nint, bufferLen:int) : bool + + <> {static} <> NormFileGetName(fileHandle:long, nameBuffer:sbyte*, bufferLen:int) : bool + {static} <> NormFileRename(fileHandle:long, fileName:string) : bool + {static} <> NormDataAccessData(objectHandle:long) : nint + {static} <> NormObjectGetSender(objectHandle:long) : long diff --git a/src/dotnet/src/Mil.Navy.Nrl.Norm/NormApi.cs b/src/dotnet/src/Mil.Navy.Nrl.Norm/NormApi.cs index d150d99..341ccda 100644 --- a/src/dotnet/src/Mil.Navy.Nrl.Norm/NormApi.cs +++ b/src/dotnet/src/Mil.Navy.Nrl.Norm/NormApi.cs @@ -1020,7 +1020,7 @@ public struct NormEvent /// does not refer to an object of type NORM_OBJECT_FILE. /// [DllImport(NORM_LIBRARY)] - public static extern bool NormFileGetName(long fileHandle, nint nameBuffer, int bufferLen); + public unsafe static extern bool NormFileGetName(long fileHandle, sbyte* nameBuffer, int bufferLen); /// /// This function renames the file used to store content for the NORM_OBJECT_FILE transport object specified by diff --git a/src/dotnet/src/Mil.Navy.Nrl.Norm/NormFile.cs b/src/dotnet/src/Mil.Navy.Nrl.Norm/NormFile.cs index 954e084..bb95a47 100644 --- a/src/dotnet/src/Mil.Navy.Nrl.Norm/NormFile.cs +++ b/src/dotnet/src/Mil.Navy.Nrl.Norm/NormFile.cs @@ -1,7 +1,4 @@ -using System.Runtime.InteropServices; -using System.Text; - -namespace Mil.Navy.Nrl.Norm +namespace Mil.Navy.Nrl.Norm { /// /// A transport object of type NORM_OBJECT_FILE. @@ -29,16 +26,14 @@ public unsafe string Name { get { - var buffer = stackalloc byte[FILENAME_MAX]; - var bufferPtr = (nint)buffer; + var buffer = stackalloc sbyte[FILENAME_MAX]; - if (!NormFileGetName(_handle, bufferPtr, FILENAME_MAX)) + if (!NormFileGetName(_handle, buffer, FILENAME_MAX)) { throw new IOException("Failed to get file name"); } - var name = Marshal.PtrToStringAnsi(bufferPtr); - - return name ?? string.Empty; + + return new string(buffer); } }