From a511337ce880f6e910cc0b5b20195c6cf1ec70e7 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 10 Oct 2018 12:01:59 +0200 Subject: [PATCH] ADDED: GetFileNameWithoutExt --- src/core.c | 35 +++++++++++++++++++++++++++++++++++ src/raylib.h | 1 + 2 files changed, 36 insertions(+) diff --git a/src/core.c b/src/core.c index f9de55e51..37772e7e7 100644 --- a/src/core.c +++ b/src/core.c @@ -1523,6 +1523,41 @@ const char *GetFileName(const char *filePath) return fileName + 1; } +// Get filename string without extension (memory should be freed) +const char *GetFileNameWithoutExt(const char *filePath) +{ + char *result, *lastDot, *lastSep; + + char nameDot = '.'; // Default filename to extension separator character + char pathSep = '/'; // Default filepath separator character + + // Error checks and allocate string + if (filePath == NULL) return NULL; + + // Try to allocate new string, same size as original + // NOTE: By default strlen() does not count the '\0' character + if ((result = malloc(strlen(filePath) + 1)) == NULL) return NULL; + + strcpy(result, filePath); // Make a copy of the string + + // NOTE: strrchr() returns a pointer to the last occurrence of character + lastDot = strrchr(result, nameDot); + lastSep = (pathSep == 0) ? NULL : strrchr(result, pathSep); + + if (lastDot != NULL) // Check if it has an extension separator... + { + if (lastSep != NULL) // ...and it's before the extenstion separator... + { + if (lastSep < lastDot) + { + *lastDot = '\0'; // ...then remove it + } + } + else *lastDot = '\0'; // Has extension separator with no path separator + } + + return result; // Return the modified string +} // Get directory for a given fileName (with path) const char *GetDirectoryPath(const char *fileName) diff --git a/src/raylib.h b/src/raylib.h index 2bc37906d..3bd16f00c 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -871,6 +871,7 @@ RLAPI int GetRandomValue(int min, int max); // Returns a r RLAPI bool IsFileExtension(const char *fileName, const char *ext);// Check file extension RLAPI const char *GetExtension(const char *fileName); // Get pointer to extension for a filename string RLAPI const char *GetFileName(const char *filePath); // Get pointer to filename for a path string +RLAPI const char *GetFileNameWithoutExt(const char *filePath); // Get filename string without extension (memory should be freed) RLAPI const char *GetDirectoryPath(const char *fileName); // Get full path for a given fileName (uses static string) RLAPI const char *GetWorkingDirectory(void); // Get current working directory (uses static string) RLAPI char **GetDirectoryFiles(const char *dirPath, int *count); // Get filenames in a directory path (memory should be freed)