shithub: cstory

Download patch

ref: b650294b8bc6ba298e15a5a5fa76cfaedd599a9d
parent: cf8977207eaf98c4d0674a51c67a368a463ce7c0
author: Clownacy <Clownacy@users.noreply.github.com>
date: Sun Jan 26 15:06:31 EST 2020

Change LoadFileToMemory's function signature

--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -282,10 +282,10 @@
 		return FALSE;
 	}
 
-	unsigned char *data;
-	long size = LoadFileToMemory(path, &data);
+	size_t size;
+	unsigned char *data = LoadFileToMemory(path, &size);
 
-	if (size == -1)
+	if (data == NULL)
 	{
 		PrintBitmapError(path, 1);
 		return FALSE;
@@ -373,10 +373,10 @@
 		return FALSE;
 	}
 
-	unsigned char *data;
-	long size = LoadFileToMemory(path, &data);
+	size_t size;
+	unsigned char *data = LoadFileToMemory(path, &size);
 
-	if (size == -1)
+	if (data == NULL)
 	{
 		PrintBitmapError(path, 1);
 		return FALSE;
--- a/src/File.cpp
+++ b/src/File.cpp
@@ -4,9 +4,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-long LoadFileToMemory(const char *file_path, unsigned char **file_buffer)
+unsigned char* LoadFileToMemory(const char *file_path, size_t *file_size)
 {
-	long returned_size = -1;
+	unsigned char *buffer = NULL;
 
 	FILE *file = fopen(file_path, "rb");
 
@@ -14,17 +14,23 @@
 	{
 		if (!fseek(file, 0, SEEK_END))
 		{
-			const long file_size = ftell(file);
+			const long _file_size = ftell(file);
 
-			if (file_size >= 0)
+			if (_file_size >= 0)
 			{
 				rewind(file);
-				*file_buffer = (unsigned char*)malloc(file_size);
+				buffer = (unsigned char*)malloc(_file_size);
 
-				if (*file_buffer != NULL)
+				if (buffer != NULL)
 				{
-					if (fread(*file_buffer, file_size, 1, file) == 1)
-						returned_size = file_size;
+					if (fread(buffer, _file_size, 1, file) == 1)
+					{
+						fclose(file);
+						*file_size = (size_t)_file_size;
+						return buffer;
+					}
+
+					free(buffer);
 				}
 			}
 		}
@@ -32,7 +38,7 @@
 		fclose(file);
 	}
 
-	return returned_size;
+	return NULL;
 }
 
 unsigned short File_ReadBE16(FILE *stream)
--- a/src/File.h
+++ b/src/File.h
@@ -1,8 +1,9 @@
 #pragma once
 
+#include <stddef.h>
 #include <stdio.h>
 
-long LoadFileToMemory(const char *file_path, unsigned char **file_buffer);
+unsigned char* LoadFileToMemory(const char *file_path, size_t *file_size);
 
 unsigned short File_ReadBE16(FILE *stream);
 unsigned long File_ReadBE32(FILE *stream);
--- a/src/Font.cpp
+++ b/src/Font.cpp
@@ -1098,10 +1098,10 @@
 {
 	FontObject *font_object = NULL;
 
-	unsigned char *file_buffer;
-	const long file_size = LoadFileToMemory(font_filename, &file_buffer);
+	size_t file_size;
+	unsigned char *file_buffer = LoadFileToMemory(font_filename, &file_size);
 
-	if (file_size != -1)
+	if (file_buffer != NULL)
 	{
 		font_object = LoadFontFromData(file_buffer, file_size, cell_width, cell_height);
 		free(file_buffer);
--