Refactor read_generic_file
This commit is contained in:
parent
395aa9b15f
commit
b0f8ac993c
29
file_ops.c
29
file_ops.c
|
@ -97,9 +97,9 @@ bool write_file(const char *path, const void *data, size_t size)
|
||||||
*
|
*
|
||||||
* Returns: number of items read, -1 on error.
|
* Returns: number of items read, -1 on error.
|
||||||
*/
|
*/
|
||||||
static long read_generic_file(const char *path, void **buf)
|
static bool read_generic_file(const char *path, void **buf, size_t *len)
|
||||||
{
|
{
|
||||||
long rc = 0, len = 0;
|
long ret = 0, _len = 0;
|
||||||
void *rom_buf = NULL;
|
void *rom_buf = NULL;
|
||||||
|
|
||||||
FILE *file = fopen(path, "rb");
|
FILE *file = fopen(path, "rb");
|
||||||
|
@ -108,25 +108,33 @@ static long read_generic_file(const char *path, void **buf)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
fseek(file, 0, SEEK_END);
|
fseek(file, 0, SEEK_END);
|
||||||
len = ftell(file);
|
|
||||||
|
_len = ftell(file);
|
||||||
|
|
||||||
rewind(file);
|
rewind(file);
|
||||||
rom_buf = malloc(len + 1);
|
|
||||||
|
rom_buf = malloc(_len + 1);
|
||||||
|
|
||||||
if (!rom_buf)
|
if (!rom_buf)
|
||||||
{
|
{
|
||||||
RARCH_ERR("Couldn't allocate memory.\n");
|
RARCH_ERR("Couldn't allocate memory.\n");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((rc = fread(rom_buf, 1, len, file)) < len)
|
if ((ret = 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;
|
|
||||||
|
if (len)
|
||||||
|
*len = ret;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
if (file)
|
if (file)
|
||||||
|
@ -134,7 +142,7 @@ error:
|
||||||
if (rom_buf)
|
if (rom_buf)
|
||||||
free(rom_buf);
|
free(rom_buf);
|
||||||
*buf = NULL;
|
*buf = NULL;
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_COMPRESSION
|
#ifdef HAVE_COMPRESSION
|
||||||
|
@ -217,5 +225,8 @@ long read_file(const char *path, void **buf)
|
||||||
if (path_contains_compressed_file(path))
|
if (path_contains_compressed_file(path))
|
||||||
return read_compressed_file(path,buf,0);
|
return read_compressed_file(path,buf,0);
|
||||||
#endif
|
#endif
|
||||||
return read_generic_file(path,buf);
|
size_t length = 0;
|
||||||
|
if (!read_generic_file(path, buf, &length))
|
||||||
|
return 0;
|
||||||
|
return length;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue