2015-09-17 11:41:48 +02:00
|
|
|
#include <retro_file.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-09-17 20:24:49 +02:00
|
|
|
#include <errno.h>
|
2015-09-17 11:41:48 +02:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <compat/posix_string.h>
|
|
|
|
# ifdef _MSC_VER
|
|
|
|
# define setmode _setmode
|
|
|
|
# endif
|
|
|
|
# ifdef _XBOX
|
|
|
|
# include <xtl.h>
|
|
|
|
# define INVALID_FILE_ATTRIBUTES -1
|
|
|
|
# else
|
|
|
|
# include <io.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
# include <direct.h>
|
|
|
|
# include <windows.h>
|
|
|
|
# endif
|
|
|
|
#elif defined(VITA)
|
|
|
|
# include <psp2/io/fcntl.h>
|
|
|
|
# include <psp2/io/dirent.h>
|
|
|
|
#else
|
|
|
|
# if defined(PSP)
|
|
|
|
# include <pspiofilemgr.h>
|
|
|
|
# endif
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/stat.h>
|
|
|
|
# include <dirent.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __CELLOS_LV2__
|
|
|
|
#include <cell/cell_fs.h>
|
|
|
|
#else
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2015-09-18 03:25:53 +02:00
|
|
|
#if 1
|
2015-09-17 21:31:00 +02:00
|
|
|
#define HAVE_BUFFERED_IO 1
|
|
|
|
#endif
|
|
|
|
|
2015-09-17 20:24:49 +02:00
|
|
|
struct RFILE
|
2015-09-17 11:41:48 +02:00
|
|
|
{
|
2015-09-17 20:48:06 +02:00
|
|
|
#if defined(PSP) || defined(VITA)
|
2015-09-18 02:38:48 +02:00
|
|
|
SceUID fd;
|
2015-09-17 21:31:00 +02:00
|
|
|
#elif defined(HAVE_BUFFERED_IO)
|
2015-09-18 02:38:48 +02:00
|
|
|
FILE *fd;
|
2015-09-17 20:48:06 +02:00
|
|
|
#else
|
2015-09-17 11:41:48 +02:00
|
|
|
int fd;
|
2015-09-17 20:48:06 +02:00
|
|
|
#endif
|
2015-09-17 20:24:49 +02:00
|
|
|
};
|
2015-09-17 11:41:48 +02:00
|
|
|
|
2015-09-17 19:57:51 +02:00
|
|
|
RFILE *retro_fopen(const char *path, unsigned mode, ssize_t len)
|
2015-09-17 11:41:48 +02:00
|
|
|
{
|
2015-09-17 19:57:51 +02:00
|
|
|
RFILE *stream = (RFILE*)calloc(1, sizeof(*stream));
|
2015-09-17 11:41:48 +02:00
|
|
|
|
|
|
|
if (!stream)
|
|
|
|
return NULL;
|
|
|
|
|
2015-09-17 19:57:51 +02:00
|
|
|
switch (mode)
|
2015-09-17 11:41:48 +02:00
|
|
|
{
|
2015-09-17 19:57:51 +02:00
|
|
|
case RFILE_MODE_READ:
|
2015-09-17 20:48:06 +02:00
|
|
|
#if defined(VITA) || defined(PSP)
|
2015-09-18 02:38:48 +02:00
|
|
|
stream->fd = sceIoOpen(path, O_RDONLY, 0777);
|
2015-09-17 21:31:00 +02:00
|
|
|
#elif defined(HAVE_BUFFERED_IO)
|
2015-09-18 02:38:48 +02:00
|
|
|
stream->fd = fopen(path, "rb");
|
2015-09-17 20:48:06 +02:00
|
|
|
#else
|
2015-09-17 19:57:51 +02:00
|
|
|
stream->fd = open(path, O_RDONLY);
|
2015-09-17 20:48:06 +02:00
|
|
|
#endif
|
2015-09-17 11:41:48 +02:00
|
|
|
break;
|
2015-09-17 19:57:51 +02:00
|
|
|
case RFILE_MODE_WRITE:
|
2015-09-17 20:48:06 +02:00
|
|
|
#if defined(VITA) || defined(PSP)
|
2015-09-18 02:38:48 +02:00
|
|
|
stream->fd = sceIoOpen(path, O_WRONLY | O_CREAT, 0777);
|
2015-09-17 21:31:00 +02:00
|
|
|
#elif defined(HAVE_BUFFERED_IO)
|
2015-09-18 02:38:48 +02:00
|
|
|
stream->fd = fopen(path, "wb");
|
2015-09-17 20:48:06 +02:00
|
|
|
#else
|
2015-09-17 19:57:51 +02:00
|
|
|
stream->fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
|
2015-09-17 20:48:06 +02:00
|
|
|
#endif
|
2015-09-17 11:41:48 +02:00
|
|
|
break;
|
2015-09-17 19:57:51 +02:00
|
|
|
case RFILE_MODE_READ_WRITE:
|
2015-09-17 20:48:06 +02:00
|
|
|
#if defined(VITA) || defined(PSP)
|
2015-09-18 02:38:48 +02:00
|
|
|
stream->fd = sceIoOpen(path, O_RDWR, 0777);
|
2015-09-17 21:31:00 +02:00
|
|
|
#elif defined(HAVE_BUFFERED_IO)
|
2015-09-18 02:38:48 +02:00
|
|
|
stream->fd = fopen(path, "w+");
|
2015-09-17 20:48:06 +02:00
|
|
|
#else
|
2015-09-17 11:41:48 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
stream->fd = open(path, O_RDWR | O_BINARY);
|
|
|
|
#else
|
|
|
|
stream->fd = open(path, O_RDWR);
|
2015-09-17 20:48:06 +02:00
|
|
|
#endif
|
2015-09-17 11:41:48 +02:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-18 02:38:48 +02:00
|
|
|
#if defined(HAVE_BUFFERED_IO)
|
|
|
|
if (!stream->fd)
|
2015-09-17 21:31:00 +02:00
|
|
|
#else
|
|
|
|
if (stream->fd == -1)
|
|
|
|
#endif
|
|
|
|
goto error;
|
|
|
|
|
2015-09-17 11:41:48 +02:00
|
|
|
return stream;
|
2015-09-17 21:31:00 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
retro_fclose(stream);
|
|
|
|
return NULL;
|
2015-09-17 11:41:48 +02:00
|
|
|
}
|
|
|
|
|
2015-09-17 19:57:51 +02:00
|
|
|
ssize_t retro_fseek(RFILE *stream, ssize_t offset, int whence)
|
2015-09-17 11:41:48 +02:00
|
|
|
{
|
|
|
|
if (!stream)
|
|
|
|
return -1;
|
|
|
|
|
2015-09-17 20:48:06 +02:00
|
|
|
#if defined(VITA) || defined(PSP)
|
2015-09-18 02:38:48 +02:00
|
|
|
return sceIoLseek(stream->fd, (SceOff)offset, whence);
|
2015-09-17 21:31:00 +02:00
|
|
|
#elif defined(HAVE_BUFFERED_IO)
|
2015-09-18 02:38:48 +02:00
|
|
|
return fseek(stream->fd, (long)offset, whence);
|
2015-09-17 20:48:06 +02:00
|
|
|
#else
|
2015-09-17 11:41:48 +02:00
|
|
|
return lseek(stream->fd, offset, whence);
|
2015-09-17 20:48:06 +02:00
|
|
|
#endif
|
2015-09-17 11:41:48 +02:00
|
|
|
}
|
|
|
|
|
2015-09-17 19:57:51 +02:00
|
|
|
ssize_t retro_fread(RFILE *stream, void *s, size_t len)
|
2015-09-17 11:41:48 +02:00
|
|
|
{
|
|
|
|
if (!stream)
|
|
|
|
return -1;
|
2015-09-17 20:48:06 +02:00
|
|
|
#if defined(VITA) || defined(PSP)
|
2015-09-18 02:38:48 +02:00
|
|
|
return sceIoRead(stream->fd, s, len);
|
2015-09-17 21:31:00 +02:00
|
|
|
#elif defined(HAVE_BUFFERED_IO)
|
2015-09-18 03:21:56 +02:00
|
|
|
return fread(s, 1, len, stream->fd);
|
2015-09-17 20:48:06 +02:00
|
|
|
#else
|
2015-09-17 11:41:48 +02:00
|
|
|
return read(stream->fd, s, len);
|
2015-09-17 20:48:06 +02:00
|
|
|
#endif
|
2015-09-17 11:41:48 +02:00
|
|
|
}
|
|
|
|
|
2015-09-17 19:57:51 +02:00
|
|
|
ssize_t retro_fwrite(RFILE *stream, const void *s, size_t len)
|
2015-09-17 11:41:48 +02:00
|
|
|
{
|
|
|
|
if (!stream)
|
|
|
|
return -1;
|
2015-09-17 20:48:06 +02:00
|
|
|
#if defined(VITA) || defined(PSP)
|
2015-09-18 02:38:48 +02:00
|
|
|
return sceIoWrite(stream->fd, s, len);
|
2015-09-17 21:31:00 +02:00
|
|
|
#elif defined(HAVE_BUFFERED_IO)
|
2015-09-18 03:21:56 +02:00
|
|
|
return fwrite(s, 1, len, stream->fd);
|
2015-09-17 20:48:06 +02:00
|
|
|
#else
|
2015-09-17 11:41:48 +02:00
|
|
|
return write(stream->fd, s, len);
|
2015-09-17 20:48:06 +02:00
|
|
|
#endif
|
2015-09-17 11:41:48 +02:00
|
|
|
}
|
|
|
|
|
2015-09-17 19:57:51 +02:00
|
|
|
void retro_fclose(RFILE *stream)
|
2015-09-17 11:41:48 +02:00
|
|
|
{
|
|
|
|
if (!stream)
|
|
|
|
return;
|
|
|
|
|
2015-09-17 20:48:06 +02:00
|
|
|
#if defined(VITA) || defined(PSP)
|
2015-09-18 02:38:48 +02:00
|
|
|
if (stream->fd > 0)
|
|
|
|
sceIoClose(stream->fd);
|
2015-09-17 21:31:00 +02:00
|
|
|
#elif defined(HAVE_BUFFERED_IO)
|
2015-09-18 02:38:48 +02:00
|
|
|
if (stream->fd)
|
|
|
|
fclose(stream->fd);
|
2015-09-17 20:48:06 +02:00
|
|
|
#else
|
2015-09-17 21:31:00 +02:00
|
|
|
if (stream->fd > 0)
|
|
|
|
close(stream->fd);
|
2015-09-17 20:48:06 +02:00
|
|
|
#endif
|
2015-09-17 11:41:48 +02:00
|
|
|
free(stream);
|
|
|
|
}
|
2015-09-17 20:24:49 +02:00
|
|
|
|
2015-09-17 21:31:00 +02:00
|
|
|
static bool retro_fread_iterate(RFILE *stream, char *s, size_t len, ssize_t *bytes_written)
|
|
|
|
{
|
|
|
|
*bytes_written = retro_fread(stream, s, len);
|
|
|
|
#if defined(HAVE_BUFFERED_IO)
|
|
|
|
return (*bytes_written < 0);
|
|
|
|
#else
|
|
|
|
return (*bytes_written < 0 && errno == EINTR);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-09-17 20:24:49 +02:00
|
|
|
bool retro_fmemcpy(const char *path, char *s, size_t len, ssize_t *bytes_written)
|
|
|
|
{
|
|
|
|
RFILE *stream = retro_fopen(path, RFILE_MODE_READ, -1);
|
|
|
|
if (!stream)
|
|
|
|
return false;
|
|
|
|
|
2015-09-18 02:38:48 +02:00
|
|
|
while(retro_fread_iterate(stream, s, len, bytes_written));
|
2015-09-17 20:24:49 +02:00
|
|
|
|
|
|
|
retro_fclose(stream);
|
|
|
|
if (*bytes_written < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* NULL-terminate the string. */
|
|
|
|
s[*bytes_written] = '\0';
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|