diff --git a/src/movie.cpp b/src/movie.cpp index 6c9855e9..15daf6f8 100644 --- a/src/movie.cpp +++ b/src/movie.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #ifdef WIN32 #include @@ -335,7 +336,7 @@ bool FCEUMOV_Mode(int modemask) } //yuck... another custom text parser. -static void LoadFM2(MovieData& movieData, std::istream* fp, int size=-1, bool stopAfterHeader = false) +static void LoadFM2(MovieData& movieData, std::istream* fp, int size=INT_MAX, bool stopAfterHeader = false) { std::string key,value; enum { @@ -345,7 +346,7 @@ static void LoadFM2(MovieData& movieData, std::istream* fp, int size=-1, bool st for(;;) { bool iswhitespace, isrecchar, isnewline; - if(size--==0) goto bail; + if(size--<=0) goto bail; int c = fp->get(); if(c == -1) goto bail; @@ -369,7 +370,10 @@ static void LoadFM2(MovieData& movieData, std::istream* fp, int size=-1, bool st dorecord: if (stopAfterHeader) return; MovieRecord record; + int preparse = fp->tellg(); record.parse(&movieData, fp); + int postparse = fp->tellg(); + size -= (postparse-preparse); movieData.records.push_back(record); state = NEWLINE; break; diff --git a/src/state.cpp b/src/state.cpp index 0cc62a74..b6a2f9af 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -135,49 +135,6 @@ static int SubWrite(std::ostream* os, SFORMAT *sf) return(acc); } -static int SubWrite(FILE *st, SFORMAT *sf) -{ - uint32 acc=0; - - while(sf->v) - { - if(sf->s==~0) //Link to another struct - { - uint32 tmp; - - if(!(tmp=SubWrite(st,(SFORMAT *)sf->v))) - return(0); - acc+=tmp; - sf++; - continue; - } - - acc+=8; //Description + size - acc+=sf->s&(~FCEUSTATE_FLAGS); - - if(st) // Are we writing or calculating the size of this block? - { - fwrite(sf->desc,1,4,st); - write32le(sf->s&(~FCEUSTATE_FLAGS),st); - -#ifndef LSB_FIRST - if(sf->s&RLSB) - FlipByteOrder(sf->v,sf->s&(~FCEUSTATE_FLAGS)); -#endif - - fwrite((uint8 *)sf->v,1,sf->s&(~FCEUSTATE_FLAGS),st); - //Now restore the original byte order. -#ifndef LSB_FIRST - if(sf->s&RLSB) - FlipByteOrder(sf->v,sf->s&(~FCEUSTATE_FLAGS)); -#endif - } - sf++; - } - - return(acc); -} - static int WriteStateChunk(std::ostream* os, int type, SFORMAT *sf) { os->put(type); @@ -191,22 +148,6 @@ static int WriteStateChunk(std::ostream* os, int type, SFORMAT *sf) return (bsize+5); } -static int WriteStateChunk(FILE *st, int type, SFORMAT *sf) -{ - int bsize; - - fputc(type,st); - - bsize=SubWrite((FILE*)0,sf); - write32le(bsize,st); - - if(!SubWrite(st,sf)) - { - return 5; - } - return (bsize+5); -} - static SFORMAT *CheckS(SFORMAT *sf, uint32 tsize, char *desc) { while(sf->v) @@ -239,7 +180,7 @@ static bool ReadStateChunk(std::istream* is, SFORMAT *sf, int size) { uint32 tsize; char toa[4]; - if(is->readsome(toa,4)<4) + if(is->read(toa,4).gcount()<4) return false; read32le(&tsize,is); @@ -247,9 +188,9 @@ static bool ReadStateChunk(std::istream* is, SFORMAT *sf, int size) if((tmp=CheckS(sf,tsize,toa))) { if(tmp->s&FCEUSTATE_INDIRECT) - is->readsome(*(char **)tmp->v,tmp->s&(~FCEUSTATE_FLAGS)); + is->read(*(char **)tmp->v,tmp->s&(~FCEUSTATE_FLAGS)); else - is->readsome((char *)tmp->v,tmp->s&(~FCEUSTATE_FLAGS)); + is->read((char *)tmp->v,tmp->s&(~FCEUSTATE_FLAGS)); #ifndef LSB_FIRST if(tmp->s&RLSB) @@ -317,7 +258,7 @@ static bool ReadStateChunks(std::istream* is, int32 totalsize) // load back buffer { extern uint8 *XBackBuf; - if(is->readsome((char*)XBackBuf,size) != size) + if(is->read((char*)XBackBuf,size).gcount() != size) ret = false; //MBG TODO - can this be moved to a better place? @@ -406,6 +347,15 @@ bool FCEUSS_SaveMS(std::ostream* outstream, int compressionLevel) //do not save the movie state if we are in tasedit! that is a huge waste of time and space! if(!FCEUMOV_Mode(MOVIEMODE_TASEDIT)) { + //more stable? but we dont think the other one is unstable + //memorystream mstemp; + //int size = FCEUMOV_WriteState(&mstemp); + //mstemp.sync(); + //os->put(7); + //write32le(size, os); + //os->write(mstemp.buf(),mstemp.size()); + //totalsize += 5+size; + os->seekp(5,std::ios::cur); int size = FCEUMOV_WriteState(os); os->seekp(-(size+5),std::ios::cur); @@ -634,7 +584,6 @@ bool FCEUSS_Load(char *fname) void FCEUSS_CheckStates(void) { FILE *st=NULL; - char *fn; int ssel; for(ssel=0;ssel<10;ssel++) diff --git a/src/utils/endian.cpp b/src/utils/endian.cpp index e19fcc3a..3c920900 100644 --- a/src/utils/endian.cpp +++ b/src/utils/endian.cpp @@ -95,7 +95,7 @@ int read32le(uint32 *Bufo, FILE *fp) int read32le(uint32 *Bufo, std::istream *is) { uint32 buf; - if(is->readsome((char*)&buf,4) != 4) + if(is->read((char*)&buf,4).gcount() != 4) return 0; #ifdef LSB_FIRST *(uint32*)Bufo=buf;