ADDED: LoadFileData(), SaveFileData()

This commit is contained in:
Ray 2020-02-26 20:29:33 +01:00
parent c5d5d19443
commit 1046449339
2 changed files with 51 additions and 1 deletions

View File

@ -949,6 +949,8 @@ RLAPI void TakeScreenshot(const char *fileName); // Takes a scr
RLAPI int GetRandomValue(int min, int max); // Returns a random value between min and max (both included) RLAPI int GetRandomValue(int min, int max); // Returns a random value between min and max (both included)
// Files management functions // Files management functions
RLAPI unsigned char *LoadFileData(const char *fileName, int *bytesRead); // Load file data as byte array (read)
RLAPI void SaveFileData(const char *fileName, void *data, int bytesToWrite); // Save data to file from byte array (write)
RLAPI bool FileExists(const char *fileName); // Check if file exists RLAPI bool FileExists(const char *fileName); // Check if file exists
RLAPI bool IsFileExtension(const char *fileName, const char *ext);// Check file extension RLAPI bool IsFileExtension(const char *fileName, const char *ext);// Check file extension
RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists

View File

@ -163,6 +163,54 @@ void TraceLog(int logType, const char *text, ...)
#endif // SUPPORT_TRACELOG #endif // SUPPORT_TRACELOG
} }
// Load data from file into a buffer
unsigned char *LoadFileData(const char *fileName, int *bytesRead)
{
unsigned char *data = NULL;
*bytesRead = 0;
FILE *file = fopen(fileName, "rb");
if (file != NULL)
{
fseek(file, 0, SEEK_END);
int size = ftell(file);
fseek(file, 0, SEEK_SET);
if (size > 0)
{
data = (unsigned char *)RL_MALLOC(sizeof(unsigned char)*size);
int count = fread(data, sizeof(unsigned char), size, file);
*bytesRead = count;
if (count != size) TRACELOG(LOG_WARNING, "[%s] File partially read", fileName);
}
else TRACELOG(LOG_WARNING, "[%s] File could not be read", fileName);
fclose(file);
}
else TRACELOG(LOG_WARNING, "[%s] File could not be opened", fileName);
return data;
}
// Save data to file from buffer
void SaveFileData(const char *fileName, void *data, int bytesToWrite)
{
FILE *file = fopen(fileName, "wb");
if (file != NULL)
{
int count = fwrite(data, sizeof(unsigned char), bytesToWrite, file);
if (count == 0) TRACELOG(LOG_WARNING, "[%s] File could not be written", fileName);
else if (count != bytesToWrite) TRACELOG(LOG_WARNING, "[%s] File partially written", fileName);
fclose(file);
}
else TRACELOG(LOG_WARNING, "[%s] File could not be opened", fileName);
}
#if defined(PLATFORM_ANDROID) #if defined(PLATFORM_ANDROID)
// Initialize asset manager from android app // Initialize asset manager from android app
void InitAssetManager(AAssetManager *manager) void InitAssetManager(AAssetManager *manager)