aboutsummaryrefslogtreecommitdiff
path: root/backends/PalmOS/Src/palmsave.cpp
diff options
context:
space:
mode:
authorMax Horn2003-04-30 11:37:10 +0000
committerMax Horn2003-04-30 11:37:10 +0000
commit42f156ddada4c6f51e1612e9b0f18ec7c9dd9cee (patch)
treea2b7ba14f7f919987239902e513c1a646ca2e9f8 /backends/PalmOS/Src/palmsave.cpp
parent79ebda7613b3280ae644d67af50712deef8819d7 (diff)
downloadscummvm-rg350-42f156ddada4c6f51e1612e9b0f18ec7c9dd9cee.tar.gz
scummvm-rg350-42f156ddada4c6f51e1612e9b0f18ec7c9dd9cee.tar.bz2
scummvm-rg350-42f156ddada4c6f51e1612e9b0f18ec7c9dd9cee.zip
started to merge in Chrilith's PalmOS port
svn-id: r7219
Diffstat (limited to 'backends/PalmOS/Src/palmsave.cpp')
-rw-r--r--backends/PalmOS/Src/palmsave.cpp160
1 files changed, 160 insertions, 0 deletions
diff --git a/backends/PalmOS/Src/palmsave.cpp b/backends/PalmOS/Src/palmsave.cpp
new file mode 100644
index 0000000000..d90e23223f
--- /dev/null
+++ b/backends/PalmOS/Src/palmsave.cpp
@@ -0,0 +1,160 @@
+#include <ctype.h>
+#include "common/scummsys.h"
+//#include "common/stdafx.h"
+#include "common/engine.h"
+#include "scumm/saveload.h"
+#include "palm.h"
+//#include "gui/newgui.h"
+//#include "gui/message.h"
+
+#define MAX_BLOCK 64000
+
+// SaveFile class
+
+class PalmSaveFile : public SaveFile {
+public:
+ PalmSaveFile(const char *filename, const char *mode);
+ ~PalmSaveFile();
+
+ bool is_open() { return file != NULL; }
+ int fread(void *buf, int size, int cnt);
+ int fwrite(void *buf, int size, int cnt);
+// must be removed
+ int feof() { return ::feof(file); }
+
+private :
+ FILE *file;
+ UInt8 * _readWriteData;
+ UInt32 _readWritePos;
+ bool _needDump;
+};
+
+PalmSaveFile::PalmSaveFile(const char *filename, const char *mode) {
+ _readWriteData = NULL;
+ _readWritePos = 0;
+ _needDump = false;
+
+ file = ::fopen(filename, mode);
+}
+
+PalmSaveFile::~PalmSaveFile() {
+ if (file) {
+ if (_needDump && _readWriteData) {
+ ::fwrite(_readWriteData, _readWritePos, 1, file);
+ free(_readWriteData);
+ }
+
+ ::fclose(file);
+ }
+}
+
+int PalmSaveFile::fread(void *buf, int size, int cnt) {
+ return ::fread(buf, size, cnt, file);
+}
+
+int PalmSaveFile::fwrite(void *buf, int size, int cnt) {
+ UInt32 fullsize = size*cnt;
+
+ if (fullsize<=MAX_BLOCK)
+ {
+ if (!_readWriteData)
+ _readWriteData = (byte *)malloc(MAX_BLOCK);
+
+ if ((_readWritePos+fullsize)>MAX_BLOCK) {
+ ::fwrite(_readWriteData, _readWritePos, 1, file);
+ _readWritePos = 0;
+ _needDump = false;
+ }
+
+ MemMove(_readWriteData + _readWritePos, buf, fullsize);
+ _readWritePos += fullsize;
+ _needDump = true;
+
+ return cnt;
+ }
+
+ return ::fwrite(buf, size, cnt, file);
+}
+
+// SaveFileManager class
+
+class PalmSaveFileManager : public SaveFileManager {
+
+public:
+/* SaveFile *open_savefile(const char *filename,
+ bool saveOrLoad)
+ {
+ PalmSaveFile *sf = new PalmSaveFile(filename,
+ (saveOrLoad? "wb":"rb"));
+ if(!sf->is_open()) {
+ delete sf;
+ sf = NULL;
+ }
+ return sf;
+ }
+
+ void list_savefiles(const char *prefix,
+ bool *marks, int num)
+ {
+ memset(marks, true, num*sizeof(bool));
+ }
+*/
+ SaveFile *open_savefile(const char *filename, bool saveOrLoad);
+ void list_savefiles(const char *prefix, bool *marks, int num);
+};
+
+SaveFile *PalmSaveFileManager::open_savefile(const char *filename, bool saveOrLoad) {
+ PalmSaveFile *sf = new PalmSaveFile(filename, (saveOrLoad? "wb":"rb"));
+
+ if(!sf->is_open()) {
+ delete sf;
+ sf = NULL;
+ }
+
+ return sf;
+}
+
+void PalmSaveFileManager::list_savefiles(const char *prefix, bool *marks, int num) {
+ FileRef fileRef;
+ // try to open the dir
+ Err e = VFSFileOpen(gVars->volRefNum, SCUMMVM_SAVEPATH, vfsModeRead, &fileRef);
+ memset(marks, false, num*sizeof(bool));
+
+ if (e != errNone)
+ return;
+
+ // enumerate all files
+ Char *nameonly = strrchr(prefix,'/') + 1;
+ UInt32 dirEntryIterator = vfsIteratorStart;
+ Char filename[32];
+ FileInfoType info = {0, filename, 32};
+ UInt16 length = StrLen(nameonly);
+ int slot = 0;
+
+ while (dirEntryIterator != vfsIteratorStop) {
+ e = VFSDirEntryEnumerate (fileRef, &dirEntryIterator, &info);
+
+ if (e != expErrEnumerationEmpty) { // there is something
+
+ if (StrLen(info.nameP) == (length + 2)) { // consider max 99, filename length is ok
+ if (StrNCaselessCompare(nameonly, info.nameP, length) == 0) { // this seems to be a save file
+ if (isdigit(info.nameP[length]) && isdigit(info.nameP[length+1])) {
+
+ slot = StrAToI(filename + length);
+ if (slot >= 0 && slot < num)
+ *(marks+slot) = true;
+
+ }
+ }
+ }
+
+ }
+ }
+
+ VFSFileClose(fileRef);
+}
+
+// OSystem
+SaveFileManager *OSystem_PALMOS::get_savefile_manager() {
+ return new PalmSaveFileManager();
+}