From 316ba2e6a9ed8a3856ff216017ad8d5025b6314e Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 18 Nov 2010 17:31:12 +0000 Subject: DS: Replace GBAMPSaveFile by DSFileStream + wrapBufferedWriteStream svn-id: r54332 --- backends/platform/ds/arm9/source/gbampsave.cpp | 172 +++++-------------------- backends/platform/ds/arm9/source/gbampsave.h | 50 +------ 2 files changed, 35 insertions(+), 187 deletions(-) (limited to 'backends/platform/ds/arm9/source') diff --git a/backends/platform/ds/arm9/source/gbampsave.cpp b/backends/platform/ds/arm9/source/gbampsave.cpp index dde711e1f3..ea2e7e01de 100644 --- a/backends/platform/ds/arm9/source/gbampsave.cpp +++ b/backends/platform/ds/arm9/source/gbampsave.cpp @@ -31,161 +31,53 @@ #include "backends/fs/ds/ds-fs.h" #include "common/config-manager.h" -///////////////////////// -// GBAMP Save File -///////////////////////// - -GBAMPSaveFile::GBAMPSaveFile(char *name, bool saveOrLoad) { - handle = DS::std_fopen(name, saveOrLoad? "w": "r"); -// consolePrintf("%s handle is %d\n", name, handle); -// consolePrintf("Created %s\n", name); - bufferPos = 0; - saveSize = 0; - flushed = 0; -} - -GBAMPSaveFile::~GBAMPSaveFile() { - flushSaveBuffer(); - if (handle) - DS::std_fclose((FILE *)handle); -// consolePrintf("Closed file\n"); -} - -uint32 GBAMPSaveFile::read(void *buf, uint32 length) { - saveSize += length; -// consolePrintf("Read %d %d ", length, saveSize); - return DS::std_fread(buf, 1, length, (FILE *)handle); -} - -bool GBAMPSaveFile::eos() const { - return DS::std_feof((FILE *)handle); -} - -bool GBAMPSaveFile::skip(uint32 bytes) { - return DS::std_fseek((FILE *)handle, bytes, SEEK_CUR) == 0; -} - -void GBAMPSaveFile::flushSaveBuffer() { - if (bufferPos != 0) { -// consolePrintf("Flushing %d bytes from %x\n", bufferPos, buffer); - flushed += bufferPos; - DS::std_fwrite(buffer, 1, bufferPos, (FILE *)handle); - bufferPos = 0; - } -} - -int32 GBAMPSaveFile::pos() const { - return DS::std_ftell((FILE *)handle); -} - -int32 GBAMPSaveFile::size() const { - int position = pos(); - DS::std_fseek((FILE *)handle, 0, SEEK_END); - int length = DS::std_ftell((FILE *)handle); - DS::std_fseek((FILE *)handle, position, SEEK_SET); - return length; -} - -bool GBAMPSaveFile::seek(int32 newPos, int whence) { - return DS::std_fseek((FILE *)handle, newPos, whence) == 0; -} - - -uint32 GBAMPSaveFile::write(const void *buf, uint32 length) { - if (bufferPos + length > SAVE_BUFFER_SIZE) { - flushSaveBuffer(); - saveSize += length; -// consolePrintf("Writing %d bytes from %x", length, buf); -// DS::std_fwrite(buf, 1, length, handle); - memcpy(buffer + bufferPos, buf, length); - bufferPos += length; - - saveSize += length; - - -/* int pos = 0; - - int rest = SAVE_BUFFER_SIZE - bufferPos; - memcpy(buffer + bufferPos, buf, rest); - bufferPos = 512; - pos += rest; - flushSaveBuffer(); - length -= rest; -// consolePrintf("First section: %d\n", rest); - - while (length >= 512) { - DS::std_fwrite(((char *) (buf)) + pos, 1, 512, handle); - length -= 512; - pos += 512; -// consolePrintf("Full chunk, %d left ", length); - } +#define SAVE_BUFFER_SIZE 100000 - bufferPos = 0; - memcpy(buffer + bufferPos, ((char *) (buf)) + pos, length); - bufferPos += length; -// consolePrintf("%d left in buffer ", bufferPos);*/ - - } else { - - memcpy(buffer + bufferPos, buf, length); - bufferPos += length; - - saveSize += length; - } - -// if ((length > 100) || (length <= 0)) consolePrintf("Write %d bytes\n", length); - return length; +// This method copied from an old version of the savefile.cpp, since it's been removed from there and +// placed in default-saves.cpp, where I cannot call it. +// FIXME: Does it even make sense to change the "savepath" on the NDS? Considering +// that nothing sets a default value for the "savepath" either, wouldn't it better +// to return a fixed path here? +static Common::String getSavePath() { + // Try to use game specific savepath from config + return ConfMan.get("savepath"); } - ////////////////////////// // GBAMP Save File Manager ////////////////////////// -GBAMPSaveFileManager::GBAMPSaveFileManager() { - +Common::OutSaveFile *GBAMPSaveFileManager::openForSaving(const Common::String &filename) { + Common::String fileSpec = getSavePath(); + if (fileSpec.lastChar() != '/') + fileSpec += '/'; + fileSpec += filename; + +// consolePrintf("Opening the file: %s\n", fileSpec.c_str()); + + Common::WriteStream *stream = DS::DSFileStream::makeFromPath(fileSpec, true); + // Use a write buffer + stream = Common::wrapBufferedWriteStream(stream, SAVE_BUFFER_SIZE, DisposeAfterUse::YES); + return stream; } -GBAMPSaveFileManager::~GBAMPSaveFileManager() { +Common::InSaveFile *GBAMPSaveFileManager::openForLoading(const Common::String &filename) { + Common::String fileSpec = getSavePath(); + if (fileSpec.lastChar() != '/') + fileSpec += '/'; + fileSpec += filename; +// consolePrintf("Opening the file: %s\n", fileSpec.c_str()); + + return DS::DSFileStream::makeFromPath(fileSpec, false); } -GBAMPSaveFile *GBAMPSaveFileManager::openSavefile(const char *name, bool saveOrLoad) { - char fileSpec[128]; - strcpy(fileSpec, getSavePath()); - - if (fileSpec[strlen(fileSpec) - 1] == '/') { - sprintf(fileSpec, "%s%s", getSavePath(), name); - } else { - sprintf(fileSpec, "%s/%s", getSavePath(), name); - } - -// consolePrintf("Opening the file: %s\n", fileSpec); - GBAMPSaveFile *sf = new GBAMPSaveFile(fileSpec, saveOrLoad); - if (sf->isOpen()) { -// consolePrintf("Ok"); - return sf; - } else { -// consolePrintf("Fail"); - delete sf; - return NULL; - } +bool GBAMPSaveFileManager::removeSavefile(const Common::String &filename) { + return false; // TODO: Implement this } -// This method copied from an old version of the savefile.cpp, since it's been removed from there and -// placed in default-saves.cpp, where I cannot call it. -// FIXME: Does it even make sense to change the "savepath" on the NDS? Considering -// that nothing sets a default value for the "savepath" either, wouldn't it better -// to return a fixed path here? -const char *GBAMPSaveFileManager::getSavePath() const { - // Try to use game specific savepath from config - const char *dir = ConfMan.get("savepath").c_str(); - assert(dir); - - return dir; -} Common::StringArray GBAMPSaveFileManager::listSavefiles(const Common::String &pattern) { @@ -194,7 +86,7 @@ Common::StringArray GBAMPSaveFileManager::listSavefiles(const Common::String &pa { char dir[128]; - strcpy(dir, getSavePath()); + strcpy(dir, getSavePath().c_str()); char *realName = dir; if ((strlen(dir) >= 4) && (dir[0] == 'm') && (dir[1] == 'p') && (dir[2] == ':') && (dir[3] == '/')) { diff --git a/backends/platform/ds/arm9/source/gbampsave.h b/backends/platform/ds/arm9/source/gbampsave.h index 9eb551dea6..caac0c0b28 100644 --- a/backends/platform/ds/arm9/source/gbampsave.h +++ b/backends/platform/ds/arm9/source/gbampsave.h @@ -26,59 +26,15 @@ #ifndef _GBAMPSAVE_H_ #define _GBAMPSAVE_H_ -#include "common/system.h" #include "common/savefile.h" -#define SAVE_BUFFER_SIZE 100000 - -class GBAMPSaveFile : public Common::InSaveFile, public Common::OutSaveFile { - void *handle; - char buffer[SAVE_BUFFER_SIZE]; - int bufferPos; - int saveSize; - int flushed; - -public: - GBAMPSaveFile(char *name, bool saveOrLoad); - virtual ~GBAMPSaveFile(); - - virtual uint32 read(void *buf, uint32 size); - virtual uint32 write(const void *buf, uint32 size); - - virtual bool eos() const; - virtual bool skip(uint32 bytes); - - virtual int32 pos() const; - virtual int32 size() const; - virtual bool seek(int32 pos, int whence); - - void flushSaveBuffer(); - - virtual bool isOpen() const { - return handle != 0; - } -}; - - class GBAMPSaveFileManager : public Common::SaveFileManager { public: - GBAMPSaveFileManager(); - ~GBAMPSaveFileManager(); - -// static GBAMPSaveFileManager *instance() { return instancePtr; } + virtual Common::OutSaveFile *openForSaving(const Common::String &filename); + virtual Common::InSaveFile *openForLoading(const Common::String &filename); - GBAMPSaveFile *openSavefile(const char *filename, bool saveOrLoad); - - virtual Common::OutSaveFile *openForSaving(const Common::String &filename) { return openSavefile(filename.c_str(), true); } - virtual Common::InSaveFile *openForLoading(const Common::String &filename) { return openSavefile(filename.c_str(), false); } - - virtual bool removeSavefile(const Common::String &filename) { return false; } // TODO: Implement this + virtual bool removeSavefile(const Common::String &filename); virtual Common::StringArray listSavefiles(const Common::String &pattern); - - void deleteFile(const Common::String &name); - void listFiles(); - - const char *getSavePath() const; }; #endif -- cgit v1.2.3