diff options
| -rw-r--r-- | backends/PalmOS/Src/palmsave.cpp | 31 | ||||
| -rw-r--r-- | backends/dc/vmsave.cpp | 2 | ||||
| -rw-r--r-- | common/savefile.cpp | 71 | 
3 files changed, 80 insertions, 24 deletions
| diff --git a/backends/PalmOS/Src/palmsave.cpp b/backends/PalmOS/Src/palmsave.cpp index 44e4260300..97a3518829 100644 --- a/backends/PalmOS/Src/palmsave.cpp +++ b/backends/PalmOS/Src/palmsave.cpp @@ -32,10 +32,10 @@  class PalmSaveFile : public SaveFile {  public: -	PalmSaveFile(const char *filename, const char *mode); +	PalmSaveFile(const char *filename, bool saveOrLoad);  	~PalmSaveFile(); -	bool is_open() { return file != NULL; } +	bool isOpen() const { return file != NULL; }  protected:  	int fread(void *buf, int size, int cnt);  	int fwrite(const void *buf, int size, int cnt); @@ -47,12 +47,12 @@ private :  	bool _needDump;  }; -PalmSaveFile::PalmSaveFile(const char *filename, const char *mode) { +PalmSaveFile::PalmSaveFile(const char *filename, bool saveOrLoad) {  	_readWriteData = NULL;  	_readWritePos = 0;  	_needDump = false; -	file = ::fopen(filename, mode); +	file = ::fopen(filename, (saveOrLoad ? "wb" : "rb"));  }  PalmSaveFile::~PalmSaveFile() { @@ -97,25 +97,12 @@ int PalmSaveFile::fwrite(const void *buf, int size, int cnt) {  // SaveFileManager class  class PalmSaveFileManager : public SaveFileManager { -  public: -	SaveFile *open_savefile(const char *filename, const char *dirname, bool saveOrLoad);  	void list_savefiles(const char *prefix, const char *directory, bool *marks, int num); -}; - -SaveFile *PalmSaveFileManager::open_savefile(const char *filename, const char *dirname, bool saveOrLoad) { -	char buf[256]; - -	join_paths(filename, dirname, buf, sizeof(buf)); -	PalmSaveFile *sf = new PalmSaveFile(buf, (saveOrLoad? "wb":"rb")); - -	if(!sf->is_open()) { -		delete sf; -		sf = NULL; -	} -	return sf; -} +protected: +	SaveFile *makeSaveFile(const char *filename, bool saveOrLoad); +};  void PalmSaveFileManager::list_savefiles(const char *prefix, const char *directory, bool *marks, int num) {  	FileRef fileRef; @@ -156,6 +143,10 @@ void PalmSaveFileManager::list_savefiles(const char *prefix, const char *directo  	VFSFileClose(fileRef);  } +SaveFile *SaveFileManager::makeSaveFile(const char *filename, bool saveOrLoad) { +	return new PalmSaveFile(filename, saveOrLoad); +} +  // OSystem  SaveFileManager *OSystem_PALMOS::get_savefile_manager() {  	return new PalmSaveFileManager(); diff --git a/backends/dc/vmsave.cpp b/backends/dc/vmsave.cpp index afd2afb277..9bd5e1803c 100644 --- a/backends/dc/vmsave.cpp +++ b/backends/dc/vmsave.cpp @@ -237,6 +237,8 @@ public:    ~VMSave(); +  bool isOpen() const { return true; } +    bool readSaveGame()    { return ::readSaveGame(buffer, size, filename); } diff --git a/common/savefile.cpp b/common/savefile.cpp index 6133c190cb..1e2725f98f 100644 --- a/common/savefile.cpp +++ b/common/savefile.cpp @@ -23,12 +23,22 @@  #include "common/util.h"  #include "common/savefile.h" + +// FIXME HACK +//#define USE_ZLIB + + +#ifdef USE_ZLIB +#include <zlib.h> +#endif +  uint32 SaveFile::read(void *ptr, uint32 size) {  	return fread(ptr, 1, size);  }  byte SaveFile::readByte() {  	byte b; +	// TODO: Proper error handling  	if (fread(&b, 1, 1) != 1)  		return 0;  	return b; @@ -86,14 +96,63 @@ void SaveFile::writeUint32BE(uint32 value) {  	writeUint16BE((uint16)(value & 0xffff));  } + +class StdioSaveFile : public SaveFile { +private: +	FILE *fh; +public: +	StdioSaveFile(const char *filename, bool saveOrLoad) +		{ fh = ::fopen(filename, (saveOrLoad? "wb" : "rb")); } +	~StdioSaveFile() +		{ if(fh) ::fclose(fh); } + +	bool isOpen() const { return fh != 0; } + +protected: +	int fread(void *buf, int size, int cnt) +		{ return ::fread(buf, size, cnt, fh); } +	int fwrite(const void *buf, int size, int cnt) +		{ return ::fwrite(buf, size, cnt, fh); } +}; + + +#ifdef USE_ZLIB +class GzipSaveFile : public SaveFile { +private: +	gzFile fh; +public: +	GzipSaveFile(const char *filename, bool saveOrLoad) +		{ fh = ::gzopen(filename, (saveOrLoad? "wb" : "rb")); } +	~GzipSaveFile() +		{ if(fh) ::gzclose(fh); } + +	bool isOpen() const { return fh != 0; } + +protected: +	int fread(void *buf, int size, int cnt) { +		return ::gzread(fh, buf, size * cnt); +	} +	int fwrite(const void *buf, int size, int cnt) { +		// Due to a "bug" in the zlib headers (or maybe I should say, +		// a bug in the C++ spec? Whatever <g>) we have to be a bit +		// hackish here and remove the const qualifier. +		// Note that gzwrite's buf param is declared as "const voidp" +		// which you might think is the same as "const void *" but it +		// is not - rather it is equal to "void const *" which is the  +		// same as "void *". Hrmpf +		return ::gzwrite(fh, const_cast<void *>(buf), size * cnt); +	} +}; +#endif + +  SaveFile *SaveFileManager::open_savefile(const char *filename, const char *directory, bool saveOrLoad) {  	char buf[256];  	join_paths(filename, directory, buf, sizeof(buf)); -	StdioSaveFile *sf = new StdioSaveFile(buf, -							(saveOrLoad? "wb":"rb")); -	if (!sf->is_open()) { +	SaveFile *sf = makeSaveFile(buf, saveOrLoad); +	if (!sf->isOpen()) {  		delete sf; -		sf = NULL; +		sf = 0;  	}  	return sf;  } @@ -123,3 +182,7 @@ void SaveFileManager::join_paths(const char *filename, const char *directory,  	}  	strncat(buf, filename, bufsize-1);  } + +SaveFile *SaveFileManager::makeSaveFile(const char *filename, bool saveOrLoad) { +	return new StdioSaveFile(filename, saveOrLoad); +} | 
