Skip to content

Commit b8b62c7

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents b24c2fa + 3116d04 commit b8b62c7

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

BeefLibs/corlib/src/IO/FileDialog.bf

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,17 @@ abstract class CommonDialog
677677
return (DialogResult)mResult;
678678
}
679679

680+
private static uint8 HexToInt(char8 c)
681+
{
682+
if (c >= '0' && c <= '9')
683+
return (uint8)(c - '0');
684+
if (c >= 'a' && c <= 'f')
685+
return (uint8)(c - 'a' + 10);
686+
if (c >= 'A' && c <= 'F')
687+
return (uint8)(c - 'A' + 10);
688+
return 0;
689+
}
690+
680691
private static int32 ParseResponse(Linux.DBusMsg* response, void* ptr, Linux.DBusErr* error)
681692
{
682693
Self dia = (.)Internal.UnsafeCastToObject(ptr);
@@ -688,7 +699,9 @@ abstract class CommonDialog
688699
while(Linux.SdBusMessagePeekType(response, null, null) != 0)
689700
{
690701
Linux.SdBusMessageEnterContainer(response, .DictEntry, "sv");
691-
Linux.SdBusMessageReadBasic(response, .String, &key);
702+
if (Linux.SdBusMessageReadBasic(response, .String, &key) < 0)
703+
key = null;
704+
692705
switch(StringView(key))
693706
{
694707
case "uris":
@@ -699,8 +712,34 @@ abstract class CommonDialog
699712
while(Linux.SdBusMessagePeekType(response, null, null) != 0)
700713
{
701714
char8* uri = ?;
702-
Linux.SdBusMessageReadBasic(response, .String, &uri);
703-
uris.Add(new .(StringView(uri+7))); // Removing the "file://" prefix
715+
if (Linux.SdBusMessageReadBasic(response, .String, &uri) < 0)
716+
break;
717+
718+
const String FILE_PREFIX = "file://";
719+
720+
let uriView = StringView(uri);
721+
if (!uriView.StartsWith(FILE_PREFIX))
722+
break;
723+
724+
String parsedUri = new .(uriView.Length - FILE_PREFIX.Length);
725+
// Parse URI, skip file:// prefix
726+
for (int i = FILE_PREFIX.Length; i < uriView.Length; i++)
727+
{
728+
let c = uriView[i];
729+
if (c == '%')
730+
{
731+
if (i + 2 < uriView.Length)
732+
{
733+
let byteVal = (HexToInt(uriView[i + 1]) << 4) | HexToInt(uriView[i + 2]);
734+
parsedUri.Append((char8)byteVal);
735+
}
736+
i += 2;
737+
continue;
738+
}
739+
parsedUri.Append(c);
740+
}
741+
742+
uris.Add(parsedUri);
704743
}
705744
Linux.SdBusMessageExitContainer(response);
706745
Linux.SdBusMessageExitContainer(response);

0 commit comments

Comments
 (0)