Main Page | Namespace List | Class List | File List | Namespace Members | Class Members | File Members

source/model/SRAMFile.cc

Go to the documentation of this file.
00001 /*
00002  * z2se
00003  * Copyright (C) 2004-2005 emuWorks
00004  * http://games.technoplaza.net/
00005  *
00006  * This file is part of z2se.
00007  *
00008  * z2se is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * z2se is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with z2se; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00021  */
00022 
00023 // $Id: SRAMFile.cc,v 1.3 2005/08/04 05:23:21 technoplaza Exp $
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 }

Generated on Thu Aug 4 00:30:33 2005 for Zelda II SRAM Editor by  doxygen 1.4.4