Change LoadFileToMemory's function signature

This commit is contained in:
Clownacy 2020-01-26 20:06:31 +00:00
parent cf8977207e
commit b650294b8b
4 changed files with 26 additions and 19 deletions

View file

@ -282,10 +282,10 @@ BOOL MakeSurface_File(const char *name, SurfaceID surf_no)
return FALSE; return FALSE;
} }
unsigned char *data; size_t size;
long size = LoadFileToMemory(path, &data); unsigned char *data = LoadFileToMemory(path, &size);
if (size == -1) if (data == NULL)
{ {
PrintBitmapError(path, 1); PrintBitmapError(path, 1);
return FALSE; return FALSE;
@ -373,10 +373,10 @@ BOOL ReloadBitmap_File(const char *name, SurfaceID surf_no)
return FALSE; return FALSE;
} }
unsigned char *data; size_t size;
long size = LoadFileToMemory(path, &data); unsigned char *data = LoadFileToMemory(path, &size);
if (size == -1) if (data == NULL)
{ {
PrintBitmapError(path, 1); PrintBitmapError(path, 1);
return FALSE; return FALSE;

View file

@ -4,9 +4,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.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"); 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)) 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); 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) if (fread(buffer, _file_size, 1, file) == 1)
returned_size = file_size; {
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); fclose(file);
} }
return returned_size; return NULL;
} }
unsigned short File_ReadBE16(FILE *stream) unsigned short File_ReadBE16(FILE *stream)

View file

@ -1,8 +1,9 @@
#pragma once #pragma once
#include <stddef.h>
#include <stdio.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 short File_ReadBE16(FILE *stream);
unsigned long File_ReadBE32(FILE *stream); unsigned long File_ReadBE32(FILE *stream);

View file

@ -1098,10 +1098,10 @@ FontObject* LoadFont(const char *font_filename, unsigned int cell_width, unsigne
{ {
FontObject *font_object = NULL; FontObject *font_object = NULL;
unsigned char *file_buffer; size_t file_size;
const long file_size = LoadFileToMemory(font_filename, &file_buffer); 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); font_object = LoadFontFromData(file_buffer, file_size, cell_width, cell_height);
free(file_buffer); free(file_buffer);