(retro_file) Create retro_fmemcpy_alloc

This commit is contained in:
twinaphex 2015-09-19 00:19:51 +02:00
parent 58e2f31073
commit a6e0b2f22d
4 changed files with 83 additions and 126 deletions

View File

@ -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,

View File

@ -20,19 +20,22 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <file/file_extract.h>
#include <file/file_path.h>
#include <compat/strl.h>
#include <retro_miscellaneous.h>
#include <string/string_list.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <compat/zlib.h>
#include <compat/strl.h>
#include <file/file_extract.h>
#include <file/file_path.h>
#include <retro_file.h>
#include <retro_miscellaneous.h>
#include <string/string_list.h>
/* 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)
{

View File

@ -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;
}

View File

@ -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