(file_ops) Convert some functions in file_ops over to retro_file

(retro_file.c) Fix retro_fseek and retro_ftell functions
This commit is contained in:
twinaphex 2015-09-18 05:22:50 +02:00
parent 432aac58a3
commit 611be4bc16
3 changed files with 43 additions and 13 deletions

View File

@ -30,6 +30,7 @@
#include <retro_assert.h>
#include <retro_miscellaneous.h>
#include <file/file_path.h>
#include <retro_file.h>
#include <string/string_list.h>
#ifdef HAVE_COMPRESSION
#include <file/file_extract.h>
@ -632,12 +633,13 @@ error:
bool write_file(const char *path, const void *data, ssize_t size)
{
ssize_t ret = 0;
FILE *file = fopen(path, "wb");
RFILE *file = retro_fopen(path, RFILE_MODE_WRITE, -1);
if (!file)
return false;
ret = fwrite(data, 1, size, file);
fclose(file);
ret = retro_fwrite(file, data, size);
retro_fclose(file);
return (ret == size);
}
@ -653,29 +655,32 @@ bool write_file(const char *path, const void *data, ssize_t size)
*/
static int read_generic_file(const char *path, void **buf, ssize_t *len)
{
ssize_t ret;
ssize_t bytes_read = 0;
ssize_t content_buf_size = 0;
void *content_buf = NULL;
FILE *file = fopen(path, "rb");
RFILE *file = retro_fopen(path, RFILE_MODE_READ, -1);
if (!file)
goto error;
if (fseek(file, 0, SEEK_END) != 0)
ret = retro_fseek(file, 0, SEEK_END);
if (ret != 0)
goto error;
content_buf_size = ftell(file);
content_buf_size = retro_ftell(file);
if (content_buf_size == -1)
goto error;
rewind(file);
retro_frewind(file);
content_buf = malloc(content_buf_size + 1);
if (!content_buf)
goto error;
if ((bytes_read = fread(content_buf, 1, content_buf_size, file)) < content_buf_size)
if ((bytes_read = retro_fread(file, content_buf, content_buf_size)) < content_buf_size)
RARCH_WARN("Didn't read whole file.\n");
*buf = content_buf;
@ -684,8 +689,7 @@ static int read_generic_file(const char *path, void **buf, ssize_t *len)
* Will only work with sane character formatting (Unix). */
((char*)content_buf)[content_buf_size] = '\0';
if (fclose(file) != 0)
RARCH_WARN("Failed to close file stream.\n");
retro_fclose(file);
if (len)
*len = bytes_read;
@ -693,8 +697,7 @@ static int read_generic_file(const char *path, void **buf, ssize_t *len)
return 1;
error:
if (file)
fclose(file);
retro_fclose(file);
if (content_buf)
free(content_buf);
if (len)

View File

@ -122,18 +122,41 @@ error:
ssize_t retro_fseek(RFILE *stream, ssize_t offset, int whence)
{
int ret = 0;
if (!stream)
return -1;
(void)ret;
#if defined(VITA) || defined(PSP)
return sceIoLseek(stream->fd, (SceOff)offset, whence);
#elif defined(HAVE_BUFFERED_IO)
return fseek(stream->fd, (long)offset, whence);
#else
return lseek(stream->fd, offset, whence);
ret = lseek(stream->fd, offset, whence);
if (ret == -1)
return -1;
return 0;
#endif
}
ssize_t retro_ftell(RFILE *stream)
{
int ret = 0;
if (!stream)
return -1;
#ifdef HAVE_BUFFERED_IO
return ftell(stream->fd);
#else
return lseek(stream->fd, 0, SEEK_CUR);
#endif
}
void retro_frewind(RFILE *stream)
{
retro_fseek(stream, 0L, SEEK_SET);
}
ssize_t retro_fread(RFILE *stream, void *s, size_t len)
{
if (!stream)

View File

@ -51,6 +51,10 @@ ssize_t retro_fread(RFILE *stream, void *s, size_t len);
ssize_t retro_fwrite(RFILE *stream, const void *s, size_t len);
ssize_t retro_ftell(RFILE *stream);
void retro_frewind(RFILE *stream);
void retro_fclose(RFILE *stream);
bool retro_fmemcpy(const char *path, char *s, size_t len, ssize_t *bytes_written);