00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef HAVE_CONFIG_H
00026 #include <config.h>
00027 #endif
00028
00029 #include <wx/wxprec.h>
00030
00031 #ifndef WX_PRECOMP
00032 #include <wx/wx.h>
00033 #endif
00034
00035 #include <fstream>
00036 #include <cstring>
00037
00038 #include "SRAMFile.hh"
00039
00040 using namespace emuWorks;
00041
00042 SRAMFile::SRAMFile(wxString &filename) {
00043 current = -1;
00044 load(filename);
00045 }
00046
00047 SRAMFile::~SRAMFile() {
00048 delete file;
00049 delete data;
00050 delete games[0];
00051 delete games[1];
00052 delete games[2];
00053 }
00054
00055 bool SRAMFile::isModified() {
00056 if (current == -1) {
00057 return false;
00058 }
00059
00060 for (int game = 0; game < 3; game++) {
00061 if (games[game]->isModified()) {
00062 return true;
00063 }
00064 }
00065
00066 return false;
00067 }
00068
00069 SaveSlot *SRAMFile::getCurrentGame() {
00070 if (current == -1) {
00071 return 0;
00072 }
00073
00074 return games[current];
00075 }
00076
00077 bool SRAMFile::setCurrentGame(unsigned int current) {
00078 if (current > 2) {
00079 return false;
00080 }
00081
00082 if (!isValidGame(current)) {
00083 return false;
00084 }
00085
00086 this->current = current;
00087 return true;
00088 }
00089
00090 bool SRAMFile::isValidGame(int game) {
00091 return games[game]->isValid();
00092 }
00093
00094 bool SRAMFile::save() {
00095 return save(*file);
00096 }
00097
00098 bool SRAMFile::save(wxString &filename) {
00099 for (int game = 0; game < 3; game++) {
00100 if (isValidGame(game)) {
00101 memcpy((data + GAME_OFFSET + (game * GAME_SIZE)),
00102 games[game]->nvram,
00103 GAME_SIZE);
00104 }
00105 }
00106
00107 std::ofstream out(filename.mb_str(), std::ios::binary | std::ios::out);
00108
00109 if (!out) {
00110 wxMessageBox(wxT("Unable to open the SRAM file."),
00111 wxT("File Open Error"), wxOK | wxICON_ERROR);
00112
00113 return false;
00114 }
00115
00116 out.write(data, SRAM_SIZE);
00117
00118 if (out.rdstate() & std::ios::failbit) {
00119 wxMessageBox(wxT("Unable to write to the SRAM file."),
00120 wxT("File I/O error"), wxOK | wxICON_ERROR);
00121
00122 out.close();
00123 return false;
00124 }
00125
00126 out.close();
00127
00128 games[0]->setModified(false);
00129 games[1]->setModified(false);
00130 games[2]->setModified(false);
00131
00132 return true;
00133 }
00134
00135 void SRAMFile::load(wxString &filename) {
00136 std::ifstream in(filename.mb_str(), std::ios::in | std::ios::binary);
00137
00138 if (!in) {
00139 wxMessageBox(wxT("Unable to open the SRAM file."),
00140 wxT("File Open Error"), wxOK | wxICON_ERROR);
00141 return;
00142 }
00143
00144 data = new char[SRAM_SIZE];
00145 in.read(data, SRAM_SIZE);
00146
00147 if (in.rdstate() & std::ios::failbit) {
00148 wxMessageBox(wxT("Unable to read the SRAM file."),
00149 wxT("File I/O Error"), wxOK | wxICON_ERROR);
00150
00151 in.close();
00152 delete data;
00153
00154 return;
00155 }
00156
00157 in.close();
00158
00159 for (int slot = 2; slot >= 0; slot--) {
00160 games[slot] = new SaveSlot(data + GAME_OFFSET + (slot * GAME_SIZE));
00161
00162 if (games[slot]->isValid()) {
00163 current = slot;
00164 }
00165 }
00166
00167 file = new wxString(filename);
00168
00169 if (current != -1) {
00170 wxString bakfile = filename + ".bak";
00171 std::ofstream out(bakfile.mb_str(), std::ios::out | std::ios::binary);
00172
00173 if (out) {
00174 out.write(data, SRAM_SIZE);
00175 out.close();
00176 }
00177 }
00178 }