beetle-pce-fast-libretro/libretro-common/streams/file_stream.c

660 lines
16 KiB
C
Raw Permalink Normal View History

2020-10-14 04:17:21 +00:00
/* Copyright (C) 2010-2020 The RetroArch team
2016-03-24 05:33:52 +00:00
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (file_stream.c).
* ---------------------------------------------------------------------------------------
*
* 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2017-12-16 02:07:16 +00:00
#include <stdarg.h>
2018-07-20 11:48:35 +00:00
#include <ctype.h>
2016-03-24 05:33:52 +00:00
2017-12-16 02:07:16 +00:00
#ifdef HAVE_CONFIG_H
#include "config.h"
2016-03-24 05:33:52 +00:00
#endif
2020-10-14 04:17:21 +00:00
#ifdef _MSC_VER
#include <compat/msvc.h>
#endif
#include <string/stdstring.h>
2016-03-24 05:33:52 +00:00
#include <streams/file_stream.h>
2018-03-24 12:40:06 +00:00
#define VFS_FRONTEND
2017-12-16 02:07:16 +00:00
#include <vfs/vfs_implementation.h>
2020-10-14 04:17:21 +00:00
#define VFS_ERROR_RETURN_VALUE -1
struct RFILE
{
struct retro_vfs_file_handle *hfile;
bool error_flag;
};
2017-12-16 02:07:16 +00: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;
2019-07-26 02:01:27 +00:00
static retro_vfs_truncate_t filestream_truncate_cb = NULL;
2017-12-16 02:07:16 +00:00
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;
2016-03-24 05:33:52 +00:00
2017-12-16 02:07:16 +00:00
/* VFS Initialization */
2017-04-23 18:37:59 +00:00
2017-12-16 02:07:16 +00:00
void filestream_vfs_init(const struct retro_vfs_interface_info* vfs_info)
2017-04-23 18:37:59 +00:00
{
2020-10-14 04:17:21 +00:00
const struct retro_vfs_interface *
vfs_iface = vfs_info->iface;
2018-01-05 12:23:27 +00:00
filestream_get_path_cb = NULL;
filestream_open_cb = NULL;
filestream_close_cb = NULL;
filestream_tell_cb = NULL;
filestream_size_cb = NULL;
2019-07-26 02:01:27 +00:00
filestream_truncate_cb = NULL;
2018-01-05 12:23:27 +00:00
filestream_seek_cb = NULL;
filestream_read_cb = NULL;
filestream_write_cb = NULL;
filestream_flush_cb = NULL;
filestream_remove_cb = NULL;
filestream_rename_cb = NULL;
2020-10-14 04:17:21 +00:00
if (
(vfs_info->required_interface_version <
FILESTREAM_REQUIRED_VFS_VERSION)
2018-01-05 12:23:27 +00:00
|| !vfs_iface)
return;
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;
2019-07-26 02:01:27 +00:00
filestream_truncate_cb = vfs_iface->truncate;
2018-01-05 12:23:27 +00:00
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-04-23 18:37:59 +00:00
}
2017-12-16 02:07:16 +00:00
/* Callback wrappers */
bool filestream_exists(const char *path)
2017-04-23 18:37:59 +00:00
{
2020-10-14 04:17:21 +00:00
RFILE *dummy = NULL;
2017-12-16 02:07:16 +00:00
if (!path || !*path)
return false;
if (!(dummy = filestream_open(
2020-10-14 04:17:21 +00:00
path,
2017-12-16 02:07:16 +00:00
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE)))
2017-12-16 02:07:16 +00:00
return false;
2020-10-14 04:17:21 +00:00
if (filestream_close(dummy) != 0)
free(dummy);
2020-10-14 04:17:21 +00:00
dummy = NULL;
2017-12-16 02:07:16 +00:00
return true;
2017-04-23 18:37:59 +00:00
}
2018-04-13 00:04:19 +00:00
int64_t filestream_get_size(RFILE *stream)
2017-04-23 18:37:59 +00:00
{
2018-04-13 00:04:19 +00:00
int64_t output;
2017-04-23 18:37:59 +00:00
2020-10-14 04:17:21 +00:00
if (filestream_size_cb)
2017-12-16 02:07:16 +00:00
output = filestream_size_cb(stream->hfile);
else
2020-10-14 04:17:21 +00:00
output = retro_vfs_file_size_impl(
(libretro_vfs_implementation_file*)stream->hfile);
2017-04-23 18:37:59 +00:00
2020-10-14 04:17:21 +00:00
if (output == VFS_ERROR_RETURN_VALUE)
2017-12-16 02:07:16 +00:00
stream->error_flag = true;
2017-04-23 18:37:59 +00:00
2017-12-16 02:07:16 +00:00
return output;
2016-03-24 05:33:52 +00:00
}
2019-07-26 02:01:27 +00:00
int64_t filestream_truncate(RFILE *stream, int64_t length)
{
int64_t output;
2020-10-14 04:17:21 +00:00
if (filestream_truncate_cb)
2019-07-26 02:01:27 +00:00
output = filestream_truncate_cb(stream->hfile, length);
else
2020-10-14 04:17:21 +00:00
output = retro_vfs_file_truncate_impl(
(libretro_vfs_implementation_file*)stream->hfile, length);
2019-07-26 02:01:27 +00:00
2020-10-14 04:17:21 +00:00
if (output == VFS_ERROR_RETURN_VALUE)
2019-07-26 02:01:27 +00:00
stream->error_flag = true;
return output;
}
2017-12-16 02:07:16 +00:00
/**
* filestream_open:
* @path : path to file
* @mode : file mode to use when opening (read/write)
* @hints :
*
* Opens a file for reading or writing, depending on the requested mode.
* @return A pointer to an RFILE if opened successfully, otherwise NULL.
2017-12-16 02:07:16 +00:00
**/
2019-12-28 01:06:37 +00:00
RFILE* filestream_open(const char *path, unsigned mode, unsigned hints)
2016-03-24 05:33:52 +00:00
{
2017-12-16 02:07:16 +00:00
struct retro_vfs_file_handle *fp = NULL;
2018-01-05 12:23:27 +00:00
RFILE* output = NULL;
2017-12-16 02:07:16 +00:00
2020-10-14 04:17:21 +00:00
if (filestream_open_cb)
2018-01-05 12:23:27 +00:00
fp = (struct retro_vfs_file_handle*)
2017-12-16 02:07:16 +00:00
filestream_open_cb(path, mode, hints);
2018-01-05 12:23:27 +00:00
else
fp = (struct retro_vfs_file_handle*)
2017-12-16 02:07:16 +00:00
retro_vfs_file_open_impl(path, mode, hints);
2018-01-05 12:23:27 +00:00
if (!fp)
return NULL;
2017-12-16 02:07:16 +00:00
2018-01-05 12:23:27 +00:00
output = (RFILE*)malloc(sizeof(RFILE));
output->error_flag = false;
output->hfile = fp;
return output;
2017-12-16 02:07:16 +00:00
}
2016-03-24 05:33:52 +00:00
2019-12-28 01:06:37 +00:00
char* filestream_gets(RFILE *stream, char *s, size_t len)
2017-12-16 02:07:16 +00:00
{
int c = 0;
2017-12-31 01:35:36 +00:00
char *p = s;
2016-03-24 05:33:52 +00:00
if (!stream)
return NULL;
2017-12-16 02:07:16 +00:00
/* get max bytes or up to a newline */
2016-03-24 05:33:52 +00:00
2017-12-31 01:35:36 +00:00
for (len--; len > 0; len--)
2016-03-24 05:33:52 +00:00
{
2017-12-16 02:07:16 +00:00
if ((c = filestream_getc(stream)) == EOF)
2016-03-24 05:33:52 +00:00
break;
2017-12-16 02:07:16 +00:00
*p++ = c;
if (c == '\n')
2016-03-24 05:33:52 +00:00
break;
}
2017-12-16 02:07:16 +00:00
*p = 0;
2016-03-24 05:33:52 +00:00
2017-12-31 01:35:36 +00:00
if (p == s && c == EOF)
2017-12-16 02:07:16 +00:00
return NULL;
2017-12-31 01:35:36 +00:00
return (s);
2017-12-16 02:07:16 +00:00
}
2016-03-24 05:33:52 +00:00
2017-12-16 02:07:16 +00:00
int filestream_getc(RFILE *stream)
{
char c = 0;
2020-10-14 04:17:21 +00:00
if (stream && filestream_read(stream, &c, 1) == 1)
2019-07-26 02:01:27 +00:00
return (int)(unsigned char)c;
2017-12-16 02:07:16 +00:00
return EOF;
}
2016-03-24 05:33:52 +00:00
int filestream_vscanf(RFILE *stream, const char* format, va_list *args)
2018-07-20 11:48:35 +00:00
{
char buf[4096];
char subfmt[64];
va_list args_copy;
const char *bufiter = buf;
2018-07-20 11:48:35 +00:00
int ret = 0;
2020-10-14 04:17:21 +00:00
int64_t startpos = filestream_tell(stream);
2018-07-20 11:48:35 +00:00
int64_t maxlen = filestream_read(stream, buf, sizeof(buf)-1);
2019-07-26 02:01:27 +00:00
if (maxlen <= 0)
return EOF;
2018-07-20 11:48:35 +00:00
buf[maxlen] = '\0';
2019-07-26 02:01:27 +00:00
/* Have to copy the input va_list here
* > Calling va_arg() on 'args' directly would
* cause the va_list to have an indeterminate value
* in the function calling filestream_vscanf(),
* leading to unexpected behaviour */
#ifdef __va_copy
__va_copy(args_copy, *args);
#else
va_copy(args_copy, *args);
#endif
2019-07-26 02:01:27 +00:00
2018-07-20 11:48:35 +00:00
while (*format)
{
if (*format == '%')
{
int sublen;
char* subfmtiter = subfmt;
bool asterisk = false;
2019-07-26 02:01:27 +00:00
2018-07-20 11:48:35 +00:00
*subfmtiter++ = *format++; /* '%' */
2019-07-26 02:01:27 +00:00
2018-07-20 11:48:35 +00:00
/* %[*][width][length]specifier */
2019-07-26 02:01:27 +00:00
2018-07-20 11:48:35 +00:00
if (*format == '*')
{
2020-10-14 04:17:21 +00:00
asterisk = true;
2018-07-20 11:48:35 +00:00
*subfmtiter++ = *format++;
}
2019-07-26 02:01:27 +00:00
2020-10-14 04:17:21 +00:00
while (ISDIGIT((unsigned char)*format))
*subfmtiter++ = *format++; /* width */
2019-07-26 02:01:27 +00:00
2018-07-20 11:48:35 +00:00
/* length */
if (*format == 'h' || *format == 'l')
{
2020-10-14 04:17:21 +00:00
if (format[1] == format[0])
*subfmtiter++ = *format++;
*subfmtiter++ = *format++;
2018-07-20 11:48:35 +00:00
}
2020-10-14 04:17:21 +00:00
else if (
*format == 'j' ||
*format == 'z' ||
*format == 't' ||
*format == 'L')
2018-07-20 11:48:35 +00:00
{
*subfmtiter++ = *format++;
}
2019-07-26 02:01:27 +00:00
2018-07-20 11:48:35 +00:00
/* specifier - always a single character (except ]) */
if (*format == '[')
{
2020-10-14 04:17:21 +00:00
while (*format != ']')
*subfmtiter++ = *format++;
*subfmtiter++ = *format++;
2018-07-20 11:48:35 +00:00
}
2020-10-14 04:17:21 +00:00
else
*subfmtiter++ = *format++;
*subfmtiter++ = '%';
*subfmtiter++ = 'n';
*subfmtiter++ = '\0';
2019-07-26 02:01:27 +00:00
2020-10-14 04:17:21 +00:00
if (sizeof(void*) != sizeof(long*))
abort(); /* all pointers must have the same size */
2019-07-26 02:01:27 +00:00
2018-07-20 11:48:35 +00:00
if (asterisk)
{
2020-10-14 04:17:21 +00:00
int v = sscanf(bufiter, subfmt, &sublen);
if (v == EOF)
return EOF;
if (v != 0)
break;
2018-07-20 11:48:35 +00:00
}
else
{
int v = sscanf(bufiter, subfmt, va_arg(args_copy, void*), &sublen);
2020-10-14 04:17:21 +00:00
if (v == EOF)
return EOF;
if (v != 1)
break;
2018-07-20 11:48:35 +00:00
}
2019-07-26 02:01:27 +00:00
2018-07-20 11:48:35 +00:00
ret++;
bufiter += sublen;
}
2020-10-14 04:17:21 +00:00
else if (isspace((unsigned char)*format))
2018-07-20 11:48:35 +00:00
{
2020-10-14 04:17:21 +00:00
while (isspace((unsigned char)*bufiter))
bufiter++;
2018-07-20 11:48:35 +00:00
format++;
}
else
{
if (*bufiter != *format)
break;
bufiter++;
format++;
}
}
2019-07-26 02:01:27 +00:00
va_end(args_copy);
filestream_seek(stream, startpos + (bufiter - buf),
2020-10-14 04:17:21 +00:00
RETRO_VFS_SEEK_POSITION_START);
2019-07-26 02:01:27 +00:00
2018-07-20 11:48:35 +00:00
return ret;
}
int filestream_scanf(RFILE *stream, const char* format, ...)
{
int result;
va_list vl;
va_start(vl, format);
result = filestream_vscanf(stream, format, &vl);
va_end(vl);
return result;
}
2018-04-13 00:04:19 +00:00
int64_t filestream_seek(RFILE *stream, int64_t offset, int seek_position)
2017-12-16 02:07:16 +00:00
{
int64_t output;
2017-04-23 18:37:59 +00:00
2020-10-14 04:17:21 +00:00
if (filestream_seek_cb)
2017-12-16 13:51:35 +00:00
output = filestream_seek_cb(stream->hfile, offset, seek_position);
2017-12-16 02:07:16 +00:00
else
2020-10-14 04:17:21 +00:00
output = retro_vfs_file_seek_impl(
(libretro_vfs_implementation_file*)stream->hfile,
offset, seek_position);
2017-04-23 18:37:59 +00:00
2020-10-14 04:17:21 +00:00
if (output == VFS_ERROR_RETURN_VALUE)
2017-12-16 02:07:16 +00:00
stream->error_flag = true;
2020-10-14 04:17:21 +00:00
2017-12-16 02:07:16 +00:00
return output;
2016-03-24 05:33:52 +00:00
}
2017-12-16 02:07:16 +00:00
int filestream_eof(RFILE *stream)
2017-04-23 18:37:59 +00:00
{
return filestream_tell(stream) == filestream_get_size(stream) ? EOF : 0;
2017-12-16 02:07:16 +00:00
}
2017-04-23 18:37:59 +00:00
2018-04-13 00:04:19 +00:00
int64_t filestream_tell(RFILE *stream)
2017-12-16 02:07:16 +00:00
{
2018-04-13 00:04:19 +00:00
int64_t output;
2017-04-23 18:37:59 +00:00
2020-10-14 04:17:21 +00:00
if (filestream_size_cb)
2017-12-16 02:07:16 +00:00
output = filestream_tell_cb(stream->hfile);
else
2020-10-14 04:17:21 +00:00
output = retro_vfs_file_tell_impl(
(libretro_vfs_implementation_file*)stream->hfile);
2017-04-23 18:37:59 +00:00
2020-10-14 04:17:21 +00:00
if (output == VFS_ERROR_RETURN_VALUE)
2017-12-16 02:07:16 +00:00
stream->error_flag = true;
2017-04-23 18:37:59 +00:00
2017-12-16 02:07:16 +00:00
return output;
2017-04-23 18:37:59 +00:00
}
2017-12-16 02:07:16 +00:00
void filestream_rewind(RFILE *stream)
2017-04-23 18:37:59 +00:00
{
if (!stream)
2017-12-16 02:07:16 +00:00
return;
2017-12-16 13:51:35 +00:00
filestream_seek(stream, 0L, RETRO_VFS_SEEK_POSITION_START);
2017-12-16 02:07:16 +00:00
stream->error_flag = false;
2017-04-23 18:37:59 +00:00
}
2016-03-24 05:33:52 +00:00
2018-04-13 00:04:19 +00:00
int64_t filestream_read(RFILE *stream, void *s, int64_t len)
2017-04-23 18:37:59 +00:00
{
2017-12-16 02:07:16 +00:00
int64_t output;
2016-03-24 05:33:52 +00:00
2020-10-14 04:17:21 +00:00
if (filestream_read_cb)
2017-12-16 02:07:16 +00:00
output = filestream_read_cb(stream->hfile, s, len);
else
output = retro_vfs_file_read_impl(
(libretro_vfs_implementation_file*)stream->hfile, s, len);
2016-03-24 05:33:52 +00:00
2020-10-14 04:17:21 +00:00
if (output == VFS_ERROR_RETURN_VALUE)
2017-12-16 02:07:16 +00:00
stream->error_flag = true;
2016-03-24 05:33:52 +00:00
2017-12-16 02:07:16 +00:00
return output;
}
2017-04-23 18:37:59 +00:00
2017-12-16 02:07:16 +00:00
int filestream_flush(RFILE *stream)
{
int output;
2017-04-23 18:37:59 +00:00
2020-10-14 04:17:21 +00:00
if (filestream_flush_cb)
2017-12-16 02:07:16 +00:00
output = filestream_flush_cb(stream->hfile);
else
2020-10-14 04:17:21 +00:00
output = retro_vfs_file_flush_impl(
(libretro_vfs_implementation_file*)stream->hfile);
2017-04-23 18:37:59 +00:00
2020-10-14 04:17:21 +00:00
if (output == VFS_ERROR_RETURN_VALUE)
2017-12-16 02:07:16 +00:00
stream->error_flag = true;
2017-04-23 18:37:59 +00:00
2017-12-16 02:07:16 +00:00
return output;
2017-04-23 18:37:59 +00:00
}
2017-12-16 02:07:16 +00:00
int filestream_delete(const char *path)
2017-04-23 18:37:59 +00:00
{
2020-10-14 04:17:21 +00:00
if (filestream_remove_cb)
2017-12-16 02:07:16 +00:00
return filestream_remove_cb(path);
2017-04-23 18:37:59 +00:00
2017-12-16 02:07:16 +00:00
return retro_vfs_file_remove_impl(path);
2016-03-24 05:33:52 +00:00
}
2017-12-16 02:07:16 +00:00
int filestream_rename(const char *old_path, const char *new_path)
2016-03-24 05:33:52 +00:00
{
2020-10-14 04:17:21 +00:00
if (filestream_rename_cb)
2017-12-16 02:07:16 +00:00
return filestream_rename_cb(old_path, new_path);
2017-04-23 18:37:59 +00:00
2017-12-16 02:07:16 +00:00
return retro_vfs_file_rename_impl(old_path, new_path);
2016-03-24 05:33:52 +00:00
}
2019-12-28 01:06:37 +00:00
const char* filestream_get_path(RFILE *stream)
2016-03-24 05:33:52 +00:00
{
2020-10-14 04:17:21 +00:00
if (filestream_get_path_cb)
2017-12-16 02:07:16 +00:00
return filestream_get_path_cb(stream->hfile);
2020-10-14 04:17:21 +00:00
return retro_vfs_file_get_path_impl(
(libretro_vfs_implementation_file*)stream->hfile);
2016-03-24 05:33:52 +00:00
}
2018-04-13 00:04:19 +00:00
int64_t filestream_write(RFILE *stream, const void *s, int64_t len)
2016-03-24 05:33:52 +00:00
{
2017-12-16 02:07:16 +00:00
int64_t output;
2016-03-24 05:33:52 +00:00
2020-10-14 04:17:21 +00:00
if (filestream_write_cb)
2017-12-16 02:07:16 +00:00
output = filestream_write_cb(stream->hfile, s, len);
else
2020-10-14 04:17:21 +00:00
output = retro_vfs_file_write_impl(
(libretro_vfs_implementation_file*)stream->hfile, s, len);
2016-03-24 05:33:52 +00:00
2020-10-14 04:17:21 +00:00
if (output == VFS_ERROR_RETURN_VALUE)
2017-12-16 02:07:16 +00:00
stream->error_flag = true;
2017-04-23 18:37:59 +00:00
2017-12-16 02:07:16 +00:00
return output;
2017-04-23 18:37:59 +00:00
}
2017-12-16 02:07:16 +00:00
int filestream_putc(RFILE *stream, int c)
2017-04-23 18:37:59 +00:00
{
2017-12-16 02:07:16 +00:00
char c_char = (char)c;
if (!stream)
return EOF;
2020-10-14 04:17:21 +00:00
return filestream_write(stream, &c_char, 1) == 1
? (int)(unsigned char)c
: EOF;
2016-03-24 05:33:52 +00:00
}
2017-12-16 02:07:16 +00:00
int filestream_vprintf(RFILE *stream, const char* format, va_list args)
2016-03-24 05:33:52 +00:00
{
2018-01-05 12:23:27 +00:00
static char buffer[8 * 1024];
2020-10-14 04:17:21 +00:00
int64_t num_chars = vsnprintf(buffer, sizeof(buffer),
format, args);
2017-04-23 18:37:59 +00:00
2018-01-05 12:23:27 +00:00
if (num_chars < 0)
return -1;
else if (num_chars == 0)
return 0;
2017-12-16 02:07:16 +00:00
2018-04-13 00:04:19 +00:00
return (int)filestream_write(stream, buffer, num_chars);
2017-04-23 18:37:59 +00:00
}
2017-12-16 02:07:16 +00:00
int filestream_printf(RFILE *stream, const char* format, ...)
2017-04-23 18:37:59 +00:00
{
2018-01-05 12:23:27 +00:00
va_list vl;
2017-12-16 02:07:16 +00:00
int result;
2018-01-05 12:23:27 +00:00
va_start(vl, format);
result = filestream_vprintf(stream, format, vl);
va_end(vl);
return result;
2017-12-16 02:07:16 +00:00
}
2017-04-23 18:37:59 +00:00
2017-12-16 02:07:16 +00:00
int filestream_error(RFILE *stream)
{
return (stream && stream->error_flag);
2016-03-24 05:33:52 +00:00
}
int filestream_close(RFILE *stream)
{
2017-12-16 02:07:16 +00:00
int output;
struct retro_vfs_file_handle* fp = stream->hfile;
2016-03-24 05:33:52 +00:00
2020-10-14 04:17:21 +00:00
if (filestream_close_cb)
2017-12-16 02:07:16 +00:00
output = filestream_close_cb(fp);
2016-03-24 05:33:52 +00:00
else
2020-10-14 04:17:21 +00:00
output = retro_vfs_file_close_impl(
(libretro_vfs_implementation_file*)fp);
2016-03-24 05:33:52 +00:00
2017-12-16 02:07:16 +00:00
if (output == 0)
free(stream);
2017-04-23 18:37:59 +00:00
2017-12-16 02:07:16 +00:00
return output;
2016-03-24 05:33:52 +00:00
}
/**
* filestream_read_file:
* @path : path to file.
* @buf : buffer to allocate and read the contents of the
* file into. Needs to be freed manually.
* @len : optional output integer containing bytes read.
2016-03-24 05:33:52 +00:00
*
* Read the contents of a file into @buf.
*
* @return Non-zero on success.
2016-03-24 05:33:52 +00:00
*/
2018-04-13 00:04:19 +00:00
int64_t filestream_read_file(const char *path, void **buf, int64_t *len)
2016-03-24 05:33:52 +00:00
{
2018-04-13 00:04:19 +00:00
int64_t ret = 0;
2017-12-16 02:07:16 +00:00
int64_t content_buf_size = 0;
2016-03-24 05:33:52 +00:00
void *content_buf = NULL;
2017-12-16 02:07:16 +00:00
RFILE *file = filestream_open(path,
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
2016-03-24 05:33:52 +00:00
if (!file)
{
2020-10-14 04:17:21 +00:00
*buf = NULL;
return 0;
2016-03-24 05:33:52 +00:00
}
if ((content_buf_size = filestream_get_size(file)) < 0)
2016-03-24 05:33:52 +00:00
goto error;
if (!(content_buf = malloc((size_t)(content_buf_size + 1))))
2016-03-24 05:33:52 +00:00
goto error;
2018-04-13 00:04:19 +00:00
if ((int64_t)(uint64_t)(content_buf_size + 1) != (content_buf_size + 1))
2017-12-16 02:07:16 +00:00
goto error;
2016-03-24 05:33:52 +00:00
if ((ret = filestream_read(file, content_buf, (int64_t)content_buf_size)) <
0)
2016-03-24 05:33:52 +00:00
goto error;
2020-10-14 04:17:21 +00:00
if (filestream_close(file) != 0)
free(file);
2016-03-24 05:33:52 +00:00
*buf = content_buf;
/* Allow for easy reading of strings to be safe.
* Will only work with sane character formatting (Unix). */
2017-04-23 18:37:59 +00:00
((char*)content_buf)[ret] = '\0';
2016-03-24 05:33:52 +00:00
if (len)
*len = ret;
return 1;
error:
if (filestream_close(file) != 0)
free(file);
2016-03-24 05:33:52 +00:00
if (content_buf)
free(content_buf);
if (len)
*len = -1;
*buf = NULL;
return 0;
}
/**
* filestream_write_file:
* @path : path to file.
* @data : contents to write to the file.
* @size : size of the contents.
*
* Writes data to a file.
*
* @return true on success, otherwise false.
**/
2018-04-13 00:04:19 +00:00
bool filestream_write_file(const char *path, const void *data, int64_t size)
2016-03-24 05:33:52 +00:00
{
2018-04-13 00:04:19 +00:00
int64_t ret = 0;
2017-12-16 02:07:16 +00:00
RFILE *file = filestream_open(path,
RETRO_VFS_FILE_ACCESS_WRITE,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
2016-03-24 05:33:52 +00:00
if (!file)
return false;
ret = filestream_write(file, data, size);
2020-10-14 04:17:21 +00:00
if (filestream_close(file) != 0)
free(file);
return (ret == size);
2016-03-24 05:33:52 +00:00
}
2017-12-16 02:07:16 +00:00
/**
* filestream_getline:
*
* Returned pointer must be freed by the caller.
**/
char *filestream_getline(RFILE *stream)
2017-12-16 02:07:16 +00:00
{
2019-12-28 01:06:37 +00:00
char *newline_tmp = NULL;
2018-03-24 12:40:06 +00:00
size_t cur_size = 8;
size_t idx = 0;
int in = 0;
2019-12-28 01:06:37 +00:00
char *newline = (char*)malloc(9);
2017-12-16 02:07:16 +00:00
2018-03-24 12:40:06 +00:00
if (!stream || !newline)
{
if (newline)
free(newline);
2017-12-16 02:07:16 +00:00
return NULL;
2018-03-24 12:40:06 +00:00
}
in = filestream_getc(stream);
2017-12-16 02:07:16 +00:00
while (in != EOF && in != '\n')
{
if (idx == cur_size)
{
2018-03-24 12:40:06 +00:00
cur_size *= 2;
2017-12-16 02:07:16 +00:00
if (!(newline_tmp = (char*)realloc(newline, cur_size + 1)))
2017-12-16 02:07:16 +00:00
{
free(newline);
return NULL;
}
2018-03-24 12:40:06 +00:00
newline = newline_tmp;
2017-12-16 02:07:16 +00:00
}
newline[idx++] = in;
in = filestream_getc(stream);
}
2018-03-24 12:40:06 +00:00
newline[idx] = '\0';
2017-12-16 02:07:16 +00:00
return newline;
}
2019-07-26 02:01:27 +00:00
libretro_vfs_implementation_file* filestream_get_vfs_handle(RFILE *stream)
{
return (libretro_vfs_implementation_file*)stream->hfile;
}