From d4059b636f09d537c092dbd79757e89d534a4a8f Mon Sep 17 00:00:00 2001 From: Juna <46632782+JunaMeinhold@users.noreply.github.com> Date: Fri, 8 Nov 2024 15:15:09 +0100 Subject: [PATCH] Removed debugging code and improved path handling. --- .../Dialogs/FileUtilities.cs | 118 ++++++++++++++---- 1 file changed, 95 insertions(+), 23 deletions(-) diff --git a/Hexa.NET.ImGui.Widgets/Dialogs/FileUtilities.cs b/Hexa.NET.ImGui.Widgets/Dialogs/FileUtilities.cs index be8b3d7..df6f212 100644 --- a/Hexa.NET.ImGui.Widgets/Dialogs/FileUtilities.cs +++ b/Hexa.NET.ImGui.Widgets/Dialogs/FileUtilities.cs @@ -1,12 +1,9 @@ namespace Hexa.NET.ImGui.Widgets.Dialogs { using Hexa.NET.Utilities; - using Microsoft.CodeAnalysis; using System; using System.Collections.Generic; - using System.Diagnostics.CodeAnalysis; using System.IO; - using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; @@ -20,6 +17,10 @@ public static long GetFileSize(string filePath) { return GetFileMetadataWindows(filePath).Size; } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + return GetFileMetadataOSX(filePath).Size; + } else { return GetFileMetadataUnix(filePath).Size; @@ -32,6 +33,10 @@ public static FileMetadata GetFileMetadata(string filePath) { return GetFileMetadataWindows(filePath); } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + return GetFileMetadataOSX(filePath); + } else { return GetFileMetadataUnix(filePath); @@ -44,12 +49,49 @@ public static IEnumerable EnumerateEntries(string path, string pat { return EnumerateEntriesWin(path, pattern, option); } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + return EnumerateEntriesOSX(path, pattern, option); + } else { return EnumerateEntriesUnix(path, pattern, option); } } + public static readonly char DirectorySeparatorChar = Path.DirectorySeparatorChar; + public static readonly char AltDirectorySeparatorChar = Path.AltDirectorySeparatorChar; + + public static void CorrectPath(StdString str) + { + byte* ptr = str.Data; + byte* end = ptr + str.Size; + while (ptr != end) + { + byte c = *ptr; + if (c == '/' || c == '\\') + { + *ptr = (byte)DirectorySeparatorChar; + } + ptr++; + } + } + + public static void CorrectPath(StdWString str) + { + char* ptr = str.Data; + char* end = ptr + str.Size; + while (ptr != end) + { + char c = *ptr; + if (c == '/' || c == '\\') + { + *ptr = DirectorySeparatorChar; + } + ptr++; + } + } + #region WIN32 public static IEnumerable EnumerateEntriesWin(string path, string pattern, SearchOption option) @@ -60,6 +102,7 @@ public static IEnumerable EnumerateEntriesWin(string path, string StdWString str = path; str.Append('\\'); str.Append('*'); + CorrectPath(str); walkStack.Push(str); } @@ -129,19 +172,24 @@ private static nint StartSearch(StdWString st, out WIN32_FIND_DATA data) [MethodImpl(MethodImplOptions.AggressiveInlining)] private static FileMetadata Convert(WIN32_FIND_DATA data, StdWString path) { - FileMetadata metadata = new(); int length = StrLen(data.cFileName); - StdWString str = new(length + path.Size + 1); - for (int i = 0; i < path.Size - 1; i++) + StdWString str; + if (path[path.Size - 1] != '/' || path[path.Size - 1] != '\\') { - str.Append(path[i]); + str = new(length + 1 + path.Size); + str.Append(path); + str.Append('/'); + str.Append(data.cFileName, length); } - - for (int i = 0; i < length; i++) + else { - str.Append(data.cFileName[i]); + str = new(length + path.Size); + str.Append(path); + str.Append(data.cFileName, length); } + *(str.Data + str.Size) = '\0'; + FileMetadata metadata = new(); metadata.Path = str; metadata.CreationTime = DateTime.FromFileTime(data.ftCreationTime); metadata.LastAccessTime = DateTime.FromFileTime(data.ftLastAccessTime); @@ -498,8 +546,10 @@ public bool ShouldIgnore() public static IEnumerable EnumerateEntriesUnix(string path, string pattern, SearchOption option) { + StdString str = path; + CorrectPath(str); UnsafeStack walkStack = new(); - walkStack.Push(path); + walkStack.Push(str); while (walkStack.TryPop(out var dir)) { @@ -555,17 +605,26 @@ private static bool TryReadDir(nint dirHandle, out DirEnt dirEnt) private static FileMetadata Convert(DirEnt entry, StdString path) { - MemoryDump(&entry); int length = NET.Utilities.Utils.StrLen(entry.d_name); - StdWString str = new(path.Size + 1 + length); - str.Append(path); - str.Append('/'); - str.Append(entry.d_name); + StdWString str; + if (path.Data[path.Size - 1] != '/') + { + str = new(path.Size + 1 + length); + str.Append(path); + str.Append('/'); + str.Append(entry.d_name); + } + else + { + str = new(path.Size + length); + str.Append(path); + str.Append(entry.d_name); + } *(str.Data + str.Size) = '\0'; - FileMetadata meta = new(); - meta.Path = str; + FileMetadata meta = default; FileStat(str, out var stat); + meta.Path = str; meta.CreationTime = DateTimeOffset.FromUnixTimeSeconds(stat.StCtime).LocalDateTime.AddTicks((long)(stat.StCtimensec / 100)); meta.LastAccessTime = DateTimeOffset.FromUnixTimeSeconds(stat.StAtime).LocalDateTime.AddTicks((long)(stat.StAtimensec / 100)); meta.LastWriteTime = DateTimeOffset.FromUnixTimeSeconds(stat.StMtime).LocalDateTime.AddTicks((long)(stat.StMtimensec / 100)); @@ -773,8 +832,10 @@ public bool ShouldIgnore() public static IEnumerable EnumerateEntriesOSX(string path, string pattern, SearchOption option) { + StdString str = path; + CorrectPath(str); UnsafeStack walkStack = new(); - walkStack.Push(path); + walkStack.Push(str); while (walkStack.TryPop(out var dir)) { @@ -832,10 +893,21 @@ private static bool OSXTryReadDir(nint dirHandle, out OSXDirEnt dirEnt) private static FileMetadata OSXConvert(OSXDirEnt entry, StdString path) { int length = entry.d_namlen; - StdWString str = new(path.Size + 1 + length); - str.Append(path); - str.Append('/'); - str.Append(entry.d_name, length); + StdWString str; + if (path.Data[path.Size - 1] != '/') + { + str = new(path.Size + 1 + length); + str.Append(path); + str.Append('/'); + str.Append(entry.d_name, length); + } + else + { + str = new(path.Size + length); + str.Append(path); + str.Append(entry.d_name, length); + } + *(str.Data + str.Size) = '\0'; FileMetadata meta = default;