2017-01-22 13:58:20 +01:00
|
|
|
/* Copyright (C) 2010-2017 The RetroArch team
|
2015-09-22 12:52:15 +02:00
|
|
|
*
|
|
|
|
* ---------------------------------------------------------------------------------------
|
2016-03-20 16:29:14 +01:00
|
|
|
* The following license statement only applies to this file (file_stream.c).
|
2015-09-22 12:52:15 +02:00
|
|
|
* ---------------------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge,
|
|
|
|
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
|
|
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2015-09-17 11:41:48 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-12-15 00:31:37 +01:00
|
|
|
#include <stdarg.h>
|
2015-09-17 20:24:49 +02:00
|
|
|
#include <errno.h>
|
2015-09-17 11:41:48 +02:00
|
|
|
|
2017-11-25 06:17:19 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2017-12-14 14:03:42 +01:00
|
|
|
#include <streams/file_stream.h>
|
2017-12-15 00:31:37 +01:00
|
|
|
#include <vfs/vfs_implementation.h>
|
2015-09-22 01:45:16 +02:00
|
|
|
|
2017-12-14 00:01:58 +01:00
|
|
|
static const int64_t vfs_error_return_value = -1;
|
|
|
|
|
2017-12-15 17:26:28 +01:00
|
|
|
static retro_vfs_get_path_t filestream_get_path_cb = NULL;
|
|
|
|
static retro_vfs_open_t filestream_open_cb = NULL;
|
|
|
|
static retro_vfs_close_t filestream_close_cb = NULL;
|
|
|
|
static retro_vfs_size_t filestream_size_cb = NULL;
|
|
|
|
static retro_vfs_tell_t filestream_tell_cb = NULL;
|
|
|
|
static retro_vfs_seek_t filestream_seek_cb = NULL;
|
|
|
|
static retro_vfs_read_t filestream_read_cb = NULL;
|
|
|
|
static retro_vfs_write_t filestream_write_cb = NULL;
|
|
|
|
static retro_vfs_flush_t filestream_flush_cb = NULL;
|
|
|
|
static retro_vfs_remove_t filestream_remove_cb = NULL;
|
|
|
|
static retro_vfs_rename_t filestream_rename_cb = NULL;
|
2017-12-11 21:24:14 +01:00
|
|
|
|
2017-12-14 00:01:58 +01:00
|
|
|
struct RFILE
|
|
|
|
{
|
2017-12-14 14:03:42 +01:00
|
|
|
struct retro_vfs_file_handle *hfile;
|
|
|
|
bool error_flag;
|
2015-09-17 20:24:49 +02:00
|
|
|
};
|
2015-09-17 11:41:48 +02:00
|
|
|
|
2017-12-11 21:24:14 +01:00
|
|
|
/* VFS Initialization */
|
|
|
|
|
|
|
|
void filestream_vfs_init(const struct retro_vfs_interface_info* vfs_info)
|
|
|
|
{
|
|
|
|
const struct retro_vfs_interface* vfs_iface;
|
|
|
|
|
|
|
|
filestream_get_path_cb = NULL;
|
|
|
|
filestream_open_cb = NULL;
|
|
|
|
filestream_close_cb = NULL;
|
|
|
|
filestream_tell_cb = NULL;
|
|
|
|
filestream_size_cb = NULL;
|
|
|
|
filestream_seek_cb = NULL;
|
|
|
|
filestream_read_cb = NULL;
|
|
|
|
filestream_write_cb = NULL;
|
|
|
|
filestream_flush_cb = NULL;
|
2017-12-15 17:26:28 +01:00
|
|
|
filestream_remove_cb = NULL;
|
2017-12-14 13:29:36 -05:00
|
|
|
filestream_rename_cb = NULL;
|
2017-12-11 21:24:14 +01:00
|
|
|
|
|
|
|
vfs_iface = vfs_info->iface;
|
|
|
|
|
2017-12-15 17:26:28 +01:00
|
|
|
if (vfs_info->required_interface_version < FILESTREAM_REQUIRED_VFS_VERSION
|
|
|
|
|| !vfs_iface)
|
2017-12-11 21:24:14 +01:00
|
|
|
return;
|
|
|
|
|
2017-12-15 17:26:28 +01:00
|
|
|
filestream_get_path_cb = vfs_iface->get_path;
|
|
|
|
filestream_open_cb = vfs_iface->open;
|
|
|
|
filestream_close_cb = vfs_iface->close;
|
|
|
|
filestream_size_cb = vfs_iface->size;
|
|
|
|
filestream_tell_cb = vfs_iface->tell;
|
|
|
|
filestream_seek_cb = vfs_iface->seek;
|
|
|
|
filestream_read_cb = vfs_iface->read;
|
|
|
|
filestream_write_cb = vfs_iface->write;
|
|
|
|
filestream_flush_cb = vfs_iface->flush;
|
|
|
|
filestream_remove_cb = vfs_iface->remove;
|
|
|
|
filestream_rename_cb = vfs_iface->rename;
|
2017-12-11 21:24:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback wrappers */
|
2017-12-14 20:05:46 +01:00
|
|
|
bool filestream_exists(const char *path)
|
|
|
|
{
|
|
|
|
RFILE *dummy = NULL;
|
|
|
|
|
|
|
|
if (!path || !*path)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
dummy = filestream_open(path,
|
|
|
|
RETRO_VFS_FILE_ACCESS_READ,
|
|
|
|
RETRO_VFS_FILE_ACCESS_HINT_NONE);
|
|
|
|
|
|
|
|
if (!dummy)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
filestream_close(dummy);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:01:58 +01:00
|
|
|
int64_t filestream_get_size(RFILE *stream)
|
|
|
|
{
|
2017-12-14 14:03:42 +01:00
|
|
|
int64_t output;
|
|
|
|
|
|
|
|
if (filestream_size_cb != NULL)
|
|
|
|
output = filestream_size_cb(stream->hfile);
|
|
|
|
else
|
2017-12-14 17:27:02 +01:00
|
|
|
output = retro_vfs_file_size_impl((libretro_vfs_implementation_file*)stream->hfile);
|
2017-12-14 00:01:58 +01:00
|
|
|
|
|
|
|
if (output == vfs_error_return_value)
|
|
|
|
stream->error_flag = true;
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-11-17 16:12:36 -05:00
|
|
|
/**
|
|
|
|
* filestream_open:
|
|
|
|
* @path : path to file
|
|
|
|
* @mode : file mode to use when opening (read/write)
|
2017-12-11 19:55:42 +01:00
|
|
|
* @hints :
|
2017-11-17 16:12:36 -05:00
|
|
|
*
|
|
|
|
* Opens a file for reading or writing, depending on the requested mode.
|
|
|
|
* Returns a pointer to an RFILE if opened successfully, otherwise NULL.
|
|
|
|
**/
|
2017-12-10 22:25:38 +01:00
|
|
|
RFILE *filestream_open(const char *path, unsigned mode, unsigned hints)
|
2015-09-17 11:41:48 +02:00
|
|
|
{
|
2017-12-14 14:03:42 +01:00
|
|
|
struct retro_vfs_file_handle *fp = NULL;
|
|
|
|
RFILE* output = NULL;
|
|
|
|
|
|
|
|
if (filestream_open_cb != NULL)
|
2017-12-14 15:41:23 +01:00
|
|
|
fp = (struct retro_vfs_file_handle*)
|
|
|
|
filestream_open_cb(path, mode, hints);
|
2017-12-14 14:03:42 +01:00
|
|
|
else
|
|
|
|
fp = (struct retro_vfs_file_handle*)
|
|
|
|
retro_vfs_file_open_impl(path, mode, hints);
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
output = (RFILE*)malloc(sizeof(RFILE));
|
|
|
|
output->error_flag = false;
|
|
|
|
output->hfile = fp;
|
|
|
|
return output;
|
2015-09-17 11:41:48 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 00:03:58 +02:00
|
|
|
char *filestream_gets(RFILE *stream, char *s, size_t len)
|
|
|
|
{
|
2017-12-13 19:19:49 +01:00
|
|
|
int c = 0;
|
|
|
|
char *p = NULL;
|
2016-06-03 00:03:58 +02:00
|
|
|
if (!stream)
|
|
|
|
return NULL;
|
2017-12-13 19:19:49 +01:00
|
|
|
|
|
|
|
/* get max bytes or up to a newline */
|
|
|
|
|
|
|
|
for (p = s, len--; len > 0; len--)
|
|
|
|
{
|
|
|
|
if ((c = filestream_getc(stream)) == EOF)
|
|
|
|
break;
|
|
|
|
*p++ = c;
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
if (p == s || c == EOF)
|
|
|
|
return NULL;
|
|
|
|
return (p);
|
2016-06-03 00:03:58 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 00:07:00 +02:00
|
|
|
int filestream_getc(RFILE *stream)
|
|
|
|
{
|
2017-12-11 21:49:51 +01:00
|
|
|
char c = 0;
|
2016-06-03 00:07:00 +02:00
|
|
|
if (!stream)
|
|
|
|
return 0;
|
2017-12-11 21:49:51 +01:00
|
|
|
if(filestream_read(stream, &c, 1) == 1)
|
|
|
|
return (int)c;
|
2017-12-12 00:24:46 -08:00
|
|
|
return EOF;
|
2016-06-03 00:07:00 +02:00
|
|
|
}
|
|
|
|
|
2016-03-24 04:23:17 +01:00
|
|
|
ssize_t filestream_seek(RFILE *stream, ssize_t offset, int whence)
|
2015-09-17 11:41:48 +02:00
|
|
|
{
|
2017-12-14 14:03:42 +01:00
|
|
|
int64_t output;
|
|
|
|
|
|
|
|
if (filestream_seek_cb != NULL)
|
|
|
|
output = filestream_seek_cb(stream->hfile, offset, whence);
|
|
|
|
else
|
2017-12-14 17:27:02 +01:00
|
|
|
output = retro_vfs_file_seek_impl((libretro_vfs_implementation_file*)stream->hfile, offset, whence);
|
2016-04-07 04:16:46 +02:00
|
|
|
|
2017-12-14 00:01:58 +01:00
|
|
|
if (output == vfs_error_return_value)
|
|
|
|
stream->error_flag = true;
|
2016-04-07 04:16:46 +02:00
|
|
|
|
2017-12-14 00:01:58 +01:00
|
|
|
return output;
|
2015-09-17 11:41:48 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 07:29:27 +02:00
|
|
|
int filestream_eof(RFILE *stream)
|
|
|
|
{
|
2017-12-11 17:36:20 +01:00
|
|
|
int64_t current_position = filestream_tell(stream);
|
|
|
|
int64_t end_position = filestream_get_size(stream);
|
2016-06-03 07:29:27 +02:00
|
|
|
|
|
|
|
if (current_position >= end_position)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:01:58 +01:00
|
|
|
|
2016-03-24 04:23:17 +01:00
|
|
|
ssize_t filestream_tell(RFILE *stream)
|
2015-09-18 05:22:50 +02:00
|
|
|
{
|
2017-12-14 14:03:42 +01:00
|
|
|
ssize_t output;
|
|
|
|
|
|
|
|
if (filestream_size_cb != NULL)
|
|
|
|
output = filestream_tell_cb(stream->hfile);
|
|
|
|
else
|
2017-12-14 17:27:02 +01:00
|
|
|
output = retro_vfs_file_tell_impl((libretro_vfs_implementation_file*)stream->hfile);
|
2016-04-06 16:55:17 +02:00
|
|
|
|
2017-12-14 00:01:58 +01:00
|
|
|
if (output == vfs_error_return_value)
|
|
|
|
stream->error_flag = true;
|
2016-04-07 04:20:09 +02:00
|
|
|
|
2017-12-14 00:01:58 +01:00
|
|
|
return output;
|
2015-09-18 05:22:50 +02:00
|
|
|
}
|
|
|
|
|
2016-03-24 04:23:17 +01:00
|
|
|
void filestream_rewind(RFILE *stream)
|
2015-09-18 05:22:50 +02:00
|
|
|
{
|
2017-12-14 00:01:58 +01:00
|
|
|
if (!stream)
|
|
|
|
return;
|
2016-03-24 04:23:17 +01:00
|
|
|
filestream_seek(stream, 0L, SEEK_SET);
|
2017-12-14 00:01:58 +01:00
|
|
|
stream->error_flag = false;
|
2015-09-18 05:22:50 +02:00
|
|
|
}
|
|
|
|
|
2017-12-15 21:10:23 +01:00
|
|
|
ssize_t filestream_read(RFILE *stream, void *s, int64_t len)
|
2017-12-14 00:01:58 +01:00
|
|
|
{
|
2017-12-14 14:03:42 +01:00
|
|
|
int64_t output;
|
|
|
|
|
|
|
|
if (filestream_read_cb != NULL)
|
|
|
|
output = filestream_read_cb(stream->hfile, s, len);
|
|
|
|
else
|
2017-12-15 21:10:23 +01:00
|
|
|
output = retro_vfs_file_read_impl(
|
|
|
|
(libretro_vfs_implementation_file*)stream->hfile, s, len);
|
2017-12-14 00:01:58 +01:00
|
|
|
|
|
|
|
if (output == vfs_error_return_value)
|
|
|
|
stream->error_flag = true;
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
int filestream_flush(RFILE *stream)
|
|
|
|
{
|
2017-12-14 14:03:42 +01:00
|
|
|
int output;
|
|
|
|
|
|
|
|
if (filestream_flush_cb != NULL)
|
|
|
|
output = filestream_flush_cb(stream->hfile);
|
|
|
|
else
|
2017-12-14 17:27:02 +01:00
|
|
|
output = retro_vfs_file_flush_impl((libretro_vfs_implementation_file*)stream->hfile);
|
2017-12-14 00:01:58 +01:00
|
|
|
|
|
|
|
if (output == vfs_error_return_value)
|
|
|
|
stream->error_flag = true;
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:16:18 +01:00
|
|
|
int filestream_delete(const char *path)
|
|
|
|
{
|
2017-12-15 17:26:28 +01:00
|
|
|
if (filestream_remove_cb != NULL)
|
|
|
|
return filestream_remove_cb(path);
|
2017-12-14 14:03:42 +01:00
|
|
|
|
2017-12-15 17:26:28 +01:00
|
|
|
return retro_vfs_file_remove_impl(path);
|
2017-12-14 00:16:18 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 13:29:36 -05:00
|
|
|
int filestream_rename(const char *old_path, const char *new_path)
|
|
|
|
{
|
|
|
|
if (filestream_rename_cb != NULL)
|
|
|
|
return filestream_rename_cb(old_path, new_path);
|
|
|
|
|
|
|
|
return retro_vfs_file_rename_impl(old_path, new_path);
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:16:18 +01:00
|
|
|
const char *filestream_get_path(RFILE *stream)
|
|
|
|
{
|
2017-12-14 14:05:55 +01:00
|
|
|
if (filestream_get_path_cb != NULL)
|
|
|
|
return filestream_get_path_cb(stream->hfile);
|
|
|
|
|
2017-12-14 17:27:02 +01:00
|
|
|
return retro_vfs_file_get_path_impl((libretro_vfs_implementation_file*)stream->hfile);
|
2017-12-14 00:16:18 +01:00
|
|
|
}
|
|
|
|
|
2017-12-15 21:10:23 +01:00
|
|
|
ssize_t filestream_write(RFILE *stream, const void *s, int64_t len)
|
2017-12-14 00:16:18 +01:00
|
|
|
{
|
2017-12-14 14:03:42 +01:00
|
|
|
int64_t output;
|
|
|
|
|
|
|
|
if (filestream_write_cb != NULL)
|
|
|
|
output = filestream_write_cb(stream->hfile, s, len);
|
|
|
|
else
|
2017-12-14 17:27:02 +01:00
|
|
|
output = retro_vfs_file_write_impl((libretro_vfs_implementation_file*)stream->hfile, s, len);
|
2017-12-14 00:16:18 +01:00
|
|
|
|
|
|
|
if (output == vfs_error_return_value)
|
|
|
|
stream->error_flag = true;
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2016-04-07 03:23:01 +02:00
|
|
|
int filestream_putc(RFILE *stream, int c)
|
|
|
|
{
|
2017-12-14 18:45:54 +01:00
|
|
|
char c_char = (char)c;
|
|
|
|
if (!stream)
|
|
|
|
return EOF;
|
|
|
|
return filestream_write(stream, &c_char, 1);
|
2016-04-07 03:23:01 +02:00
|
|
|
}
|
|
|
|
|
2017-09-06 23:17:00 +02:00
|
|
|
int filestream_vprintf(RFILE *stream, const char* format, va_list args)
|
|
|
|
{
|
|
|
|
static char buffer[8 * 1024];
|
2017-12-10 22:07:42 +01:00
|
|
|
int num_chars = vsprintf(buffer, format, args);
|
2017-09-06 23:17:00 +02:00
|
|
|
|
2017-12-10 22:07:42 +01:00
|
|
|
if (num_chars < 0)
|
2017-09-06 23:17:00 +02:00
|
|
|
return -1;
|
2017-12-10 22:07:42 +01:00
|
|
|
else if (num_chars == 0)
|
2017-09-06 23:17:00 +02:00
|
|
|
return 0;
|
|
|
|
|
2017-12-10 22:10:32 +01:00
|
|
|
return filestream_write(stream, buffer, num_chars);
|
2017-09-06 23:17:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int filestream_printf(RFILE *stream, const char* format, ...)
|
|
|
|
{
|
|
|
|
va_list vl;
|
|
|
|
int result;
|
|
|
|
va_start(vl, format);
|
|
|
|
result = filestream_vprintf(stream, format, vl);
|
|
|
|
va_end(vl);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int filestream_error(RFILE *stream)
|
|
|
|
{
|
2017-12-14 00:16:18 +01:00
|
|
|
if (stream && stream->error_flag)
|
2017-12-14 00:01:58 +01:00
|
|
|
return 1;
|
|
|
|
return 0;
|
2017-09-06 23:17:00 +02:00
|
|
|
}
|
|
|
|
|
2016-03-24 04:23:17 +01:00
|
|
|
int filestream_close(RFILE *stream)
|
2015-09-17 11:41:48 +02:00
|
|
|
{
|
2017-12-14 14:03:42 +01:00
|
|
|
int output;
|
2017-12-14 17:27:02 +01:00
|
|
|
struct retro_vfs_file_handle* fp = stream->hfile;
|
2015-09-17 11:41:48 +02:00
|
|
|
|
2017-12-14 14:03:42 +01:00
|
|
|
if (filestream_close_cb != NULL)
|
|
|
|
output = filestream_close_cb(fp);
|
2015-11-14 07:17:21 -03:00
|
|
|
else
|
2017-12-14 17:27:02 +01:00
|
|
|
output = retro_vfs_file_close_impl((libretro_vfs_implementation_file*)fp);
|
2015-09-19 00:19:51 +02:00
|
|
|
|
2017-12-14 14:03:42 +01:00
|
|
|
if (output == 0)
|
|
|
|
free(stream);
|
2016-04-06 16:55:17 +02:00
|
|
|
|
2017-12-14 14:03:42 +01:00
|
|
|
return output;
|
2015-09-17 11:41:48 +02:00
|
|
|
}
|
2015-09-17 20:24:49 +02:00
|
|
|
|
2015-09-19 00:19:51 +02:00
|
|
|
/**
|
2016-03-24 04:09:25 +01:00
|
|
|
* filestream_read_file:
|
2015-09-19 00:19:51 +02:00
|
|
|
* @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.
|
|
|
|
*/
|
2016-03-24 04:09:25 +01:00
|
|
|
int filestream_read_file(const char *path, void **buf, ssize_t *len)
|
2015-09-19 00:19:51 +02:00
|
|
|
{
|
|
|
|
ssize_t ret = 0;
|
2017-12-11 17:59:18 +01:00
|
|
|
int64_t content_buf_size = 0;
|
2015-09-19 00:19:51 +02:00
|
|
|
void *content_buf = NULL;
|
2017-12-10 22:25:38 +01:00
|
|
|
RFILE *file = filestream_open(path,
|
2017-12-11 12:53:47 +01:00
|
|
|
RETRO_VFS_FILE_ACCESS_READ,
|
|
|
|
RETRO_VFS_FILE_ACCESS_HINT_NONE);
|
2015-09-19 00:19:51 +02:00
|
|
|
|
|
|
|
if (!file)
|
2015-12-23 21:20:26 -03:00
|
|
|
{
|
2015-12-26 08:13:33 +01:00
|
|
|
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
|
2015-09-19 00:19:51 +02:00
|
|
|
goto error;
|
2015-12-23 21:20:26 -03:00
|
|
|
}
|
2015-09-19 00:19:51 +02:00
|
|
|
|
2017-12-11 17:59:18 +01:00
|
|
|
content_buf_size = filestream_get_size(file);
|
2015-09-19 00:19:51 +02:00
|
|
|
|
|
|
|
if (content_buf_size < 0)
|
|
|
|
goto error;
|
|
|
|
|
2017-12-15 21:10:23 +01:00
|
|
|
content_buf = malloc((size_t)(content_buf_size + 1));
|
2015-09-19 00:19:51 +02:00
|
|
|
|
|
|
|
if (!content_buf)
|
|
|
|
goto error;
|
|
|
|
|
2017-12-15 21:10:23 +01:00
|
|
|
ret = filestream_read(file, content_buf, (int64_t)content_buf_size);
|
2015-12-23 21:20:26 -03:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-12-26 08:13:33 +01:00
|
|
|
fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno));
|
2015-09-19 00:19:51 +02:00
|
|
|
goto error;
|
2015-12-23 21:20:26 -03:00
|
|
|
}
|
|
|
|
|
2016-03-24 04:23:17 +01:00
|
|
|
filestream_close(file);
|
2015-09-19 00:19:51 +02:00
|
|
|
|
|
|
|
*buf = content_buf;
|
|
|
|
|
|
|
|
/* Allow for easy reading of strings to be safe.
|
|
|
|
* Will only work with sane character formatting (Unix). */
|
2017-04-23 20:26:31 +02:00
|
|
|
((char*)content_buf)[ret] = '\0';
|
2015-09-19 00:19:51 +02:00
|
|
|
|
|
|
|
if (len)
|
|
|
|
*len = ret;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
error:
|
2015-12-23 21:20:26 -03:00
|
|
|
if (file)
|
2016-03-24 04:23:17 +01:00
|
|
|
filestream_close(file);
|
2015-09-19 00:19:51 +02:00
|
|
|
if (content_buf)
|
|
|
|
free(content_buf);
|
|
|
|
if (len)
|
|
|
|
*len = -1;
|
|
|
|
*buf = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-19 00:34:24 +02:00
|
|
|
|
|
|
|
/**
|
2016-03-24 04:09:25 +01:00
|
|
|
* filestream_write_file:
|
2015-09-19 00:34:24 +02:00
|
|
|
* @path : path to file.
|
|
|
|
* @data : contents to write to the file.
|
|
|
|
* @size : size of the contents.
|
|
|
|
*
|
|
|
|
* Writes data to a file.
|
|
|
|
*
|
|
|
|
* Returns: true (1) on success, false (0) otherwise.
|
|
|
|
*/
|
2016-03-24 04:09:25 +01:00
|
|
|
bool filestream_write_file(const char *path, const void *data, ssize_t size)
|
2015-09-19 00:34:24 +02:00
|
|
|
{
|
|
|
|
ssize_t ret = 0;
|
2017-12-11 12:15:00 +01:00
|
|
|
RFILE *file = filestream_open(path,
|
2017-12-11 12:53:47 +01:00
|
|
|
RETRO_VFS_FILE_ACCESS_WRITE,
|
|
|
|
RETRO_VFS_FILE_ACCESS_HINT_NONE);
|
2015-09-19 00:34:24 +02:00
|
|
|
if (!file)
|
|
|
|
return false;
|
|
|
|
|
2016-03-24 04:23:17 +01:00
|
|
|
ret = filestream_write(file, data, size);
|
|
|
|
filestream_close(file);
|
2015-09-19 00:34:24 +02:00
|
|
|
|
2016-04-06 16:55:17 +02:00
|
|
|
if (ret != size)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2015-09-19 00:34:24 +02:00
|
|
|
}
|
2017-12-15 00:31:37 +01:00
|
|
|
|
|
|
|
char *filestream_getline(RFILE *stream)
|
|
|
|
{
|
|
|
|
char* newline = (char*)malloc(9);
|
|
|
|
char* newline_tmp = NULL;
|
|
|
|
size_t cur_size = 8;
|
|
|
|
size_t idx = 0;
|
|
|
|
int in = filestream_getc(stream);
|
|
|
|
|
|
|
|
if (!newline)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (in != EOF && in != '\n')
|
|
|
|
{
|
|
|
|
if (idx == cur_size)
|
|
|
|
{
|
|
|
|
cur_size *= 2;
|
|
|
|
newline_tmp = (char*)realloc(newline, cur_size + 1);
|
|
|
|
|
|
|
|
if (!newline_tmp)
|
|
|
|
{
|
|
|
|
free(newline);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
newline = newline_tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
newline[idx++] = in;
|
|
|
|
in = filestream_getc(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
newline[idx] = '\0';
|
|
|
|
return newline;
|
|
|
|
}
|