From c335c3c52c27ee68765ab3c3dde65190dbebe55c Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 29 May 2024 17:01:42 +0200 Subject: [PATCH] ADDED: `IsFileNameValid()` --- src/raylib.h | 1 + src/rcore.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/raylib.h b/src/raylib.h index c53ea555b..e810dcdca 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1124,6 +1124,7 @@ RLAPI const char *GetWorkingDirectory(void); // Get current RLAPI const char *GetApplicationDirectory(void); // Get the directory of the running application (uses static string) RLAPI bool ChangeDirectory(const char *dir); // Change working directory, return true on success 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 void UnloadDirectoryFiles(FilePathList files); // Unload filepaths diff --git a/src/rcore.c b/src/rcore.c index 1db865187..3aa5a7a65 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -2244,6 +2244,64 @@ bool IsPathFile(const char *path) return S_ISREG(result.st_mode); } +// Check if fileName is valid for the platform/OS +bool IsFileNameValid(const char *fileName) +{ + bool valid = true; + + if ((fileName != NULL) && (fileName[0] != '\0')) + { + int length = strlen(fileName); + bool allPeriods = true; + + for (int i = 0; i < length; i++) + { + // Check invalid characters + if ((fileName[i] == '<') || + (fileName[i] == '>') || + (fileName[i] == ':') || + (fileName[i] == '\"') || + (fileName[i] == '/') || + (fileName[i] == '\\') || + (fileName[i] == '|') || + (fileName[i] == '?') || + (fileName[i] == '*')) { valid = false; break; } + + // Check non-glyph characters + if ((unsigned char)fileName[i] < 32) { valid = false; break; } + + // TODO: Check trailing periods/spaces? + + // Check if filename is not all periods + if (fileName[i] != '.') allPeriods = false; + } + + if (allPeriods) valid = false; + +/* + if (valid) + { + // Check invalid DOS names + if (length >= 3) + { + if (((fileName[0] == 'C') && (fileName[1] == 'O') && (fileName[2] == 'N')) || // CON + ((fileName[0] == 'P') && (fileName[1] == 'R') && (fileName[2] == 'N')) || // PRN + ((fileName[0] == 'A') && (fileName[1] == 'U') && (fileName[2] == 'X')) || // AUX + ((fileName[0] == 'N') && (fileName[1] == 'U') && (fileName[2] == 'L'))) valid = false; // NUL + } + + if (length >= 4) + { + if (((fileName[0] == 'C') && (fileName[1] == 'O') && (fileName[2] == 'M') && ((fileName[3] >= '0') && (fileName[3] <= '9'))) || // COM0-9 + ((fileName[0] == 'L') && (fileName[1] == 'P') && (fileName[2] == 'T') && ((fileName[3] >= '0') && (fileName[3] <= '9')))) valid = false; // LPT0-9 + } + } +*/ + } + + return valid; +} + // Check if a file has been dropped into window bool IsFileDropped(void) {