diff --git a/src/core.c b/src/core.c index 5af90170..a1faa765 100644 --- a/src/core.c +++ b/src/core.c @@ -2299,8 +2299,10 @@ unsigned char *DecompressData(unsigned char *compData, int compDataLength, int * // Save integer value to storage file (to defined position) // NOTE: Storage positions is directly related to file memory layout (4 bytes each integer) -void SaveStorageValue(unsigned int position, int value) +bool SaveStorageValue(unsigned int position, int value) { + bool success = false; + #if defined(SUPPORT_DATA_STORAGE) char path[512] = { 0 }; #if defined(PLATFORM_ANDROID) @@ -2355,7 +2357,7 @@ void SaveStorageValue(unsigned int position, int value) dataPtr[position] = value; } - SaveFileData(path, newFileData, newDataSize); + success = SaveFileData(path, newFileData, newDataSize); RL_FREE(newFileData); } else @@ -2367,10 +2369,12 @@ void SaveStorageValue(unsigned int position, int value) int *dataPtr = (int *)fileData; dataPtr[position] = value; - SaveFileData(path, fileData, dataSize); + success = SaveFileData(path, fileData, dataSize); RL_FREE(fileData); } #endif + + return success; } // Load integer value from storage file (from defined position) diff --git a/src/models.c b/src/models.c index 68de0504..ab3ea7f7 100644 --- a/src/models.c +++ b/src/models.c @@ -823,8 +823,10 @@ void UnloadMesh(Mesh mesh) } // Export mesh data to file -void ExportMesh(Mesh mesh, const char *fileName) +bool ExportMesh(Mesh mesh, const char *fileName) { + bool success = false; + if (IsFileExtension(fileName, ".obj")) { // Estimated data size, it should be enough... @@ -875,7 +877,7 @@ void ExportMesh(Mesh mesh, const char *fileName) bytesCount += sprintf(txtData + bytesCount, "\n"); // NOTE: Text data length exported is determined by '\0' (NULL) character - SaveFileText(fileName, txtData); + success = SaveFileText(fileName, txtData); RL_FREE(txtData); } @@ -883,6 +885,8 @@ void ExportMesh(Mesh mesh, const char *fileName) { // TODO: Support additional file formats to export mesh vertex data } + + return success; } // Load materials from model file diff --git a/src/raudio.c b/src/raudio.c index faab6654..7e6e0f30 100644 --- a/src/raudio.c +++ b/src/raudio.c @@ -386,8 +386,8 @@ static Wave LoadMP3(const unsigned char *fileData, unsigned int fileSize); // #if defined(RAUDIO_STANDALONE) static bool IsFileExtension(const char *fileName, const char *ext); // Check file extension static unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead); // Load file data as byte array (read) -static void SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite); // Save data to file from byte array (write) -static void SaveFileText(const char *fileName, char *text); // Save text data to file (write), string must be '\0' terminated +static bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite); // Save data to file from byte array (write) +static bool SaveFileText(const char *fileName, char *text); // Save text data to file (write), string must be '\0' terminated #endif //---------------------------------------------------------------------------------- @@ -812,7 +812,7 @@ void UpdateSound(Sound sound, const void *data, int samplesCount) } // Export wave data to file -void ExportWave(Wave wave, const char *fileName) +bool ExportWave(Wave wave, const char *fileName) { bool success = false; @@ -824,17 +824,20 @@ void ExportWave(Wave wave, const char *fileName) { // Export raw sample data (without header) // NOTE: It's up to the user to track wave parameters - SaveFileData(fileName, wave.data, wave.sampleCount*wave.sampleSize/8); - success = true; + success = SaveFileData(fileName, wave.data, wave.sampleCount*wave.sampleSize/8); } if (success) TRACELOG(LOG_INFO, "FILEIO: [%s] Wave data exported successfully", fileName); else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export wave data", fileName); + + return success; } // Export wave sample data to code (.h) -void ExportWaveAsCode(Wave wave, const char *fileName) +bool ExportWaveAsCode(Wave wave, const char *fileName) { + bool success = false; + #ifndef TEXT_BYTES_PER_LINE #define TEXT_BYTES_PER_LINE 20 #endif @@ -878,9 +881,11 @@ void ExportWaveAsCode(Wave wave, const char *fileName) bytesCount += sprintf(txtData + bytesCount, "0x%x };\n", ((unsigned char *)wave.data)[waveDataSize - 1]); // NOTE: Text data length exported is determined by '\0' (NULL) character - SaveFileText(fileName, txtData); + success = SaveFileText(fileName, txtData); RL_FREE(txtData); + + return success; } // Play a sound @@ -1942,6 +1947,8 @@ static Wave LoadWAV(const unsigned char *fileData, unsigned int fileSize) // NOTE: Using dr_wav library static int SaveWAV(Wave wave, const char *fileName) { + int success = false; + drwav wav = { 0 }; drwav_data_format format = { 0 }; format.container = drwav_container_riff; @@ -1952,14 +1959,15 @@ static int SaveWAV(Wave wave, const char *fileName) unsigned char *fileData = NULL; unsigned int fileDataSize = 0; - drwav_init_memory_write(&wav, &fileData, &fileDataSize, &format, NULL); - drwav_write_pcm_frames(&wav, wave.sampleCount/wave.channels, wave.data); - drwav_uninit(&wav); - - SaveFileData(fileName, fileData, fileDataSize); + success = drwav_init_memory_write(&wav, &fileData, &fileDataSize, &format, NULL); + if (success) success = drwav_write_pcm_frames(&wav, wave.sampleCount/wave.channels, wave.data); + drwav_result result = drwav_uninit(&wav); + + if (result == DRWAV_SUCCESS) success = SaveFileData(fileName, fileData, fileDataSize); + drwav_free(fileData, NULL); - return true; + return success; } #endif @@ -2110,7 +2118,7 @@ static unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead } // Save data to file from buffer -static void SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite) +static bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite) { if (fileName != NULL) { @@ -2132,7 +2140,7 @@ static void SaveFileData(const char *fileName, void *data, unsigned int bytesToW } // Save text data to file (write), string must be '\0' terminated -static void SaveFileText(const char *fileName, char *text) +static bool SaveFileText(const char *fileName, char *text) { if (fileName != NULL) { diff --git a/src/raylib.h b/src/raylib.h index 7d938389..3a6c4b9c 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -971,9 +971,9 @@ RLAPI int GetRandomValue(int min, int max); // Returns a r // Files management functions RLAPI unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead); // Load file data as byte array (read) -RLAPI void SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite); // Save data to file from byte array (write) +RLAPI bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite); // Save data to file from byte array (write), returns true on success RLAPI char *LoadFileText(const char *fileName); // Load text data from file (read), returns a '\0' terminated string -RLAPI void SaveFileText(const char *fileName, char *text); // Save text data to file (write), string must be '\0' terminated +RLAPI bool SaveFileText(const char *fileName, char *text); // Save text data to file (write), string must be '\0' terminated, returns true on success RLAPI bool FileExists(const char *fileName); // Check if file exists RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists RLAPI bool IsFileExtension(const char *fileName, const char *ext);// Check file extension (including point: .png, .wav) @@ -995,7 +995,7 @@ RLAPI unsigned char *CompressData(unsigned char *data, int dataLength, int *comp RLAPI unsigned char *DecompressData(unsigned char *compData, int compDataLength, int *dataLength); // Decompress data (DEFLATE algorithm) // Persistent storage management -RLAPI void SaveStorageValue(unsigned int position, int value); // Save integer value to storage file (to defined position) +RLAPI bool SaveStorageValue(unsigned int position, int value); // Save integer value to storage file (to defined position) RLAPI int LoadStorageValue(unsigned int position); // Load integer value from storage file (from defined position) RLAPI void OpenURL(const char *url); // Open URL with default system browser (if available) @@ -1128,8 +1128,8 @@ RLAPI Image LoadImageRaw(const char *fileName, int width, int height, int format RLAPI Image LoadImageAnim(const char *fileName, int *frames); // Load image sequence from file (frames appended to image.data) RLAPI Image LoadImageFromMemory(const char *fileType, const unsigned char *fileData, int dataSize); // Load image from memory buffer, fileType refers to extension: i.e. "png" RLAPI void UnloadImage(Image image); // Unload image from CPU memory (RAM) -RLAPI void ExportImage(Image image, const char *fileName); // Export image data to file -RLAPI void ExportImageAsCode(Image image, const char *fileName); // Export image as code file defining an array of bytes +RLAPI bool ExportImage(Image image, const char *fileName); // Export image data to file, returns true on success +RLAPI bool ExportImageAsCode(Image image, const char *fileName); // Export image as code file defining an array of bytes // Image generation functions RLAPI Image GenImageColor(int width, int height, Color color); // Generate image: plain color @@ -1322,8 +1322,8 @@ RLAPI void UnloadModel(Model model); // Mesh loading/unloading functions RLAPI Mesh *LoadMeshes(const char *fileName, int *meshCount); // Load meshes from model file -RLAPI void ExportMesh(Mesh mesh, const char *fileName); // Export mesh data to file RLAPI void UnloadMesh(Mesh mesh); // Unload mesh from memory (RAM and/or VRAM) +RLAPI bool ExportMesh(Mesh mesh, const char *fileName); // Export mesh data to file // Material loading/unloading functions RLAPI Material *LoadMaterials(const char *fileName, int *materialCount); // Load materials from model file @@ -1445,8 +1445,8 @@ RLAPI Sound LoadSoundFromWave(Wave wave); // Load so RLAPI void UpdateSound(Sound sound, const void *data, int samplesCount);// Update sound buffer with new data RLAPI void UnloadWave(Wave wave); // Unload wave data RLAPI void UnloadSound(Sound sound); // Unload sound -RLAPI void ExportWave(Wave wave, const char *fileName); // Export wave data to file -RLAPI void ExportWaveAsCode(Wave wave, const char *fileName); // Export wave sample data to code (.h) +RLAPI bool ExportWave(Wave wave, const char *fileName); // Export wave data to file +RLAPI bool ExportWaveAsCode(Wave wave, const char *fileName); // Export wave sample data to code (.h) // Wave/Sound management functions RLAPI void PlaySound(Sound sound); // Play a sound diff --git a/src/textures.c b/src/textures.c index cd468b2a..897905cf 100644 --- a/src/textures.c +++ b/src/textures.c @@ -395,7 +395,7 @@ void UnloadImage(Image image) // Export image data to file // NOTE: File format depends on fileName extension -void ExportImage(Image image, const char *fileName) +bool ExportImage(Image image, const char *fileName) { int success = 0; @@ -436,8 +436,7 @@ void ExportImage(Image image, const char *fileName) { // Export raw pixel data (without header) // NOTE: It's up to the user to track image parameters - SaveFileData(fileName, image.data, GetPixelDataSize(image.width, image.height, image.format)); - success = true; + success = SaveFileData(fileName, image.data, GetPixelDataSize(image.width, image.height, image.format)); } if (allocatedData) RL_FREE(imgData); @@ -445,11 +444,15 @@ void ExportImage(Image image, const char *fileName) if (success != 0) TRACELOG(LOG_INFO, "FILEIO: [%s] Image exported successfully", fileName); else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export image", fileName); + + return success; } // Export image as code file (.h) defining an array of bytes -void ExportImageAsCode(Image image, const char *fileName) +bool ExportImageAsCode(Image image, const char *fileName) { + bool success = false; + #ifndef TEXT_BYTES_PER_LINE #define TEXT_BYTES_PER_LINE 20 #endif @@ -488,9 +491,11 @@ void ExportImageAsCode(Image image, const char *fileName) bytesCount += sprintf(txtData + bytesCount, "0x%x };\n", ((unsigned char *)image.data)[dataSize - 1]); // NOTE: Text data length exported is determined by '\0' (NULL) character - SaveFileText(fileName, txtData); + success = SaveFileText(fileName, txtData); RL_FREE(txtData); + + return success; } //------------------------------------------------------------------------------------ @@ -4230,12 +4235,12 @@ static int SaveKTX(Image image, const char *fileName) } } - SaveFileData(fileName, fileData, dataSize); + int success = SaveFileData(fileName, fileData, dataSize); RL_FREE(fileData); // Free file data buffer // If all data has been written correctly to file, success = 1 - return true; + return success; } #endif diff --git a/src/utils.c b/src/utils.c index e0e551d6..6595b09f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -204,8 +204,10 @@ unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead) } // Save data to file from buffer -void SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite) +bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite) { + bool success = false; + if (fileName != NULL) { FILE *file = fopen(fileName, "wb"); @@ -218,11 +220,14 @@ void SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite) else if (count != bytesToWrite) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially written", fileName); else TRACELOG(LOG_INFO, "FILEIO: [%s] File saved successfully", fileName); - fclose(file); + int result = fclose(file); + if (result == 0) success = true; } else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName); } else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid"); + + return success; } // Load text data from file, returns a '\0' terminated string @@ -270,8 +275,10 @@ char *LoadFileText(const char *fileName) } // Save text data to file (write), string must be '\0' terminated -void SaveFileText(const char *fileName, char *text) +bool SaveFileText(const char *fileName, char *text) { + bool success = false; + if (fileName != NULL) { FILE *file = fopen(fileName, "wt"); @@ -283,11 +290,14 @@ void SaveFileText(const char *fileName, char *text) if (count < 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write text file", fileName); else TRACELOG(LOG_INFO, "FILEIO: [%s] Text file saved successfully", fileName); - fclose(file); + int result = fclose(file); + if (result == 0) success = true; } else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName); } else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid"); + + return success; } #if defined(PLATFORM_ANDROID)