Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rcore] Add filtering folders to LoadDirectoryFilesEx()/ScanDirectoryFiles() #4302

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@
#define MAGENTA CLITERAL(Color){ 255, 0, 255, 255 } // Magenta
#define RAYWHITE CLITERAL(Color){ 245, 245, 245, 255 } // My own White (raylib logo)

// Filter string used in ScanDirectoryFiles, ScanDirectoryFilesRecursively and
// LoadDirectoryFilesEx to include directories in the result array
#define FILTER_FOLDER "/DIR"
raysan5 marked this conversation as resolved.
Show resolved Hide resolved

//----------------------------------------------------------------------------------
// Structures Definition
//----------------------------------------------------------------------------------
Expand Down
34 changes: 30 additions & 4 deletions src/rcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -3313,10 +3313,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 @@ -3376,7 +3387,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