summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRay <[email protected]>2024-05-29 17:01:42 +0200
committerRay <[email protected]>2024-05-29 17:01:42 +0200
commitc335c3c52c27ee68765ab3c3dde65190dbebe55c (patch)
tree3f7aa65a072395e39a90a1a61e1c4b8b87227553
parent797de0f9ad6f69f78131591f9393e9e0ef9486a2 (diff)
downloadraylib-c335c3c52c27ee68765ab3c3dde65190dbebe55c.tar.gz
raylib-c335c3c52c27ee68765ab3c3dde65190dbebe55c.zip
ADDED: `IsFileNameValid()`
-rw-r--r--src/raylib.h1
-rw-r--r--src/rcore.c58
2 files changed, 59 insertions, 0 deletions
diff --git a/src/raylib.h b/src/raylib.h
index c53ea555..e810dcdc 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 1db86518..3aa5a7a6 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)
{