瀏覽代碼

ADDED: LoadFileData(), SaveFileData()

Ray 5 年之前
父節點
當前提交
1046449339
共有 2 個文件被更改,包括 51 次插入1 次删除
  1. 2 0
      src/raylib.h
  2. 49 1
      src/utils.c

+ 2 - 0
src/raylib.h

@@ -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)
 
 // 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 IsFileExtension(const char *fileName, const char *ext);// Check file extension
 RLAPI bool DirectoryExists(const char *dirPath);                  // Check if a directory path exists

+ 49 - 1
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)