Skip to content

Commit

Permalink
Removed debugging code and improved path handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
JunaMeinhold committed Nov 8, 2024
1 parent 27accbf commit d4059b6
Showing 1 changed file with 95 additions and 23 deletions.
118 changes: 95 additions & 23 deletions Hexa.NET.ImGui.Widgets/Dialogs/FileUtilities.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -44,12 +49,49 @@ public static IEnumerable<FileMetadata> 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<FileMetadata> EnumerateEntriesWin(string path, string pattern, SearchOption option)
Expand All @@ -60,6 +102,7 @@ public static IEnumerable<FileMetadata> EnumerateEntriesWin(string path, string
StdWString str = path;
str.Append('\\');
str.Append('*');
CorrectPath(str);
walkStack.Push(str);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -498,8 +546,10 @@ public bool ShouldIgnore()

public static IEnumerable<FileMetadata> EnumerateEntriesUnix(string path, string pattern, SearchOption option)
{
StdString str = path;
CorrectPath(str);
UnsafeStack<StdString> walkStack = new();
walkStack.Push(path);
walkStack.Push(str);

while (walkStack.TryPop(out var dir))
{
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -773,8 +832,10 @@ public bool ShouldIgnore()

public static IEnumerable<FileMetadata> EnumerateEntriesOSX(string path, string pattern, SearchOption option)
{
StdString str = path;
CorrectPath(str);
UnsafeStack<StdString> walkStack = new();
walkStack.Push(path);
walkStack.Push(str);

while (walkStack.TryPop(out var dir))
{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d4059b6

Please sign in to comment.