Skip to content

Commit d4059b6

Browse files
committed
Removed debugging code and improved path handling.
1 parent 27accbf commit d4059b6

File tree

1 file changed

+95
-23
lines changed

1 file changed

+95
-23
lines changed

Hexa.NET.ImGui.Widgets/Dialogs/FileUtilities.cs

Lines changed: 95 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
namespace Hexa.NET.ImGui.Widgets.Dialogs
22
{
33
using Hexa.NET.Utilities;
4-
using Microsoft.CodeAnalysis;
54
using System;
65
using System.Collections.Generic;
7-
using System.Diagnostics.CodeAnalysis;
86
using System.IO;
9-
using System.Reflection;
107
using System.Runtime.CompilerServices;
118
using System.Runtime.InteropServices;
129
using System.Text;
@@ -20,6 +17,10 @@ public static long GetFileSize(string filePath)
2017
{
2118
return GetFileMetadataWindows(filePath).Size;
2219
}
20+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
21+
{
22+
return GetFileMetadataOSX(filePath).Size;
23+
}
2324
else
2425
{
2526
return GetFileMetadataUnix(filePath).Size;
@@ -32,6 +33,10 @@ public static FileMetadata GetFileMetadata(string filePath)
3233
{
3334
return GetFileMetadataWindows(filePath);
3435
}
36+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
37+
{
38+
return GetFileMetadataOSX(filePath);
39+
}
3540
else
3641
{
3742
return GetFileMetadataUnix(filePath);
@@ -44,12 +49,49 @@ public static IEnumerable<FileMetadata> EnumerateEntries(string path, string pat
4449
{
4550
return EnumerateEntriesWin(path, pattern, option);
4651
}
52+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
53+
{
54+
return EnumerateEntriesOSX(path, pattern, option);
55+
}
4756
else
4857
{
4958
return EnumerateEntriesUnix(path, pattern, option);
5059
}
5160
}
5261

62+
public static readonly char DirectorySeparatorChar = Path.DirectorySeparatorChar;
63+
public static readonly char AltDirectorySeparatorChar = Path.AltDirectorySeparatorChar;
64+
65+
public static void CorrectPath(StdString str)
66+
{
67+
byte* ptr = str.Data;
68+
byte* end = ptr + str.Size;
69+
while (ptr != end)
70+
{
71+
byte c = *ptr;
72+
if (c == '/' || c == '\\')
73+
{
74+
*ptr = (byte)DirectorySeparatorChar;
75+
}
76+
ptr++;
77+
}
78+
}
79+
80+
public static void CorrectPath(StdWString str)
81+
{
82+
char* ptr = str.Data;
83+
char* end = ptr + str.Size;
84+
while (ptr != end)
85+
{
86+
char c = *ptr;
87+
if (c == '/' || c == '\\')
88+
{
89+
*ptr = DirectorySeparatorChar;
90+
}
91+
ptr++;
92+
}
93+
}
94+
5395
#region WIN32
5496

5597
public static IEnumerable<FileMetadata> EnumerateEntriesWin(string path, string pattern, SearchOption option)
@@ -60,6 +102,7 @@ public static IEnumerable<FileMetadata> EnumerateEntriesWin(string path, string
60102
StdWString str = path;
61103
str.Append('\\');
62104
str.Append('*');
105+
CorrectPath(str);
63106
walkStack.Push(str);
64107
}
65108

@@ -129,19 +172,24 @@ private static nint StartSearch(StdWString st, out WIN32_FIND_DATA data)
129172
[MethodImpl(MethodImplOptions.AggressiveInlining)]
130173
private static FileMetadata Convert(WIN32_FIND_DATA data, StdWString path)
131174
{
132-
FileMetadata metadata = new();
133175
int length = StrLen(data.cFileName);
134-
StdWString str = new(length + path.Size + 1);
135-
for (int i = 0; i < path.Size - 1; i++)
176+
StdWString str;
177+
if (path[path.Size - 1] != '/' || path[path.Size - 1] != '\\')
136178
{
137-
str.Append(path[i]);
179+
str = new(length + 1 + path.Size);
180+
str.Append(path);
181+
str.Append('/');
182+
str.Append(data.cFileName, length);
138183
}
139-
140-
for (int i = 0; i < length; i++)
184+
else
141185
{
142-
str.Append(data.cFileName[i]);
186+
str = new(length + path.Size);
187+
str.Append(path);
188+
str.Append(data.cFileName, length);
143189
}
190+
*(str.Data + str.Size) = '\0';
144191

192+
FileMetadata metadata = new();
145193
metadata.Path = str;
146194
metadata.CreationTime = DateTime.FromFileTime(data.ftCreationTime);
147195
metadata.LastAccessTime = DateTime.FromFileTime(data.ftLastAccessTime);
@@ -498,8 +546,10 @@ public bool ShouldIgnore()
498546

499547
public static IEnumerable<FileMetadata> EnumerateEntriesUnix(string path, string pattern, SearchOption option)
500548
{
549+
StdString str = path;
550+
CorrectPath(str);
501551
UnsafeStack<StdString> walkStack = new();
502-
walkStack.Push(path);
552+
walkStack.Push(str);
503553

504554
while (walkStack.TryPop(out var dir))
505555
{
@@ -555,17 +605,26 @@ private static bool TryReadDir(nint dirHandle, out DirEnt dirEnt)
555605

556606
private static FileMetadata Convert(DirEnt entry, StdString path)
557607
{
558-
MemoryDump(&entry);
559608
int length = NET.Utilities.Utils.StrLen(entry.d_name);
560-
StdWString str = new(path.Size + 1 + length);
561-
str.Append(path);
562-
str.Append('/');
563-
str.Append(entry.d_name);
609+
StdWString str;
610+
if (path.Data[path.Size - 1] != '/')
611+
{
612+
str = new(path.Size + 1 + length);
613+
str.Append(path);
614+
str.Append('/');
615+
str.Append(entry.d_name);
616+
}
617+
else
618+
{
619+
str = new(path.Size + length);
620+
str.Append(path);
621+
str.Append(entry.d_name);
622+
}
564623
*(str.Data + str.Size) = '\0';
565-
FileMetadata meta = new();
566-
meta.Path = str;
567624

625+
FileMetadata meta = default;
568626
FileStat(str, out var stat);
627+
meta.Path = str;
569628
meta.CreationTime = DateTimeOffset.FromUnixTimeSeconds(stat.StCtime).LocalDateTime.AddTicks((long)(stat.StCtimensec / 100));
570629
meta.LastAccessTime = DateTimeOffset.FromUnixTimeSeconds(stat.StAtime).LocalDateTime.AddTicks((long)(stat.StAtimensec / 100));
571630
meta.LastWriteTime = DateTimeOffset.FromUnixTimeSeconds(stat.StMtime).LocalDateTime.AddTicks((long)(stat.StMtimensec / 100));
@@ -773,8 +832,10 @@ public bool ShouldIgnore()
773832

774833
public static IEnumerable<FileMetadata> EnumerateEntriesOSX(string path, string pattern, SearchOption option)
775834
{
835+
StdString str = path;
836+
CorrectPath(str);
776837
UnsafeStack<StdString> walkStack = new();
777-
walkStack.Push(path);
838+
walkStack.Push(str);
778839

779840
while (walkStack.TryPop(out var dir))
780841
{
@@ -832,10 +893,21 @@ private static bool OSXTryReadDir(nint dirHandle, out OSXDirEnt dirEnt)
832893
private static FileMetadata OSXConvert(OSXDirEnt entry, StdString path)
833894
{
834895
int length = entry.d_namlen;
835-
StdWString str = new(path.Size + 1 + length);
836-
str.Append(path);
837-
str.Append('/');
838-
str.Append(entry.d_name, length);
896+
StdWString str;
897+
if (path.Data[path.Size - 1] != '/')
898+
{
899+
str = new(path.Size + 1 + length);
900+
str.Append(path);
901+
str.Append('/');
902+
str.Append(entry.d_name, length);
903+
}
904+
else
905+
{
906+
str = new(path.Size + length);
907+
str.Append(path);
908+
str.Append(entry.d_name, length);
909+
}
910+
839911
*(str.Data + str.Size) = '\0';
840912

841913
FileMetadata meta = default;

0 commit comments

Comments
 (0)