Update file.cpp

This commit is contained in:
retro-wertz 2018-06-03 09:17:41 +08:00
parent 69793dd31c
commit a579c299c5
6 changed files with 58 additions and 246 deletions

View File

@ -82,6 +82,12 @@ ifeq ($(FRONTEND_SUPPORTS_RGB565), 1)
FLAGS += -DFRONTEND_SUPPORTS_RGB565
endif
ifneq ($(HAVE_GRIFFIN), 1)
SOURCES_C += \
$(LIBRETRO_COMMON_DIR)/streams/file_stream.c \
$(LIBRETRO_COMMON_DIR)/vfs/vfs_implementation.c
endif
ifneq ($(HAVE_GRIFFIN), 1)
SOURCES_CXX += \
$(MEDNAFEN_DIR)/settings.cpp \

View File

@ -2679,18 +2679,18 @@ static bool CPUInit(const std::string bios_fn)
{ NULL, NULL }
};
MDFNFILE bios_fp;
MDFNFILE *bios_fp = file_open(MDFN_MakeFName(MDFNMKF_FIRMWARE, 0, bios_fn.c_str()).c_str());
if(bios_fp.Open(MDFN_MakeFName(MDFNMKF_FIRMWARE, 0, bios_fn.c_str()), KnownBIOSExtensions, _("GBA BIOS")))
if(bios_fp)
{
if(GET_FSIZE(bios_fp) == 0x4000)
if(GET_FSIZE_PTR(bios_fp) == 0x4000)
{
memcpy(bios, GET_FDATA(bios_fp), 0x4000);
memcpy(bios, GET_FDATA_PTR(bios_fp), 0x4000);
useBios = true;
}
else
log_cb(RETRO_LOG_WARN, "Invalid BIOS file size.\n");
bios_fp.Close();
file_close(bios_fp);
}
else
log_cb(RETRO_LOG_WARN, "Cannot find GBA bios file. Using high-level bios emulation.\n");

View File

