diff --git a/src/Draw.cpp b/src/Draw.cpp index caf728f5..79c23f38 100644 --- a/src/Draw.cpp +++ b/src/Draw.cpp @@ -282,10 +282,10 @@ BOOL MakeSurface_File(const char *name, SurfaceID surf_no) 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 @@ BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no) 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; diff --git a/src/File.cpp b/src/File.cpp index 257e5e7c..1ee45b56 100644 --- a/src/File.cpp +++ b/src/File.cpp @@ -4,9 +4,9 @@ #include #include -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 @@ long LoadFileToMemory(const char *file_path, unsigned char **file_buffer) { 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 @@ long LoadFileToMemory(const char *file_path, unsigned char **file_buffer) fclose(file); } - return returned_size; + return NULL; } unsigned short File_ReadBE16(FILE *stream) diff --git a/src/File.h b/src/File.h index d84dc370..59ac4f34 100644 --- a/src/File.h +++ b/src/File.h @@ -1,8 +1,9 @@ #pragma once +#include #include -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); diff --git a/src/Font.cpp b/src/Font.cpp index 641a7f69..68c2c456 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -1098,10 +1098,10 @@ FontObject* LoadFont(const char *font_filename, unsigned int cell_width, unsigne { 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);