REVIEWED: IsFileExtension() to avoid other modules dependency #5071

This commit is contained in:
Ray
2025-08-08 23:01:04 +02:00
parent f73b04c42c
commit c6241b91a2
2 changed files with 41 additions and 14 deletions

View File

@@ -1129,7 +1129,7 @@ RLAPI bool SaveFileText(const char *fileName, const char *text); // Save text d
// File system functions // File system functions
RLAPI bool FileExists(const char *fileName); // Check if file exists RLAPI bool FileExists(const char *fileName); // Check if file exists
RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists
RLAPI bool IsFileExtension(const char *fileName, const char *ext); // Check file extension (including point: .png, .wav) RLAPI bool IsFileExtension(const char *fileName, const char *ext); // Check file extension (recommended include point: .png, .wav)
RLAPI int GetFileLength(const char *fileName); // Get file length in bytes (NOTE: GetFileSize() conflicts with windows.h) RLAPI int GetFileLength(const char *fileName); // Get file length in bytes (NOTE: GetFileSize() conflicts with windows.h)
RLAPI const char *GetFileExtension(const char *fileName); // Get pointer to extension for a filename string (includes dot: '.png') RLAPI const char *GetFileExtension(const char *fileName); // Get pointer to extension for a filename string (includes dot: '.png')
RLAPI const char *GetFileName(const char *filePath); // Get pointer to filename for a path string RLAPI const char *GetFileName(const char *filePath); // Get pointer to filename for a path string

View File

@@ -113,7 +113,7 @@
#include <stdlib.h> // Required for: srand(), rand(), atexit() #include <stdlib.h> // Required for: srand(), rand(), atexit()
#include <stdio.h> // Required for: sprintf() [Used in OpenURL()] #include <stdio.h> // Required for: sprintf() [Used in OpenURL()]
#include <string.h> // Required for: strrchr(), strcmp(), strlen(), memset() #include <string.h> // Required for: strlen(), strcpy(), strcmp(), strrchr(), memset()
#include <time.h> // Required for: time() [Used in InitTimer()] #include <time.h> // Required for: time() [Used in InitTimer()]
#include <math.h> // Required for: tan() [Used in BeginMode3D()], atan2f() [Used in LoadVrStereoConfig()] #include <math.h> // Required for: tan() [Used in BeginMode3D()], atan2f() [Used in LoadVrStereoConfig()]
@@ -1940,34 +1940,61 @@ bool FileExists(const char *fileName)
} }
// Check file extension // Check file extension
// TODO: Avoid [rtext] module dependency
bool IsFileExtension(const char *fileName, const char *ext) bool IsFileExtension(const char *fileName, const char *ext)
{ {
#define MAX_FILE_EXTENSION_LENGTH 16 #define MAX_FILE_EXTENSIONS 32
bool result = false; bool result = false;
const char *fileExt = GetFileExtension(fileName); const char *fileExt = GetFileExtension(fileName);
if (fileExt != NULL) if (fileExt != NULL)
{ {
#if defined(SUPPORT_MODULE_RTEXT) && defined(SUPPORT_TEXT_MANIPULATION) int fileExtLen = strlen(fileExt);
int extCount = 0; char fileExtLower[8] = { 0 };
char **checkExts = TextSplit(ext, ';', &extCount); // WARNING: Module required: rtext char *fileExtLowerPtr = fileExtLower;
for (int i = 0; i < fileExtLen; i++)
{
// Copy and convert to lower-case
if ((fileExt[i] >= 'A') && (fileExt[i] <= 'Z')) fileExtLower[i] = fileExt[i] + 32;
else fileExtLower[i] = fileExt[i];
}
char fileExtLower[MAX_FILE_EXTENSION_LENGTH + 1] = { 0 }; int extCount = 1;
strncpy(fileExtLower, TextToLower(fileExt), MAX_FILE_EXTENSION_LENGTH); // WARNING: Module required: rtext int extLen = strlen(ext);
char *extList = (char *)RL_CALLOC(extLen + 1, 1);
char *extListPtrs[MAX_FILE_EXTENSIONS] = { 0 };
strcpy(extList, ext);
extListPtrs[0] = extList;
for (int i = 0; i < extLen; i++)
{
// Convert to lower-case if extension is upper-case
if ((extList[i] >= 'A') && (extList[i] <= 'Z')) extList[i] += 32;
// Get pointer to next extension and add null-terminator
if ((extList[i] == ';') && (extCount < (MAX_FILE_EXTENSIONS - 1)))
{
extList[i] = '\0';
extListPtrs[extCount] = extList + i + 1;
extCount++;
}
}
for (int i = 0; i < extCount; i++) for (int i = 0; i < extCount; i++)
{ {
if (strcmp(fileExtLower, TextToLower(checkExts[i])) == 0) // Consider the case where extension provided
// does not start with the '.'
fileExtLowerPtr = fileExtLower;
if (extListPtrs[i][0] != '.') fileExtLowerPtr++;
if (strcmp(fileExtLowerPtr, extListPtrs[i]) == 0)
{ {
result = true; result = true;
break; break;
} }
} }
#else
if (strcmp(fileExt, ext) == 0) result = true; RL_FREE(extList);
#endif
} }
return result; return result;