Skip to content

Commit cf10cdd

Browse files
authored
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
1 parent f4add5f commit cf10cdd

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

src/raudio.c

+70-4
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 fr
416416
#if defined(RAUDIO_STANDALONE)
417417
static bool IsFileExtension(const char *fileName, const char *ext); // Check file extension
418418
static const char *GetFileExtension(const char *fileName); // Get pointer to extension for a filename string (includes the dot: .png)
419+
static const char *GetFileName(const char *filePath); // Get pointer to filename for a path string
420+
static const char *GetFileNameWithoutExt(const char *filePath); // Get filename string without extension (uses static string)
419421

420422
static unsigned char *LoadFileData(const char *fileName, int *dataSize); // Load file data as byte array (read)
421423
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)
26422644
return dot;
26432645
}
26442646

2647+
// String pointer reverse break: returns right-most occurrence of charset in s
2648+
static const char *strprbrk(const char *s, const char *charset)
2649+
{
2650+
const char *latestMatch = NULL;
2651+
for (; s = strpbrk(s, charset), s != NULL; latestMatch = s++) { }
2652+
return latestMatch;
2653+
}
2654+
2655+
// Get pointer to filename for a path string
2656+
static const char *GetFileName(const char *filePath)
2657+
{
2658+
const char *fileName = NULL;
2659+
if (filePath != NULL) fileName = strprbrk(filePath, "\\/");
2660+
2661+
if (!fileName) return filePath;
2662+
2663+
return fileName + 1;
2664+
}
2665+
2666+
// Get filename string without extension (uses static string)
2667+
static const char *GetFileNameWithoutExt(const char *filePath)
2668+
{
2669+
#define MAX_FILENAMEWITHOUTEXT_LENGTH 256
2670+
2671+
static char fileName[MAX_FILENAMEWITHOUTEXT_LENGTH] = { 0 };
2672+
memset(fileName, 0, MAX_FILENAMEWITHOUTEXT_LENGTH);
2673+
2674+
if (filePath != NULL) strcpy(fileName, GetFileName(filePath)); // Get filename with extension
2675+
2676+
int size = (int)strlen(fileName); // Get size in bytes
2677+
2678+
for (int i = 0; (i < size) && (i < MAX_FILENAMEWITHOUTEXT_LENGTH); i++)
2679+
{
2680+
if (fileName[i] == '.')
2681+
{
2682+
// NOTE: We break on first '.' found
2683+
fileName[i] = '\0';
2684+
break;
2685+
}
2686+
}
2687+
2688+
return fileName;
2689+
}
2690+
26452691
// Load data from file into a buffer
26462692
static unsigned char *LoadFileData(const char *fileName, int *dataSize)
26472693
{
@@ -2699,9 +2745,19 @@ static bool SaveFileData(const char *fileName, void *data, int dataSize)
26992745

27002746
fclose(file);
27012747
}
2702-
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
2748+
else
2749+
{
2750+
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
2751+
return false;
2752+
}
27032753
}
2704-
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
2754+
else
2755+
{
2756+
TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
2757+
return false;
2758+
}
2759+
2760+
return true;
27052761
}
27062762

27072763
// Save text data to file (write), string must be '\0' terminated
@@ -2720,9 +2776,19 @@ static bool SaveFileText(const char *fileName, char *text)
27202776

27212777
fclose(file);
27222778
}
2723-
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
2779+
else
2780+
{
2781+
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
2782+
return false;
2783+
}
27242784
}
2725-
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
2785+
else
2786+
{
2787+
TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
2788+
return false;
2789+
}
2790+
2791+
return true;
27262792
}
27272793
#endif
27282794

0 commit comments

Comments
 (0)