diff --git a/src/movie.cpp b/src/movie.cpp index 9a84bc42..873fe552 100644 --- a/src/movie.cpp +++ b/src/movie.cpp @@ -47,6 +47,9 @@ using namespace std; //todo - handle case where read+write is requested, but the file is actually readonly (could be confusing) +//todo - could we, given a field size, over-read from an inputstream and then parse out an integer? +//that would be faster than several reads, perhaps. + //sometimes we accidentally produce movie stop signals while we're trying to do other things with movies.. bool suppressMovieStop=false; @@ -143,7 +146,7 @@ void MovieRecord::parse(MovieData* md, std::istream* is) //by the time we get in here, the initial pipe has already been extracted //extract the commands - commands = uintDecFromIstream(is); + commands = uint32DecFromIstream(is); //*is >> commands; is->get(); //eat the pipe @@ -165,13 +168,12 @@ void MovieRecord::parse(MovieData* md, std::istream* is) { int x,y,b,bogo; uint64 zaphit; - *is >> x >> y >> b >> bogo >> zaphit; - //todo: test uintDecFromIstream - zappers[port].x = x; - zappers[port].y = y; - zappers[port].b = b; - zappers[port].bogo = bogo; - zappers[port].zaphit = zaphit; + //*is >> x >> y >> b >> bogo >> zaphit; + zappers[port].x = uint32DecFromIstream(is); + zappers[port].y = uint32DecFromIstream(is); + zappers[port].b = uint32DecFromIstream(is); + zappers[port].bogo = uint32DecFromIstream(is); + zappers[port].zaphit = uint64DecFromIstream(is); } is->get(); //eat the pipe @@ -192,7 +194,9 @@ void MovieRecord::dump(MovieData* md, std::ostream* os, int index) //fprintf(fp,"%08d",index); //dump the misc commands - *os << '|' << setw(1) << (int)commands; + //*os << '|' << setw(1) << (int)commands; + os->put('|'); + putdec(os,commands); //a special case: if fourscore is enabled, dump four gamepads if(md->fourscore) @@ -211,7 +215,13 @@ void MovieRecord::dump(MovieData* md, std::ostream* os, int index) if(md->ports[port] == SI_GAMEPAD) dumpJoy(os, joysticks[port]); else if(md->ports[port] == SI_ZAPPER) - *os << setw(3) << setfill('0') << (int)zappers[port].x << ' ' << setw(3) << setfill('0') << (int)zappers[port].y << setw(1) << ' ' << (int)zappers[port].b << ' ' << (int)zappers[port].bogo << ' ' << zappers[port].zaphit; + { + putdec(os,zappers[port].x); os->put(' '); + putdec(os,zappers[port].y); os->put(' '); + putdec(os,zappers[port].b); os->put(' '); + putdec(os,zappers[port].bogo); os->put(' '); + putdec(os,zappers[port].zaphit); + } } os->put('|'); } diff --git a/src/utils/xstring.cpp b/src/utils/xstring.cpp index 18c41ef1..a2bd38a6 100644 --- a/src/utils/xstring.cpp +++ b/src/utils/xstring.cpp @@ -463,22 +463,6 @@ char *U8ToHexStr(uint8 a) return TempArray; } -unsigned int uintDecFromIstream(std::istream* is) -{ - unsigned int ret = 0; - - for(;;) - { - int d = is->get() - '0'; - if(d<0 || d>9) - break; - ret *= 10; - ret += d; - } - is->unget(); - return ret; -} - std::string stditoa(int n) { char tempbuf[16]; @@ -486,4 +470,3 @@ std::string stditoa(int n) return tempbuf; } - diff --git a/src/utils/xstring.h b/src/utils/xstring.h index 2a355885..0df73964 100644 --- a/src/utils/xstring.h +++ b/src/utils/xstring.h @@ -58,4 +58,51 @@ char *U16ToHexStr(uint16 a); std::string stditoa(int n); //extracts a decimal uint from an istream -unsigned int uintDecFromIstream(std::istream* is); +template T templateIntegerDecFromIstream(std::istream* is) +{ + unsigned int ret = 0; + bool pre = true; + + for(;;) + { + int d = is->get() - '0'; + if((d<0 || d>9)) + { + if(!pre) + break; + } + else + { + pre = false; + ret *= 10; + ret += d; + } + } + is->unget(); + return ret; +} + +inline uint32 uint32DecFromIstream(std::istream* is) { return templateIntegerDecFromIstream(is); } +inline uint64 uint64DecFromIstream(std::istream* is) { return templateIntegerDecFromIstream(is); } + +//puts an optionally 0-padded decimal integer of type T into the ostream (0-padding is quicker) +template void putdec(std::ostream* os, T dec) +{ + char temp[DIGITS]; + int ctr = 0; + for(int i=0;iwrite(temp+DIGITS-ctr-1,ctr+1); + else + os->write(temp,DIGITS); +} \ No newline at end of file