Skip to content

Commit

Permalink
[rcore] Add filtering folders to LoadDirectoryFilesEx()/`ScanDirect…
Browse files Browse the repository at this point in the history
…oryFiles()` (#4302)

* Add filtering folders in ScanDirectoryFiles and ScanDirectoryFilesRecursively

Add define FILTER_FOLDER for that purpose
Fix folder names matching filter being added to result

* Move FILTER_FOLDER define to internals of rcore and document option in comment
  • Loading branch information
foxblock authored Sep 15, 2024
1 parent 329b2df commit 16e9317
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ RLAPI bool ChangeDirectory(const char *dir); // Change work
RLAPI bool IsPathFile(const char *path); // Check if a given path is a file or a directory
RLAPI bool IsFileNameValid(const char *fileName); // Check if fileName is valid for the platform/OS
RLAPI FilePathList LoadDirectoryFiles(const char *dirPath); // Load directory filepaths
RLAPI FilePathList LoadDirectoryFilesEx(const char *basePath, const char *filter, bool scanSubdirs); // Load directory filepaths with extension filtering and recursive directory scan
RLAPI FilePathList LoadDirectoryFilesEx(const char *basePath, const char *filter, bool scanSubdirs); // Load directory filepaths with extension filtering and recursive directory scan. Use "/DIR" in the filter string to include directories in the result
RLAPI void UnloadDirectoryFiles(FilePathList files); // Unload filepaths
RLAPI bool IsFileDropped(void); // Check if a file has been dropped into window
RLAPI FilePathList LoadDroppedFiles(void); // Load dropped filepaths
Expand Down
38 changes: 34 additions & 4 deletions src/rcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ unsigned int __stdcall timeEndPeriod(unsigned int uPeriod);
#define MAX_AUTOMATION_EVENTS 16384 // Maximum number of automation events to record
#endif

#ifndef FILTER_FOLDER
#define FILTER_FOLDER "/DIR" // Filter string used in ScanDirectoryFiles, ScanDirectoryFilesRecursively and LoadDirectoryFilesEx to include directories in the result array
#endif

// Flags operation macros
#define FLAG_SET(n, f) ((n) |= (f))
#define FLAG_CLEAR(n, f) ((n) &= ~(f))
Expand Down Expand Up @@ -3339,10 +3343,21 @@ static void ScanDirectoryFiles(const char *basePath, FilePathList *files, const

if (filter != NULL)
{
if (IsFileExtension(path, filter))
if (IsPathFile(path))
{
strcpy(files->paths[files->count], path);
files->count++;
if (IsFileExtension(path, filter))
{
strcpy(files->paths[files->count], path);
files->count++;
}
}
else
{
if (TextFindIndex(filter, FILTER_FOLDER) >= 0)
{
strcpy(files->paths[files->count], path);
files->count++;
}
}
}
else
Expand Down Expand Up @@ -3402,7 +3417,22 @@ static void ScanDirectoryFilesRecursively(const char *basePath, FilePathList *fi
break;
}
}
else ScanDirectoryFilesRecursively(path, files, filter);
else
{
if (filter != NULL && TextFindIndex(filter, FILTER_FOLDER) >= 0)
{
strcpy(files->paths[files->count], path);
files->count++;
}

if (files->count >= files->capacity)
{
TRACELOG(LOG_WARNING, "FILEIO: Maximum filepath scan capacity reached (%i files)", files->capacity);
break;
}

ScanDirectoryFilesRecursively(path, files, filter);
}
}
}

Expand Down

0 comments on commit 16e9317

Please sign in to comment.