diff options
| author | Ray <[email protected]> | 2020-02-26 20:29:33 +0100 |
|---|---|---|
| committer | Ray <[email protected]> | 2020-02-26 20:29:33 +0100 |
| commit | 10464493391f1b4c7714caf0eb45cc76f0053c1e (patch) | |
| tree | 3b873bb1b9351c2ae9cc465052fa3a868f34a92b /src/utils.c | |
| parent | c5d5d19443575cc5b5961f25f3fa00d20dca627f (diff) | |
| download | raylib-10464493391f1b4c7714caf0eb45cc76f0053c1e.tar.gz raylib-10464493391f1b4c7714caf0eb45cc76f0053c1e.zip | |
ADDED: LoadFileData(), SaveFileData()
Diffstat (limited to 'src/utils.c')
| -rw-r--r-- | src/utils.c | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/src/utils.c b/src/utils.c index 8a957b45..5d8f92b4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -64,7 +64,7 @@ static int logTypeExit = LOG_ERROR; // Log type that exits static TraceLogCallback logCallback = NULL; // Log callback function pointer #if defined(PLATFORM_ANDROID) -static AAssetManager *assetManager = NULL; // Android assets manager pointer +static AAssetManager *assetManager = NULL; // Android assets manager pointer #endif #if defined(PLATFORM_UWP) @@ -163,6 +163,54 @@ void TraceLog(int logType, const char *text, ...) #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) // Initialize asset manager from android app void InitAssetManager(AAssetManager *manager) |
