Update recent commit to use C++ functionality (unique_ptr and streams).

- fixes compile error in Linux and some libretro builds
- fixes issue #485
This commit is contained in:
Stephen Anthony 2019-05-31 11:38:19 -02:30
parent 67db29e826
commit 98cb5a4ecd
5 changed files with 24 additions and 34 deletions

View File

@ -223,7 +223,6 @@ uInt32 RewindManager::windStates(uInt32 numStates, bool unwind)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string RewindManager::saveAllStates()
{
uInt8* buffer = NULL;
try
{
ostringstream buf;
@ -231,15 +230,9 @@ string RewindManager::saveAllStates()
<< myOSystem.console().properties().get(PropType::Cart_Name)
<< ".sta";
// Truncate existing file to 0
FILE* fp;
errno_t err = fopen_s(&fp, buf.str().c_str(), "w");
// Make sure the file can be opened for writing
if (err != NULL)
Serializer out(buf.str(), Serializer::Mode::ReadWriteTrunc);
if (!out)
return "Can't save to all states file";
fclose(fp);
Serializer out(buf.str());
int numStates = rewindStates(1000) + 1;
// Save header
@ -248,7 +241,7 @@ string RewindManager::saveAllStates()
out.putShort(numStates);
out.putInt(myStateSize);
buffer = new uInt8[myStateSize];
unique_ptr<uInt8[]> buffer = make_unique<uInt8[]>(myStateSize);
for (int i = 0; i < numStates; i++)
{
RewindState& state = myStateList.current();
@ -256,15 +249,14 @@ string RewindManager::saveAllStates()
// Rewind Serializer internal buffers
s.rewind();
// Save state
s.getByteArray(buffer, myStateSize);
out.putByteArray(buffer, myStateSize);
s.getByteArray(buffer.get(), myStateSize);
out.putByteArray(buffer.get(), myStateSize);
out.putString(state.message);
out.putLong(state.cycles);
if (i < numStates)
unwindStates(1);
}
delete[] buffer;
buf.str("");
buf << "Saved " << numStates << " states";
@ -272,17 +264,13 @@ string RewindManager::saveAllStates()
}
catch (...)
{
if (buffer)
delete[] buffer;
return "Error loading all states";
return "Error saving all states";
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string RewindManager::loadAllStates()
{
uInt8* buffer = NULL;
try
{
ostringstream buf;
@ -291,7 +279,7 @@ string RewindManager::loadAllStates()
<< ".sta";
// Make sure the file can be opened for reading
Serializer in(buf.str(), true);
Serializer in(buf.str(), Serializer::Mode::ReadOnly);
if (!in)
return "Can't load from all states file";
@ -306,7 +294,7 @@ string RewindManager::loadAllStates()
numStates = in.getShort();
myStateSize = in.getInt();
buffer = new uInt8[myStateSize];
unique_ptr<uInt8[]> buffer = make_unique<uInt8[]>(myStateSize);
for (int i = 0; i < numStates; i++)
{
if (myStateList.full())
@ -321,12 +309,11 @@ string RewindManager::loadAllStates()
s.rewind();
// Fill new state with saved values
in.getByteArray(buffer, myStateSize);
s.putByteArray(buffer, myStateSize);
in.getByteArray(buffer.get(), myStateSize);
s.putByteArray(buffer.get(), myStateSize);
state.message = in.getString();
state.cycles = in.getLong();
}
delete[] buffer;
// initialize current state (parameters ignored)
loadState(0, 0);
@ -337,10 +324,7 @@ string RewindManager::loadAllStates()
}
catch (...)
{
if (buffer)
delete[] buffer;
return "Error saving all states";
return "Error loading all states";
}
}

View File

@ -212,7 +212,7 @@ void StateManager::loadState(int slot)
<< ".st" << slot;
// Make sure the file can be opened in read-only mode
Serializer in(buf.str(), true);
Serializer in(buf.str(), Serializer::Mode::ReadOnly);
if(!in)
{
buf.str("");

View File

@ -494,7 +494,7 @@ void CartridgeCTY::updateTune()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void CartridgeCTY::loadScore(uInt8 index)
{
Serializer serializer(myEEPROMFile, true);
Serializer serializer(myEEPROMFile, Serializer::Mode::ReadOnly);
if(serializer)
{
uInt8 scoreRAM[256];

View File

@ -22,10 +22,10 @@ using std::ios;
using std::ios_base;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Serializer::Serializer(const string& filename, bool readonly)
Serializer::Serializer(const string& filename, Mode m)
: myStream(nullptr)
{
if(readonly)
if(m == Mode::ReadOnly)
{
FilesystemNode node(filename);
if(node.isFile() && node.isReadable())
@ -51,7 +51,10 @@ Serializer::Serializer(const string& filename, bool readonly)
fstream temp(filename, ios::out | ios::app);
temp.close();
unique_ptr<fstream> str = make_unique<fstream>(filename, ios::in | ios::out | ios::binary);
ios_base::openmode stream_mode = ios::in | ios::out | ios::binary;
if(m == Mode::ReadWriteTrunc)
stream_mode |= ios::trunc;
unique_ptr<fstream> str = make_unique<fstream>(filename, stream_mode);
if(str && str->is_open())
{
myStream = std::move(str);
@ -222,7 +225,7 @@ void Serializer::putDouble(double value)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Serializer::putString(const string& str)
{
int len = int(str.length());
uInt32 len = uInt32(str.length());
putInt(len);
myStream->write(str.data(), len);
}

View File

@ -34,6 +34,9 @@
*/
class Serializer
{
public:
enum class Mode { ReadOnly, ReadWrite, ReadWriteTrunc };
public:
/**
Creates a new Serializer device for streaming binary data.
@ -46,7 +49,7 @@ class Serializer
The valid() method must immediately be called to verify the stream
was correctly initialized.
*/
Serializer(const string& filename, bool readonly = false);
Serializer(const string& filename, Mode m = Mode::ReadWrite);
Serializer();
public: