From 611be4bc165a3c7045225ee56e5bd6c622885785 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 18 Sep 2015 05:22:50 +0200 Subject: [PATCH] (file_ops) Convert some functions in file_ops over to retro_file (retro_file.c) Fix retro_fseek and retro_ftell functions --- file_ops.c | 27 +++++++++++++++------------ libretro-common/file/retro_file.c | 25 ++++++++++++++++++++++++- libretro-common/include/retro_file.h | 4 ++++ 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/file_ops.c b/file_ops.c index 3efacff3f9..1675e50c52 100644 --- a/file_ops.c +++ b/file_ops.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #ifdef HAVE_COMPRESSION #include @@ -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) diff --git a/libretro-common/file/retro_file.c b/libretro-common/file/retro_file.c index a335a052f5..e9f733f5ad 100644 --- a/libretro-common/file/retro_file.c +++ b/libretro-common/file/retro_file.c @@ -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) diff --git a/libretro-common/include/retro_file.h b/libretro-common/include/retro_file.h index 81e0b22a8f..0888071428 100644 --- a/libretro-common/include/retro_file.h +++ b/libretro-common/include/retro_file.h @@ -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);