From a6e0b2f22d203e99da6625b586edf216c54a97af Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sat, 19 Sep 2015 00:19:51 +0200 Subject: [PATCH] (retro_file) Create retro_fmemcpy_alloc --- file_ops.c | 67 +------------------------- libretro-common/file/file_extract.c | 68 ++++----------------------- libretro-common/file/retro_file.c | 70 +++++++++++++++++++++++++++- libretro-common/include/retro_file.h | 4 +- 4 files changed, 83 insertions(+), 126 deletions(-) diff --git a/file_ops.c b/file_ops.c index 56ec8a1430..9f16ef260c 100644 --- a/file_ops.c +++ b/file_ops.c @@ -644,69 +644,6 @@ bool write_file(const char *path, const void *data, ssize_t size) return (ret == size); } -/** - * read_generic_file: - * @path : path to file. - * @buf : buffer to allocate and read the contents of the - * file into. Needs to be freed manually. - * - * Read the contents of a file into @buf. - * - * Returns: number of items read, -1 on error. - */ -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; - RFILE *file = retro_fopen(path, RFILE_MODE_READ, -1); - - if (!file) - goto error; - - ret = retro_fseek(file, 0, SEEK_END); - - if (ret != 0) - goto error; - - content_buf_size = retro_ftell(file); - if (content_buf_size == -1) - goto error; - - retro_frewind(file); - - content_buf = malloc(content_buf_size + 1); - - if (!content_buf) - goto error; - - 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; - - /* Allow for easy reading of strings to be safe. - * Will only work with sane character formatting (Unix). */ - ((char*)content_buf)[content_buf_size] = '\0'; - - retro_fclose(file); - - if (len) - *len = bytes_read; - - return 1; - -error: - retro_fclose(file); - if (content_buf) - free(content_buf); - if (len) - *len = -1; - *buf = NULL; - return 0; -} - #ifdef HAVE_COMPRESSION /* Generic compressed file loader. * Extracts to buf, unless optional_filename != 0 @@ -787,7 +724,7 @@ int read_compressed_file(const char * path, void **buf, * @length : Number of items read, -1 on error. * * Read the contents of a file into @buf. Will call read_compressed_file - * if path contains a compressed file, otherwise will call read_generic_file. + * if path contains a compressed file, otherwise will call retro_fmemcpy_alloc. * * Returns: 1 if file read, 0 on error. */ @@ -800,7 +737,7 @@ int read_file(const char *path, void **buf, ssize_t *length) return 1; } #endif - return read_generic_file(path, buf, length); + return retro_fmemcpy_alloc(path, buf, length); } struct string_list *compressed_file_list_new(const char *path, diff --git a/libretro-common/file/file_extract.c b/libretro-common/file/file_extract.c index 173af8a2da..ab53cdee49 100644 --- a/libretro-common/file/file_extract.c +++ b/libretro-common/file/file_extract.c @@ -20,19 +20,22 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include -#include -#include -#include #include #include #include + #include +#include + +#include +#include +#include +#include +#include /* File backends. Can be fleshed out later, but keep it simple for now. * The file is mapped to memory directly (via mmap() or just - * plain zlib_read_file()). + * plain retro_fmemcpy_alloc()). */ struct zlib_file_backend @@ -155,57 +158,6 @@ typedef struct static int zlib_read_file(const char *path, void **buf, ssize_t *len) { - long ret = 0; - ssize_t content_buf_size = 0; - void *content_buf = NULL; - FILE *file = fopen(path, "rb"); - - if (!file) - goto error; - - if (fseek(file, 0, SEEK_END) != 0) - goto error; - - content_buf_size = ftell(file); - if (content_buf_size < 0) - goto error; - - rewind(file); - - content_buf = malloc(content_buf_size + 1); - - if (!content_buf) - goto error; - - if ((ret = fread(content_buf, 1, content_buf_size, file)) < content_buf_size) - printf("Didn't read whole file.\n"); - - if (!content_buf) - goto error; - - *buf = content_buf; - - /* Allow for easy reading of strings to be safe. - * Will only work with sane character formatting (Unix). */ - ((char*)content_buf)[content_buf_size] = '\0'; - - if (fclose(file) != 0) - printf("Failed to close file stream.\n"); - - if (len) - *len = ret; - - return 1; - -error: - if (file) - fclose(file); - if (content_buf) - free(content_buf); - if (len) - *len = -1; - *buf = NULL; - return 0; } static void zlib_file_free(void *handle) @@ -242,7 +194,7 @@ static void *zlib_file_open(const char *path) if (!data) return NULL; - read_from_file = zlib_read_file(path, &data->data, &ret); + read_from_file = retro_fmemcpy_alloc(path, &data->data, &ret); if (!read_from_file || ret < 0) { diff --git a/libretro-common/file/retro_file.c b/libretro-common/file/retro_file.c index 8fc1c5931e..0b82a54d10 100644 --- a/libretro-common/file/retro_file.c +++ b/libretro-common/file/retro_file.c @@ -207,10 +207,10 @@ ssize_t retro_fwrite(RFILE *stream, const void *s, size_t len) #endif } -void retro_fclose(RFILE *stream) +int retro_fclose(RFILE *stream) { if (!stream) - return; + return -1; #if defined(VITA) || defined(PSP) if (stream->fd > 0) @@ -223,6 +223,8 @@ void retro_fclose(RFILE *stream) close(stream->fd); #endif free(stream); + + return 0; } static bool retro_fread_iterate(RFILE *stream, char *s, size_t len, ssize_t *bytes_written) @@ -252,3 +254,67 @@ bool retro_fmemcpy(const char *path, char *s, size_t len, ssize_t *bytes_written return true; } + +/** + * retro_fmemcpy_alloc: + * @path : path to file. + * @buf : buffer to allocate and read the contents of the + * file into. Needs to be freed manually. + * + * Read the contents of a file into @buf. + * + * Returns: number of items read, -1 on error. + */ +int retro_fmemcpy_alloc(const char *path, void **buf, ssize_t *len) +{ + ssize_t ret = 0; + ssize_t content_buf_size = 0; + void *content_buf = NULL; + RFILE *file = retro_fopen(path, RFILE_MODE_READ, -1); + + if (!file) + goto error; + + if (retro_fseek(file, 0, SEEK_END) != 0) + goto error; + + content_buf_size = retro_ftell(file); + if (content_buf_size < 0) + goto error; + + retro_frewind(file); + + content_buf = malloc(content_buf_size + 1); + + if (!content_buf) + goto error; + + if ((ret = retro_fread(file, content_buf, content_buf_size)) < content_buf_size) + printf("Didn't read whole file.\n"); + + if (!content_buf) + goto error; + + *buf = content_buf; + + /* Allow for easy reading of strings to be safe. + * Will only work with sane character formatting (Unix). */ + ((char*)content_buf)[content_buf_size] = '\0'; + + if (retro_fclose(file) != 0) + printf("Failed to close file stream.\n"); + + if (len) + *len = ret; + + return 1; + +error: + retro_fclose(file); + if (content_buf) + free(content_buf); + if (len) + *len = -1; + *buf = NULL; + return 0; +} diff --git a/libretro-common/include/retro_file.h b/libretro-common/include/retro_file.h index ef4da816f4..1bc83e501a 100644 --- a/libretro-common/include/retro_file.h +++ b/libretro-common/include/retro_file.h @@ -55,10 +55,12 @@ ssize_t retro_ftell(RFILE *stream); void retro_frewind(RFILE *stream); -void retro_fclose(RFILE *stream); +int retro_fclose(RFILE *stream); bool retro_fmemcpy(const char *path, char *s, size_t len, ssize_t *bytes_written); +int retro_fmemcpy_alloc(const char *path, void **buf, ssize_t *len); + int retro_get_fd(RFILE *stream); #ifdef __cplusplus