summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorAlessandro Nikolaev <[email protected]>2024-01-28 12:46:27 +0000
committerGitHub <[email protected]>2024-01-28 13:46:27 +0100
commitcf10cdd6b33718c066ae58f7973b04dabde6efb1 (patch)
treed54cb0c59239287ce142523140a8a47f2bd19258 /src
parentf4add5f10d38c4f3fb3aa748423773f3de18b420 (diff)
downloadraylib-cf10cdd6b33718c066ae58f7973b04dabde6efb1.tar.gz
raylib-cf10cdd6b33718c066ae58f7973b04dabde6efb1.zip
Added missing "standalone" functions to raudio.c & fixed return bug (#3760)
* Added GetFileNameWithoutExt, GetFileName & strprbrk to raudio.c * Gave return values to SaveFileData & SaveFileText in raudio.c
Diffstat (limited to 'src')
-rw-r--r--src/raudio.c74
1 files changed, 70 insertions, 4 deletions
diff --git a/src/raudio.c b/src/raudio.c
index d3648bc8..59d49c96 100644
--- a/src/raudio.c
+++ b/src/raudio.c
@@ -416,6 +416,8 @@ static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 fr
#if defined(RAUDIO_STANDALONE)
static bool IsFileExtension(const char *fileName, const char *ext); // Check file extension
static const char *GetFileExtension(const char *fileName); // Get pointer to extension for a filename string (includes the dot: .png)
+static const char *GetFileName(const char *filePath); // Get pointer to filename for a path string
+static const char *GetFileNameWithoutExt(const char *filePath); // Get filename string without extension (uses static string)
static unsigned char *LoadFileData(const char *fileName, int *dataSize); // Load file data as byte array (read)
static bool SaveFileData(const char *fileName, void *data, int dataSize); // Save data to file from byte array (write)
@@ -2642,6 +2644,50 @@ static const char *GetFileExtension(const char *fileName)
return dot;
}
+// String pointer reverse break: returns right-most occurrence of charset in s
+static const char *strprbrk(const char *s, const char *charset)
+{
+ const char *latestMatch = NULL;
+ for (; s = strpbrk(s, charset), s != NULL; latestMatch = s++) { }
+ return latestMatch;
+}
+
+// Get pointer to filename for a path string
+static const char *GetFileName(const char *filePath)
+{
+ const char *fileName = NULL;
+ if (filePath != NULL) fileName = strprbrk(filePath, "\\/");
+
+ if (!fileName) return filePath;
+
+ return fileName + 1;
+}
+
+// Get filename string without extension (uses static string)
+static const char *GetFileNameWithoutExt(const char *filePath)
+{
+ #define MAX_FILENAMEWITHOUTEXT_LENGTH 256
+
+ static char fileName[MAX_FILENAMEWITHOUTEXT_LENGTH] = { 0 };
+ memset(fileName, 0, MAX_FILENAMEWITHOUTEXT_LENGTH);
+
+ if (filePath != NULL) strcpy(fileName, GetFileName(filePath)); // Get filename with extension
+
+ int size = (int)strlen(fileName); // Get size in bytes
+
+ for (int i = 0; (i < size) && (i < MAX_FILENAMEWITHOUTEXT_LENGTH); i++)
+ {
+ if (fileName[i] == '.')
+ {
+ // NOTE: We break on first '.' found
+ fileName[i] = '\0';
+ break;
+ }
+ }
+
+ return fileName;
+}
+
// Load data from file into a buffer
static unsigned char *LoadFileData(const char *fileName, int *dataSize)
{
@@ -2699,9 +2745,19 @@ static bool SaveFileData(const char *fileName, void *data, int dataSize)
fclose(file);
}
- else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
+ else
+ {
+ TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
+ return false;
+ }
}
- else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
+ else
+ {
+ TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
+ return false;
+ }
+
+ return true;
}
// Save text data to file (write), string must be '\0' terminated
@@ -2720,9 +2776,19 @@ static bool SaveFileText(const char *fileName, char *text)
fclose(file);
}
- else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
+ else
+ {
+ TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
+ return false;
+ }
}
- else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
+ else
+ {
+ TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
+ return false;
+ }
+
+ return true;
}
#endif