@ -16,195 +16,52 @@
*/
#include "mednafen.h"
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <streams/file_stream.h>
#include "file.h"
#include "general.h"
bool MDFNFILE::ApplyIPS(void *unused)
struct MDFNFILE *file_open(const char *path)
{
(void)unused;
int64_t size = 0;
const char *ld = NULL;
struct MDFNFILE *file = (struct MDFNFILE*)calloc(1, sizeof(*file));
return 1;
if (!file)
return NULL;
if (!filestream_read_file(path, (void**)&file->data, &size))
goto error;
ld = (const char*)strrchr(path, '.');
file->size = (int64_t)size;
file->ext = strdup(ld ? ld + 1 : "");
return file;
error:
if (file)
free(file);
return NULL;
}
// 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 *fp)
int file_close(struct MDFNFILE *file)
{
bool ret = FALSE;
location = 0;
::fseek((FILE *)fp, 0, SEEK_END);
f_size = ::ftell((FILE *)fp);
::fseek((FILE *)fp, 0, SEEK_SET);
if (!(f_data = (uint8*)malloc(f_size)))
goto fail;
::fread(f_data, 1, f_size, (FILE *)fp);
ret = TRUE;
fail:
fclose((FILE*)fp);
return ret;
}
MDFNFILE::MDFNFILE()
{
f_data = NULL;
f_size = 0;
f_ext = NULL;
location = 0;
}
MDFNFILE::MDFNFILE(const char *path, const void *known_ext, const char *purpose)
{
(void)known_ext;
if (!Open(path, known_ext, purpose, false))
throw(MDFN_Error(0, "TODO ERROR"));
}
MDFNFILE::~MDFNFILE()
{
Close();
}
bool MDFNFILE::Open(const char *path, const void *known_ext, const char *purpose, const bool suppress_notfound_pe)
{
FILE *fp;
(void)known_ext;
if (!(fp = fopen(path, "rb")))
return FALSE;
::fseek(fp, 0, SEEK_SET);
if (!MakeMemWrapAndClose(fp))
return FALSE;
const char *ld = (const char*)strrchr(path, '.');
f_ext = strdup(ld ? ld + 1 : "");
return(TRUE);
}
bool MDFNFILE::Close(void)
{
if (f_ext)
free(f_ext);
f_ext = 0;
if (f_data)
free(f_data);
f_data = 0;
return(1);
}
uint64 MDFNFILE::fread(void *ptr, size_t element_size, size_t nmemb)
{
uint32 total = element_size * nmemb;
if (location >= f_size)
if (!file)
return 0;
if ((location + total) > f_size)
{
int64 ak = f_size - location;
if (file->ext)
free(file->ext);
file->ext = NULL;
memcpy((uint8*)ptr, f_data + location, ak);
if (file->data)
free(file->data);
file->data = NULL;
location = f_size;
return(ak / element_size);
}
else
{
memcpy((uint8*)ptr, f_data + location, total);
location += total;
return nmemb;
}
}
int MDFNFILE::fseek(int64 offset, int whence)
{
switch(whence)
{
case SEEK_SET:
if (offset >= f_size)
return -1;
location = offset;
break;
case SEEK_CUR:
if ((offset + location) > f_size)
return -1;
location += offset;
break;
}
return 0;
}
int MDFNFILE::read16le(uint16 *val)
{
if ((location + 2) > f_size)
return 0;
*val = MDFN_de16lsb(f_data + location);
location += 2;
free(file);
return 1;
}
int MDFNFILE::read32le(uint32 *val)
{
if ((location + 4) > f_size)
return 0;
*val = MDFN_de32lsb(f_data + location);
location += 4;
return 1;
}
char *MDFNFILE::fgets(char *s, int buffer_size)
{
int pos = 0;
if (!buffer_size)
return(NULL);
if (location >= buffer_size)
return(NULL);
while(pos < (buffer_size - 1) && location < buffer_size)
{
int v = f_data[location];
s[pos] = v;
location++;
pos++;
if (v == '\n')
break;
}
if (buffer_size)
s[pos] = 0;
return s;
}
static INLINE bool MDFN_DumpToFileReal(const char *filename, int compress, const std::vector<PtrLengthPair> &pearpairs)
{
FILE *fp = fopen(filename, "wb");

View File

@ -6,66 +6,17 @@
#define MDFNFILE_EC_NOTFOUND 1
#define MDFNFILE_EC_OTHER 2
class MDFNFILE
struct MDFNFILE
{
public:
MDFNFILE();
// WIP constructors:
MDFNFILE(const char *path, const void *known_ext, const char *purpose = NULL);
~MDFNFILE();
bool Open(const char *path, const void *known_ext, const char *purpose = NULL, const bool suppress_notfound_pe = FALSE);
INLINE bool Open(const std::string &path, const void *known_ext, const char *purpose = NULL, const bool suppress_notfound_pe = FALSE)
{
return(Open(path.c_str(), known_ext, purpose, suppress_notfound_pe));
}
bool ApplyIPS(void*);
bool Close(void);
uint64 fread(void *ptr, size_t size, size_t nmemb);
int fseek(int64 offset, int whence);
inline uint64 ftell(void)
{
return(location);
}
inline void rewind(void)
{
location = 0;
}
int read32le(uint32 *Bufo);
int read16le(uint16 *Bufo);
inline int fgetc(void)
{
if(location < f_size)
return f_data[location++];
return EOF;
}
inline int fisarchive(void)
{
return(0);
}
char *fgets(char *s, int size);
uint8 *f_data;
int64 f_size;
char *f_ext;
private:
int64 location;
bool MakeMemWrapAndClose(void *tz);
char *ext;
uint8_t *data;
int64_t size;
int64_t location;
};
struct MDFNFILE *file_open(const char *path);
int file_close(struct MDFNFILE *file);
class PtrLengthPair
{
public:

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <stdint.h>
#include <retro_inline.h>
typedef int8_t int8;
typedef int16_t int16;
@ -18,7 +19,6 @@ typedef uint64_t uint64;
#define MDFN_UNLIKELY(n) __builtin_expect((n) != 0, 0)
#define MDFN_LIKELY(n) __builtin_expect((n) != 0, 1)
#define INLINE inline __attribute__((always_inline))
#define NO_INLINE __attribute__((noinline))
#if defined(__386__) || defined(__i386__) || defined(__i386) || defined(_M_IX86) || defined(_M_I386)
@ -33,11 +33,12 @@ typedef uint64_t uint64;
#define MDFN_NOWARN_UNUSED __attribute__((unused))
#elif defined(_MSC_VER)
#define roundf(in) (in >= 0.0f ? floorf(in + 0.5f) : ceilf(in - 0.5f))
#define INLINE inline
#if _MSC_VER < 1800
#define roundf(in) (in >= 0.0f ? floorf(in + 0.5f) : ceilf(in - 0.5f))
#endif
#define NO_INLINE
#define MDFN_LIKELY(n) ((n) != 0)
#define MDFN_UNLIKELY(n) ((n) != 0)
#define MDFN_LIKELY(n) ((n) != 0)
#define MDFN_UNLIKELY(n) ((n) != 0)
#define MDFN_FASTCALL
@ -50,7 +51,6 @@ typedef uint64_t uint64;
#else
#error "Not compiling with GCC nor MSVC"
#define INLINE inline
#define NO_INLINE
#define MDFN_FASTCALL
@ -119,6 +119,4 @@ typedef unsigned char Boolean; /* 0 or 1 */
#define require( expr ) assert( expr )
#include "error.h"
#endif

View File

@ -18,9 +18,9 @@
#define GET_FDATA(fp) (fp.f_data)
#define GET_FSIZE(fp) (fp.f_size)
#define GET_FEXTS(fp) (fp.f_ext)
#define GET_FDATA_PTR(fp) (fp->f_data)
#define GET_FSIZE_PTR(fp) (fp->f_size)
#define GET_FEXTS_PTR(fp) (fp->f_ext)
#define GET_FDATA_PTR(fp) (fp->data)
#define GET_FSIZE_PTR(fp) (fp->size)
#define GET_FEXTS_PTR(fp) (fp->ext)
#define gzopen(a, b) fopen(a, b)
#define gzread(a, b, c) fread(b, c, 1, a)
#define gzclose(a) fclose(a)