mirror of
https://github.com/libretro/beetle-pce-fast-libretro.git
synced 2024-11-23 07:50:03 +00:00
Reimplement FileStream
This commit is contained in:
parent
d2c099e971
commit
1606fff668
@ -22,16 +22,61 @@
|
||||
#include "FileStream.h"
|
||||
|
||||
#include <trio/trio.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __CELLOS_LV2__
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#define fseeko fseek
|
||||
#define ftello ftell
|
||||
|
||||
#if SIZEOF_OFF_T == 4
|
||||
|
||||
#ifdef HAVE_FOPEN64
|
||||
#define fopen fopen64
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_FTELLO64
|
||||
#undef ftello
|
||||
#define ftello ftello64
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_FSEEKO64
|
||||
#undef fseeko
|
||||
#define fseeko fseeko64
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_FSTAT64
|
||||
#define fstat fstat64
|
||||
#define stat stat64
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
FileStream::FileStream(const char *path, const int mode) : fw(path, mode), OpenedMode(mode)
|
||||
FileStream::FileStream(const char *path, const int mode): OpenedMode(mode)
|
||||
{
|
||||
path_save = std::string(path);
|
||||
|
||||
if(mode == MODE_WRITE)
|
||||
fp = fopen(path, "wb");
|
||||
else
|
||||
fp = fopen(path, "rb");
|
||||
|
||||
if(!fp)
|
||||
{
|
||||
ErrnoHolder ene(errno);
|
||||
|
||||
throw(MDFN_Error(ene.Errno(), _("Error opening file \"%s\": %s"), path_save.c_str(), ene.StrError()));
|
||||
}
|
||||
}
|
||||
|
||||
FileStream::~FileStream()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
uint64 FileStream::attributes(void)
|
||||
@ -43,7 +88,6 @@ uint64 FileStream::attributes(void)
|
||||
case MODE_READ:
|
||||
ret |= ATTRIBUTE_READABLE;
|
||||
break;
|
||||
|
||||
case MODE_WRITE_SAFE:
|
||||
case MODE_WRITE:
|
||||
ret |= ATTRIBUTE_WRITEABLE;
|
||||
@ -55,30 +99,48 @@ uint64 FileStream::attributes(void)
|
||||
|
||||
uint64 FileStream::read(void *data, uint64 count, bool error_on_eos)
|
||||
{
|
||||
return fw.read(data, count, error_on_eos);
|
||||
uint64 read_count = fread(data, 1, count, fp);
|
||||
|
||||
return(read_count);
|
||||
}
|
||||
|
||||
void FileStream::write(const void *data, uint64 count)
|
||||
{
|
||||
fw.write(data, count);
|
||||
fwrite(data, 1, count, fp);
|
||||
}
|
||||
|
||||
void FileStream::seek(int64 offset, int whence)
|
||||
{
|
||||
fw.seek(offset, whence);
|
||||
fseeko(fp, offset, whence);
|
||||
}
|
||||
|
||||
int64 FileStream::tell(void)
|
||||
{
|
||||
return fw.tell();
|
||||
return ftello(fp);
|
||||
}
|
||||
|
||||
int64 FileStream::size(void)
|
||||
{
|
||||
return fw.size();
|
||||
struct stat buf;
|
||||
|
||||
fstat(fileno(fp), &buf);
|
||||
|
||||
return(buf.st_size);
|
||||
}
|
||||
|
||||
void FileStream::close(void)
|
||||
{
|
||||
fw.close();
|
||||
if(fp)
|
||||
{
|
||||
FILE *tmp = fp;
|
||||
|
||||
fp = NULL;
|
||||
|
||||
if(fclose(tmp) == EOF)
|
||||
{
|
||||
ErrnoHolder ene(errno);
|
||||
|
||||
throw(MDFN_Error(ene.Errno(), _("Error closing opened file \"%s\": %s"), path_save.c_str(), ene.StrError()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ class FileStream : public Stream
|
||||
{
|
||||
MODE_READ = FileWrapper::MODE_READ,
|
||||
MODE_WRITE = FileWrapper::MODE_WRITE,
|
||||
MODE_WRITE_SAFE = FileWrapper::MODE_WRITE_SAFE
|
||||
MODE_WRITE_SAFE = FileWrapper::MODE_WRITE_SAFE,
|
||||
};
|
||||
|
||||
FileStream(const char *path, const int mode);
|
||||
@ -47,7 +47,8 @@ class FileStream : public Stream
|
||||
virtual void close(void);
|
||||
|
||||
private:
|
||||
FileWrapper fw;
|
||||
FILE *fp;
|
||||
std::string path_save;
|
||||
const int OpenedMode;
|
||||
};
|
||||
|
||||
|
@ -22,7 +22,6 @@
|
||||
#include <stdarg.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __CELLOS_LV2__
|
||||
@ -32,13 +31,8 @@
|
||||
// Some really bad preprocessor abuse follows to handle platforms that don't have fseeko and ftello...and of course
|
||||
// for largefile support on Windows:
|
||||
|
||||
#ifndef HAVE_FSEEKO
|
||||
#define fseeko fseek
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_FTELLO
|
||||
#define ftello ftell
|
||||
#endif
|
||||
#define fseeko fseek
|
||||
#define ftello ftell
|
||||
|
||||
#if SIZEOF_OFF_T == 4
|
||||
|
||||
@ -182,17 +176,6 @@ int64 FileWrapper::size(void)
|
||||
fstat(fileno(fp), &buf);
|
||||
|
||||
return(buf.st_size);
|
||||
|
||||
/* TODO for systems without fstat()?
|
||||
int64 orig_pos = tell();
|
||||
int64 ret;
|
||||
|
||||
seek(0, SEEK_END);
|
||||
|
||||
ret = tell();
|
||||
|
||||
seek(orig_pos, SEEK_SET);
|
||||
*/
|
||||
}
|
||||
|
||||
int64 FileWrapper::tell(void)
|
||||
|
@ -24,8 +24,6 @@
|
||||
#include "file.h"
|
||||
#include "general.h"
|
||||
|
||||
static const int64 MaxROMImageSize = (int64)1 << 26; // 2 ^ 26 = 64MiB
|
||||
|
||||
#define MDFN_FILETYPE_PLAIN 0
|
||||
|
||||
bool MDFNFILE::ApplyIPS(FILE *ips)
|
||||
@ -35,7 +33,7 @@ bool MDFNFILE::ApplyIPS(FILE *ips)
|
||||
|
||||
// This function should ALWAYS close the system file "descriptor"(gzip library, zip library, or FILE *) it's given,
|
||||
// even if it errors out.
|
||||
bool MDFNFILE::MakeMemWrapAndClose(void *tz, int type)
|
||||
bool MDFNFILE::MakeMemWrapAndClose(void *tz)
|
||||
{
|
||||
bool ret = FALSE;
|
||||
|
||||
@ -45,24 +43,18 @@ bool MDFNFILE::MakeMemWrapAndClose(void *tz, int type)
|
||||
f_size = ::ftell((FILE *)tz);
|
||||
::fseek((FILE *)tz, 0, SEEK_SET);
|
||||
|
||||
if(size > MaxROMImageSize)
|
||||
if(!(f_data = (uint8*)MDFN_malloc(size, _("file read buffer"))))
|
||||
{
|
||||
MDFN_PrintError(_("ROM image is too large; maximum size allowed is %llu bytes."), (unsigned long long)MaxROMImageSize);
|
||||
goto doret;
|
||||
goto doret;
|
||||
}
|
||||
if((int64)::fread(f_data, 1, size, (FILE *)tz) != size)
|
||||
{
|
||||
ErrnoHolder ene(errno);
|
||||
MDFN_PrintError(_("Error reading file: %s"), ene.StrError());
|
||||
|
||||
if(!(f_data = (uint8*)MDFN_malloc(size, _("file read buffer"))))
|
||||
{
|
||||
goto doret;
|
||||
}
|
||||
if((int64)::fread(f_data, 1, size, (FILE *)tz) != size)
|
||||
{
|
||||
ErrnoHolder ene(errno);
|
||||
MDFN_PrintError(_("Error reading file: %s"), ene.StrError());
|
||||
|
||||
free(f_data);
|
||||
goto doret;
|
||||
}
|
||||
free(f_data);
|
||||
goto doret;
|
||||
}
|
||||
|
||||
ret = TRUE;
|
||||
|
||||
@ -98,45 +90,29 @@ MDFNFILE::~MDFNFILE()
|
||||
|
||||
bool MDFNFILE::Open(const char *path, const FileExtensionSpecStruct *known_ext, const char *purpose, const bool suppress_notfound_pe)
|
||||
{
|
||||
local_errno = 0;
|
||||
error_code = MDFNFILE_EC_OTHER; // Set to 0 at the end if the function succeeds.
|
||||
FILE *fp;
|
||||
|
||||
//f_data = (uint8 *)0xDEADBEEF;
|
||||
|
||||
{
|
||||
FILE *fp;
|
||||
|
||||
if(!(fp = fopen(path, "rb")))
|
||||
{
|
||||
ErrnoHolder ene(errno);
|
||||
local_errno = ene.Errno();
|
||||
|
||||
if(ene.Errno() == ENOENT)
|
||||
if(!(fp = fopen(path, "rb")))
|
||||
{
|
||||
local_errno = ene.Errno();
|
||||
error_code = MDFNFILE_EC_NOTFOUND;
|
||||
ErrnoHolder ene(errno);
|
||||
|
||||
if(ene.Errno() != ENOENT || !suppress_notfound_pe)
|
||||
MDFN_PrintError(_("Error opening \"%s\": %s"), path, ene.StrError());
|
||||
|
||||
return(0);
|
||||
}
|
||||
|
||||
if(ene.Errno() != ENOENT || !suppress_notfound_pe)
|
||||
MDFN_PrintError(_("Error opening \"%s\": %s"), path, ene.StrError());
|
||||
|
||||
return(0);
|
||||
}
|
||||
|
||||
::fseek(fp, 0, SEEK_SET);
|
||||
|
||||
if(!MakeMemWrapAndClose(fp, MDFN_FILETYPE_PLAIN))
|
||||
return(0);
|
||||
if(!MakeMemWrapAndClose(fp))
|
||||
return(0);
|
||||
|
||||
const char *ld = strrchr(path, '.');
|
||||
f_ext = strdup(ld ? ld + 1 : "");
|
||||
} // End normal and gzip file handling else to zip
|
||||
|
||||
// FIXME: Handle extension fixing for cases where loaded filename is like "moo.moo/lalala"
|
||||
// FIXME: Handle extension fixing for cases where loaded filename is like "moo.moo/lalala"
|
||||
|
||||
error_code = 0;
|
||||
|
||||
return(TRUE);
|
||||
return(TRUE);
|
||||
}
|
||||
|
||||
bool MDFNFILE::Close(void)
|
||||
|
@ -29,15 +29,6 @@ class MDFNFILE
|
||||
const uint8 * const &data;
|
||||
const char * const &ext;
|
||||
|
||||
// Currently, only valid with Open()
|
||||
inline int GetErrorCode(int *get_errno = NULL)
|
||||
{
|
||||
if(get_errno)
|
||||
*get_errno = local_errno;
|
||||
|
||||
return(error_code);
|
||||
}
|
||||
|
||||
inline int64 Size(void)
|
||||
{
|
||||
return(f_size);
|
||||
@ -84,13 +75,9 @@ class MDFNFILE
|
||||
|
||||
private:
|
||||
|
||||
|
||||
int error_code;
|
||||
int local_errno;
|
||||
|
||||
int64 location;
|
||||
|
||||
bool MakeMemWrapAndClose(void *tz, int type);
|
||||
bool MakeMemWrapAndClose(void *tz);
|
||||
};
|
||||
|
||||
class PtrLengthPair
|
||||
|
Loading…
Reference in New Issue
Block a user