From e742a2673902af8dddc63de9ee765594fbcd3a84 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 8 Jun 2024 11:45:55 +1000 Subject: [PATCH] DS: Support enumerating files in directory --- misc/macclassic/68APPL.r | 4 ++-- misc/macclassic/ppcAPPL.r | 4 ++-- src/Platform_NDS.c | 40 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/misc/macclassic/68APPL.r b/misc/macclassic/68APPL.r index 272e6bfd6..5a714031a 100644 --- a/misc/macclassic/68APPL.r +++ b/misc/macclassic/68APPL.r @@ -18,6 +18,6 @@ resource 'SIZE' (-1) { reserved, reserved, reserved, - 3584 * 1024, - 8192 * 1024 + 8192 * 1024, // preferred (maximum) memory size + 3584 * 1024, // minimum memory size (enough to load launcher) }; diff --git a/misc/macclassic/ppcAPPL.r b/misc/macclassic/ppcAPPL.r index 5d0c59166..9bea6198b 100644 --- a/misc/macclassic/ppcAPPL.r +++ b/misc/macclassic/ppcAPPL.r @@ -27,6 +27,6 @@ resource 'SIZE' (-1) { reserved, reserved, reserved, - 8192 * 1024, - 8192 * 1024 + 8192 * 1024, // preferred (maximum) memory size + 8192 * 1024 // minimum memory size }; diff --git a/src/Platform_NDS.c b/src/Platform_NDS.c index 03765814f..355e94b04 100644 --- a/src/Platform_NDS.c +++ b/src/Platform_NDS.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "_PlatformConsole.h" const cc_result ReturnCode_FileShareViolation = 1000000000; // not used @@ -136,7 +137,44 @@ int File_Exists(const cc_string* path) { } cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCallback callback) { - return ERR_NOT_SUPPORTED; + cc_string path; char pathBuffer[FILENAME_SIZE]; + char str[NATIVE_STR_LEN]; + struct dirent* entry; + int res; + + String_EncodeUtf8(str, dirPath); + DIR* dirPtr = opendir(str); + if (!dirPtr) return errno; + + /* POSIX docs: "When the end of the directory is encountered, a null pointer is returned and errno is not changed." */ + /* errno is sometimes leftover from previous calls, so always reset it before readdir gets called */ + errno = 0; + String_InitArray(path, pathBuffer); + + while ((entry = readdir(dirPtr))) { + path.length = 0; + String_Format1(&path, "%s/", dirPath); + + /* ignore . and .. entry */ + char* src = entry->d_name; + if (src[0] == '.' && src[1] == '\0') continue; + if (src[0] == '.' && src[1] == '.' && src[2] == '\0') continue; + + int len = String_Length(src); + String_AppendUtf8(&path, src, len); + + if (entry->d_type == DT_DIR) { + res = Directory_Enum(&path, obj, callback); + if (res) { closedir(dirPtr); return res; } + } else { + callback(&path, obj); + } + errno = 0; + } + + res = errno; /* return code from readdir */ + closedir(dirPtr); + return res; } static cc_result File_Do(cc_file* file, const cc_string* path, int mode, const char* type) {