diff options
| author | Ray <[email protected]> | 2021-11-11 18:26:28 +0100 |
|---|---|---|
| committer | Ray <[email protected]> | 2021-11-11 18:26:28 +0100 |
| commit | 2c38dad214cf21cbc36c7710f3595db6ed1ad317 (patch) | |
| tree | e8f7abf28b7115d73dc849669ce7a105677fc5bc /src | |
| parent | c8f029dba9be8804b6dad2b81aae9dc29c283e91 (diff) | |
| parent | 802a1a15290dbae19e9bc4a7d4e468da990a3710 (diff) | |
| download | raylib-2c38dad214cf21cbc36c7710f3595db6ed1ad317.tar.gz raylib-2c38dad214cf21cbc36c7710f3595db6ed1ad317.zip | |
Merge branch 'master' of https://github.com/raysan5/raylib
Diffstat (limited to 'src')
| -rw-r--r-- | src/rcore.c | 37 |
1 files changed, 17 insertions, 20 deletions
diff --git a/src/rcore.c b/src/rcore.c index d24b38f9..ecdee345 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -2894,41 +2894,38 @@ const char *GetWorkingDirectory(void) 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 char **GetDirectoryFiles(const char *dirPath, int *fileCount) { - #define MAX_DIRECTORY_FILES 512 - 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; struct dirent *entity; 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, - // first one to count files and second one to read names - // That way we can allocate required memory, instead of a limited pool + // Count files + while ((entity = readdir(dir)) != NULL) counter++; - while ((entity = readdir(dir)) != NULL) - { - strcpy(dirFilesPath[counter], entity->d_name); - counter++; - } + dirFileCount = counter; + *fileCount = dirFileCount; + + // 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); } else TRACELOG(LOG_WARNING, "FILEIO: Failed to open requested directory"); // Maybe it's a file... - dirFileCount = counter; - *fileCount = dirFileCount; - return dirFilesPath; } @@ -2937,7 +2934,7 @@ void ClearDirectoryFiles(void) { 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); } |
