Fixed file/alloc limitation in GetDirectoryFiles() (#2126)
MAX_DIRECTORY_FILES macro removed. ClearDirectoryFiles() tweaked to reflect changes.
This commit is contained in:
parent
da7e0ba9d9
commit
91135c8799
1 changed files with 17 additions and 20 deletions
37
src/rcore.c
37
src/rcore.c
|
@ -2894,41 +2894,38 @@ const char *GetWorkingDirectory(void)
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get filenames in a directory path (max 512 files)
|
// Get filenames in a directory path
|
||||||
// NOTE: Files count is returned by parameters pointer
|
// NOTE: Files count is returned by parameters pointer
|
||||||
char **GetDirectoryFiles(const char *dirPath, int *fileCount)
|
char **GetDirectoryFiles(const char *dirPath, int *fileCount)
|
||||||
{
|
{
|
||||||
#define MAX_DIRECTORY_FILES 512
|
|
||||||
|
|
||||||
ClearDirectoryFiles();
|
ClearDirectoryFiles();
|
||||||
|
|
||||||
// Memory allocation for MAX_DIRECTORY_FILES
|
|
||||||
dirFilesPath = (char **)RL_MALLOC(MAX_DIRECTORY_FILES*sizeof(char *));
|
|
||||||
for (int i = 0; i < MAX_DIRECTORY_FILES; i++) dirFilesPath[i] = (char *)RL_MALLOC(MAX_FILEPATH_LENGTH*sizeof(char));
|
|
||||||
|
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
struct dirent *entity;
|
struct dirent *entity;
|
||||||
DIR *dir = opendir(dirPath);
|
DIR *dir = opendir(dirPath);
|
||||||
|
|
||||||
if (dir != NULL) // It's a directory
|
if (dir != NULL) // It's a directory
|
||||||
{
|
{
|
||||||
// TODO: Reading could be done in two passes,
|
// Count files
|
||||||
// first one to count files and second one to read names
|
while ((entity = readdir(dir)) != NULL) counter++;
|
||||||
// That way we can allocate required memory, instead of a limited pool
|
|
||||||
|
|
||||||
while ((entity = readdir(dir)) != NULL)
|
dirFileCount = counter;
|
||||||
{
|
*fileCount = dirFileCount;
|
||||||
strcpy(dirFilesPath[counter], entity->d_name);
|
|
||||||
counter++;
|
// Memory allocation for dirFileCount
|
||||||
}
|
dirFilesPath = (char **)RL_MALLOC(dirFileCount*sizeof(char *));
|
||||||
|
for (int i = 0; i < dirFileCount; i++) dirFilesPath[i] = (char *)RL_MALLOC(MAX_FILEPATH_LENGTH*sizeof(char));
|
||||||
|
|
||||||
|
// Reset our position in the directory to the beginning
|
||||||
|
rewinddir(dir);
|
||||||
|
|
||||||
|
// Read file names
|
||||||
|
for (int i = 0; (entity = readdir(dir)) != NULL; i++) strcpy(dirFilesPath[i], entity->d_name);
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
}
|
}
|
||||||
else TRACELOG(LOG_WARNING, "FILEIO: Failed to open requested directory"); // Maybe it's a file...
|
else TRACELOG(LOG_WARNING, "FILEIO: Failed to open requested directory"); // Maybe it's a file...
|
||||||
|
|
||||||
dirFileCount = counter;
|
|
||||||
*fileCount = dirFileCount;
|
|
||||||
|
|
||||||
return dirFilesPath;
|
return dirFilesPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2937,7 +2934,7 @@ void ClearDirectoryFiles(void)
|
||||||
{
|
{
|
||||||
if (dirFileCount > 0)
|
if (dirFileCount > 0)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < MAX_DIRECTORY_FILES; i++) RL_FREE(dirFilesPath[i]);
|
for (int i = 0; i < dirFileCount; i++) RL_FREE(dirFilesPath[i]);
|
||||||
|
|
||||||
RL_FREE(dirFilesPath);
|
RL_FREE(dirFilesPath);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue