diff --git a/stella/src/emucore/Deserializer.cxx b/stella/src/emucore/Deserializer.cxx new file mode 100644 index 000000000..3d2499926 --- /dev/null +++ b/stella/src/emucore/Deserializer.cxx @@ -0,0 +1,101 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-1998 by Bradford W. Mott +// +// See the file "license" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +// +// $Id: Deserializer.cxx,v 1.1 2002-05-13 19:14:17 stephena Exp $ +//============================================================================ + +#include +#include +#include + +#include "Deserializer.hxx" + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Deserializer::Deserializer(void) +{ + TruePattern = 0xfab1fab2; + FalsePattern = 0xbad1bad2; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Deserializer::~Deserializer(void) +{ + close(); + if(myStream.is_open()) + myStream.close(); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +bool Deserializer::open(string& fileName) +{ + close(); + myStream.open(fileName.c_str(), ios::in | ios::binary); + + return myStream.is_open(); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void Deserializer::close(void) +{ + if(myStream.is_open()) + myStream.close(); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +long Deserializer::getLong(void) +{ + if(myStream.eof()) + throw "Deserializer: end of file"; + + long l; + myStream.read(reinterpret_cast (&l), sizeof (long)); + if(myStream.bad()) + throw "Deserializer: file read failed"; + + return l; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +string Deserializer::getString(void) +{ + long len = getLong(); + string str; + str.resize(len); + myStream.read(&str[0], len); + + if(myStream.bad()) + throw "Deserializer: file read failed"; + + return str; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +bool Deserializer::getBool(void) +{ + bool result = false; + + long b = getLong(); + if(myStream.bad()) + throw "Deserializer: file read failed"; + + if(b == TruePattern) + result = true; + else if(b == FalsePattern) + result = false; + else + throw "Deserializer: data corruption"; + + return result; +} diff --git a/stella/src/emucore/Deserializer.hxx b/stella/src/emucore/Deserializer.hxx new file mode 100644 index 000000000..9e19f36bc --- /dev/null +++ b/stella/src/emucore/Deserializer.hxx @@ -0,0 +1,99 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-1998 by Bradford W. Mott +// +// See the file "license" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +// +// $Id: Deserializer.hxx,v 1.1 2002-05-13 19:14:17 stephena Exp $ +//============================================================================ + +#ifndef DESERIALIZER_HXX +#define DESERIALIZER_HXX + +#include +#include + +/** + This class implements a Deserializer device, whereby data is + deserialized from an input binary file in a system-independent + way. + + All longs should be cast to their appropriate data type upon method + return. + + @author Stephen Anthony + @version $Id: Deserializer.hxx,v 1.1 2002-05-13 19:14:17 stephena Exp $ +*/ +class Deserializer +{ + public: + /** + Creates a new Deserializer device. + + Open must be called with a valid file before this Deserializer can + be used. + */ + Deserializer(void); + + /** + Destructor + */ + virtual ~Deserializer(void); + + public: + /** + Opens the given file for input. Multiple calls to this method + will close previously opened files. + + @param fileName The filename to get the deserialized data from. + @return Result of opening the file. True on success, false on failure + */ + bool open(string& fileName); + + /** + Closes the current input stream. + */ + void close(void); + + /** + Reads a long value from the current input stream. + + @result The long value which has been read from the stream. + */ + long getLong(void); + + /** + Reads a string from the current input stream. + + @result The string which has been read from the stream. + */ + string getString(void); + + /** + Reads a boolean value from the current input stream. + + @result The boolean value which has been read from the stream. + */ + bool getBool(void); + + private: + // The stream to get the deserialized data from. + ifstream myStream; + + // A long pattern that represents a boolean value of true. + long TruePattern; + + // A long pattern that represents a boolean value of false. + long FalsePattern; +}; + +#endif diff --git a/stella/src/emucore/Serializer.cxx b/stella/src/emucore/Serializer.cxx new file mode 100644 index 000000000..145928356 --- /dev/null +++ b/stella/src/emucore/Serializer.cxx @@ -0,0 +1,81 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-1998 by Bradford W. Mott +// +// See the file "license" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +// +// $Id: Serializer.cxx,v 1.1 2002-05-13 19:14:17 stephena Exp $ +//============================================================================ + +#include +#include +#include + +#include "Serializer.hxx" + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Serializer::Serializer(void) +{ + TruePattern = 0xfab1fab2; + FalsePattern = 0xbad1bad2; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Serializer::~Serializer(void) +{ + close(); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +bool Serializer::open(string& fileName) +{ + close(); + myStream.open(fileName.c_str(), ios::out | ios::binary); + + return myStream.is_open(); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void Serializer::close(void) +{ + if(myStream.is_open()) + myStream.close(); +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void Serializer::putLong(long value) +{ + myStream.write(reinterpret_cast (&value), sizeof (long)); + if(myStream.bad()) + throw "Serializer: file write failed"; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void Serializer::putString(string& str) +{ + int len = str.length(); + putLong(len); + myStream.write(str.data(), len); + + if(myStream.bad()) + throw "Serializer: file write failed"; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void Serializer::putBool(bool b) +{ + long l = b ? TruePattern: FalsePattern; + putLong(l); + + if(myStream.bad ()) + throw "Serializer: file write failed"; +} diff --git a/stella/src/emucore/Serializer.hxx b/stella/src/emucore/Serializer.hxx new file mode 100644 index 000000000..6d3f36e5c --- /dev/null +++ b/stella/src/emucore/Serializer.hxx @@ -0,0 +1,100 @@ +//============================================================================ +// +// SSSS tt lll lll +// SS SS tt ll ll +// SS tttttt eeee ll ll aaaa +// SSSS tt ee ee ll ll aa +// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" +// SS SS tt ee ll ll aa aa +// SSSS ttt eeeee llll llll aaaaa +// +// Copyright (c) 1995-1998 by Bradford W. Mott +// +// See the file "license" for information on usage and redistribution of +// this file, and for a DISCLAIMER OF ALL WARRANTIES. +// +// $Id: Serializer.hxx,v 1.1 2002-05-13 19:14:17 stephena Exp $ +//============================================================================ + +#ifndef SERIALIZER_HXX +#define SERIALIZER_HXX + +#include +#include + +/** + This class implements a Serializer device, whereby data is + serialized and sent to an output binary file in a system- + independent way. + + All bytes and integers are written as long's. Strings are + written as characters prepended by the length of the string. + Boolean values are written using a special pattern. + + @author Stephen Anthony + @version $Id: Serializer.hxx,v 1.1 2002-05-13 19:14:17 stephena Exp $ +*/ +class Serializer +{ + public: + /** + Creates a new Serializer device. + + Open must be called with a valid file before this Serializer can + be used. + */ + Serializer(void); + + /** + Destructor + */ + virtual ~Serializer(void); + + public: + /** + Opens the given file for output. Multiple calls to this method + will close previously opened files. + + @param fileName The filename to send the serialized data to. + @return Result of opening the file. True on success, false on failure + */ + bool open(string& fileName); + + /** + Closes the current output stream. + */ + void close(void); + + /** + Writes a long value to the current output stream. + + @param value The long value to write to the output stream. + */ + void putLong(long value); + + /** + Writes a string to the current output stream. + + @param str The string to write to the output stream. + */ + void putString(string& str); + + /** + Writes a boolean value to the current output stream. + + @param b The boolean value to write to the output stream. + */ + void putBool(bool b); + + private: + // The stream to send the serialized data to. + ofstream myStream; + + // A long pattern that represents a boolean value of true. + long TruePattern; + + // A long pattern that represents a boolean value of false. + long FalsePattern; +}; + +#endif