Change EMUFILE fseek,ftell, and other related functions to use types that follow the C standard (long int for position offsets and size_t for sizes). This allows for larger max file sizes on unix platforms and cleaner code that better maps to the C standard.

This commit is contained in:
harry 2023-01-25 21:46:30 -05:00
parent b338d4a037
commit 5e9e80eddc
4 changed files with 27 additions and 26 deletions

View File

@ -78,7 +78,7 @@ void EMUFILE_FILE::open(const char* fname, const char* mode)
} }
void EMUFILE_FILE::truncate(s32 length) void EMUFILE_FILE::truncate(size_t length)
{ {
::fflush(fp); ::fflush(fp);
#ifdef _MSC_VER #ifdef _MSC_VER

View File

@ -109,13 +109,13 @@ public:
double readdouble(); double readdouble();
size_t readdouble(double* val); size_t readdouble(double* val);
virtual int fseek(int offset, int origin) = 0; virtual int fseek(long int offset, int origin) = 0;
virtual int ftell() = 0; virtual long int ftell() = 0;
virtual int size() = 0; virtual size_t size() = 0;
virtual void fflush() = 0; virtual void fflush() = 0;
virtual void truncate(s32 length) = 0; virtual void truncate(size_t length) = 0;
}; };
//todo - handle read-only specially? //todo - handle read-only specially?
@ -123,9 +123,10 @@ class EMUFILE_MEMORY : public EMUFILE {
protected: protected:
std::vector<u8> *vec; std::vector<u8> *vec;
bool ownvec; bool ownvec;
s32 pos, len; long int pos;
size_t len;
void reserve(u32 amt) { void reserve(size_t amt) {
if(vec->size() < amt) if(vec->size() < amt)
vec->resize(amt); vec->resize(amt);
} }
@ -133,12 +134,12 @@ protected:
public: public:
EMUFILE_MEMORY(std::vector<u8> *underlying) : vec(underlying), ownvec(false), pos(0), len((s32)underlying->size()) { } EMUFILE_MEMORY(std::vector<u8> *underlying) : vec(underlying), ownvec(false), pos(0), len((s32)underlying->size()) { }
EMUFILE_MEMORY(u32 preallocate) : vec(new std::vector<u8>()), ownvec(true), pos(0), len(0) { EMUFILE_MEMORY(size_t preallocate) : vec(new std::vector<u8>()), ownvec(true), pos(0), len(0) {
vec->resize(preallocate); vec->resize(preallocate);
len = preallocate; len = preallocate;
} }
EMUFILE_MEMORY() : vec(new std::vector<u8>()), ownvec(true), pos(0), len(0) { vec->reserve(1024); } EMUFILE_MEMORY() : vec(new std::vector<u8>()), ownvec(true), pos(0), len(0) { vec->reserve(1024); }
EMUFILE_MEMORY(void* buf, s32 size) : vec(new std::vector<u8>()), ownvec(true), pos(0), len(size) { EMUFILE_MEMORY(void* buf, size_t size) : vec(new std::vector<u8>()), ownvec(true), pos(0), len(size) {
vec->resize(size); vec->resize(size);
if(size != 0) if(size != 0)
memcpy(&vec->front(),buf,size); memcpy(&vec->front(),buf,size);
@ -150,7 +151,7 @@ public:
virtual EMUFILE* memwrap(); virtual EMUFILE* memwrap();
virtual void truncate(s32 length) virtual void truncate(size_t length)
{ {
vec->resize(length); vec->resize(length);
len = length; len = length;
@ -216,13 +217,13 @@ public:
//they handle the return values correctly //they handle the return values correctly
virtual void fwrite(const void *ptr, size_t bytes){ virtual void fwrite(const void *ptr, size_t bytes){
reserve(pos+(s32)bytes); reserve(pos+bytes);
memcpy(buf()+pos,ptr,bytes); memcpy(buf()+pos,ptr,bytes);
pos += (s32)bytes; pos += bytes;
len = std::max<int>(pos,len); len = std::max<int>(pos,len);
} }
virtual int fseek(int offset, int origin){ virtual int fseek(long int offset, int origin){
//work differently for read-only...? //work differently for read-only...?
switch(origin) { switch(origin) {
case SEEK_SET: case SEEK_SET:
@ -241,13 +242,13 @@ public:
return 0; return 0;
} }
virtual int ftell() { virtual long int ftell() {
return pos; return pos;
} }
virtual void fflush() {} virtual void fflush() {}
void set_len(s32 length) void set_len(size_t length)
{ {
len = length; len = length;
if(pos > length) if(pos > length)
@ -258,7 +259,7 @@ public:
vec->resize(len); vec->resize(len);
} }
virtual int size() { return (int)len; } virtual size_t size() { return len; }
}; };
class EMUFILE_FILE : public EMUFILE { class EMUFILE_FILE : public EMUFILE {
@ -288,7 +289,7 @@ public:
bool is_open() { return fp != NULL; } bool is_open() { return fp != NULL; }
virtual void truncate(s32 length); virtual void truncate(size_t length);
virtual int fprintf(const char *format, ...) { virtual int fprintf(const char *format, ...) {
va_list argptr; va_list argptr;
@ -321,20 +322,20 @@ public:
failbit = true; failbit = true;
} }
virtual int fseek(int offset, int origin) { virtual int fseek(long int offset, int origin) {
return ::fseek(fp, offset, origin); return ::fseek(fp, offset, origin);
} }
virtual int ftell() { virtual long int ftell() {
return (u32)::ftell(fp); return ::ftell(fp);
} }
virtual int size() { virtual size_t size() {
int oldpos = ftell(); long int oldpos = ftell();
fseek(0,SEEK_END); fseek(0,SEEK_END);
int len = ftell(); long int len = ftell();
fseek(oldpos,SEEK_SET); fseek(oldpos,SEEK_SET);
return len; return static_cast<size_t>(len);
} }
virtual void fflush() { virtual void fflush() {

View File

@ -327,7 +327,7 @@ FCEUFILE * FCEU_fopen(const char *path, const char *ipsfn, const char *mode, cha
if(gzfile) { if(gzfile) {
delete fp; delete fp;
int size; size_t size;
for(size=0; gzgetc(gzfile) != EOF; size++) {} for(size=0; gzgetc(gzfile) != EOF; size++) {}
EMUFILE_MEMORY* ms = new EMUFILE_MEMORY(size); EMUFILE_MEMORY* ms = new EMUFILE_MEMORY(size);
gzseek(gzfile,0,SEEK_SET); gzseek(gzfile,0,SEEK_SET);

View File

@ -35,7 +35,7 @@ struct FCEUFILE {
int archiveIndex; int archiveIndex;
//the size of the file //the size of the file
int size; size_t size;
//whether the file is contained in an archive //whether the file is contained in an archive
bool isArchive() { return archiveCount > 0; } bool isArchive() { return archiveCount > 0; }