At least use long for return type of read_file().

int is not acceptable as a return type for anything regarding sizes.
long is dubious as well, but better (64-bit on sane ABIs and the return
type of ftell()).
This commit is contained in:
Themaister 2013-12-31 22:54:11 +01:00
parent a91c416720
commit e5bedc343a
3 changed files with 9 additions and 9 deletions

View File

@ -18,9 +18,7 @@
#include <string.h> #include <string.h>
#include "file_list.h" #include "file_list.h"
#include "compat/strcasestr.h" #include "compat/strcasestr.h"
#ifdef _MSC_VER
#include "msvc/msvc_compat.h" #include "msvc/msvc_compat.h"
#endif
struct item_file struct item_file
{ {

View File

@ -71,12 +71,12 @@ bool write_file(const char *path, const void *data, size_t size)
} }
// Generic file loader. // Generic file loader.
int read_file(const char *path, void **buf) long read_file(const char *path, void **buf)
{ {
long rc = 0, len = 0;
void *rom_buf = NULL; void *rom_buf = NULL;
FILE *file = fopen(path, "rb"); FILE *file = fopen(path, "rb");
ssize_t rc = 0;
size_t len = 0;
if (!file) if (!file)
goto error; goto error;
@ -90,13 +90,13 @@ int read_file(const char *path, void **buf)
goto error; goto error;
} }
if ((rc = fread(rom_buf, 1, len, file)) < (ssize_t)len) if ((rc = fread(rom_buf, 1, len, file)) < len)
RARCH_WARN("Didn't read whole file.\n"); RARCH_WARN("Didn't read whole file.\n");
*buf = rom_buf; *buf = rom_buf;
// Allow for easy reading of strings to be safe. // Allow for easy reading of strings to be safe.
// Will only work with sane character formatting (Unix). // Will only work with sane character formatting (Unix).
((char*)rom_buf)[len] = '\0'; ((char*)rom_buf)[len] = '\0';
fclose(file); fclose(file);
return rc; return rc;
@ -113,12 +113,14 @@ bool read_file_string(const char *path, char **buf)
{ {
*buf = NULL; *buf = NULL;
FILE *file = fopen(path, "r"); FILE *file = fopen(path, "r");
size_t len = 0; long len = 0;
char *ptr = NULL; char *ptr = NULL;
if (!file) if (!file)
goto error; goto error;
// ftell with "r" can be troublesome ...
// Haven't run into issues yet though.
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
len = ftell(file) + 2; // Takes account of being able to read in EOF and '\0' at end. len = ftell(file) + 2; // Takes account of being able to read in EOF and '\0' at end.
rewind(file); rewind(file);

View File

@ -26,7 +26,7 @@
extern "C" { extern "C" {
#endif #endif
int read_file(const char *path, void **buf); long read_file(const char *path, void **buf);
bool read_file_string(const char *path, char **buf); bool read_file_string(const char *path, char **buf);
bool write_file(const char *path, const void *buf, size_t size); bool write_file(const char *path, const void *buf, size_t size);