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  * Copyright (C) 2004 emuWorks
00003  * http://games.technoplaza.net/
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018  */
00019 
00020 // $Id: SRAMFile.cc,v 1.2 2004/12/10 10:01:43 technoplaza Exp $
00021 
00022 #ifdef HAVE_CONFIG_H
00023     #include <config.h>
00024 #endif
00025 
00026 #include <wx/wxprec.h>
00027 
00028 #ifndef WX_PRECOMP
00029    #include <wx/wx.h>
00030 #endif
00031 
00032 #include <fstream>
00033 #include <cstring>
00034 
00035 #include "SRAMFile.hh"
00036 
00037 using namespace emuWorks;
00038 
00039 SRAMFile::SRAMFile(wxString &filename) {
00040     current = -1;
00041     load(filename);
00042 }
00043 
00044 SRAMFile::~SRAMFile() {
00045     delete file;
00046     delete data;
00047     delete games[0];
00048     delete games[1];
00049     delete games[2];
00050 }
00051 
00052 bool SRAMFile::isModified() {
00053     if (current == -1) {
00054         return false;
00055     }
00056     
00057     for (int game = 0; game < 3; game++) {
00058         if (games[game]->isModified()) {
00059             return true;
00060         }
00061     }
00062     
00063     return false;
00064 }
00065 
00066 SaveSlot *SRAMFile::getCurrentGame() {
00067     if (current == -1) {
00068         return 0;
00069     }
00070     
00071     return games[current];
00072 }
00073 
00074 bool SRAMFile::setCurrentGame(unsigned int current) {
00075     if (current > 2) {
00076         return false;
00077     }
00078     
00079     if (!isValidGame(current)) {
00080         return false;
00081     }
00082     
00083     this->current = current;
00084     return true;
00085 }
00086 
00087 bool SRAMFile::isValidGame(int game) {
00088     return games[game]->isValid();
00089 }
00090 
00091 bool SRAMFile::save() {
00092     return save(*file);
00093 }
00094 
00095 bool SRAMFile::save(wxString &filename) {
00096     for (int game = 0; game < 3; game++) {
00097         if (isValidGame(game)) {
00098             memcpy((data + GAME_OFFSET + (game * GAME_SIZE)), 
00099                    games[game]->nvram,
00100                    GAME_SIZE);
00101         }
00102     }
00103     
00104     std::ofstream out(filename.mb_str(), std::ios::binary | std::ios::out);
00105     
00106     if (!out) {
00107         wxMessageBox(wxT("Unable to open the SRAM file."),
00108                      wxT("File Open Error"), wxOK | wxICON_ERROR);
00109                      
00110         return false;
00111     }
00112     
00113     out.write(data, SRAM_SIZE);
00114     
00115     if (out.rdstate() & std::ios::failbit) {
00116         wxMessageBox(wxT("Unable to write to the SRAM file."),
00117                      wxT("File I/O error"), wxOK | wxICON_ERROR);
00118                      
00119         out.close();
00120         return false;
00121     }
00122     
00123     out.close();
00124     
00125     games[0]->setModified(false);
00126     games[1]->setModified(false);
00127     games[2]->setModified(false);
00128     
00129     return true;
00130 }
00131 
00132 void SRAMFile::load(wxString &filename) {
00133     std::ifstream in(filename.mb_str(), std::ios::in | std::ios::binary);
00134     
00135     if (!in) {
00136         wxMessageBox(wxT("Unable to open the SRAM file."),
00137                      wxT("File Open Error"), wxOK | wxICON_ERROR);
00138         return;
00139     }
00140     
00141     data = new char[SRAM_SIZE];
00142     in.read(data, SRAM_SIZE);
00143     
00144     if (in.rdstate() & std::ios::failbit) {
00145         wxMessageBox(wxT("Unable to read the SRAM file."),
00146                      wxT("File I/O Error"), wxOK | wxICON_ERROR);
00147         
00148         in.close();
00149         delete data;
00150         
00151         return;
00152     }
00153     
00154     in.close();
00155     
00156     for (int slot = 2; slot >= 0; slot--) {
00157         games[slot] = new SaveSlot(data + GAME_OFFSET + (slot * GAME_SIZE));
00158         
00159         if (games[slot]->isValid()) {
00160             current = slot;
00161         }
00162     }
00163     
00164     file = new wxString(filename);
00165     
00166     if (current != -1) {
00167         wxString bakfile = filename + ".bak";
00168         std::ofstream out(bakfile.mb_str(), std::ios::out | std::ios::binary);
00169         
00170         if (out) {
00171             out.write(data, SRAM_SIZE);
00172             out.close();
00173         }
00174     }
00175 }

Generated on Fri Dec 10 11:33:56 2004 for Zelda II SRAM Editor by  doxygen 1.3.9.1