2003-08-10 20:49:13 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2005-01-01 16:09:25 +00:00
|
|
|
* Copyright (C) 2002-2005 The ScummVM project
|
2003-08-10 20:49:13 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-06-24 15:23:51 +00:00
|
|
|
#include "common/stdafx.h"
|
2003-08-10 20:49:13 +00:00
|
|
|
#include "common/util.h"
|
2004-11-27 00:26:11 +00:00
|
|
|
#include "common/config-manager.h"
|
2003-10-12 13:27:42 +00:00
|
|
|
#include "common/savefile.h"
|
2003-08-10 20:49:13 +00:00
|
|
|
|
2004-04-17 09:57:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2003-11-30 00:35:12 +00:00
|
|
|
#ifdef USE_ZLIB
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
2005-05-10 23:17:38 +00:00
|
|
|
namespace Common {
|
|
|
|
|
2004-11-27 00:26:11 +00:00
|
|
|
const char *SaveFileManager::getSavePath() const {
|
|
|
|
|
|
|
|
#if defined(__PALM_OS__)
|
|
|
|
return SCUMMVM_SAVEPATH;
|
|
|
|
#else
|
|
|
|
|
|
|
|
const char *dir = NULL;
|
|
|
|
|
2004-12-30 21:48:22 +00:00
|
|
|
// Try to use game specific savepath from config
|
|
|
|
dir = ConfMan.get("savepath").c_str();
|
|
|
|
|
|
|
|
// Work around a bug (#999122) in the original 0.6.1 release of
|
|
|
|
// ScummVM, which would insert a bad savepath value into config files.
|
|
|
|
if (0 == strcmp(dir, "None")) {
|
|
|
|
ConfMan.removeKey("savepath", ConfMan.getActiveDomain());
|
|
|
|
ConfMan.flushToDisk();
|
2004-11-27 00:26:11 +00:00
|
|
|
dir = ConfMan.get("savepath").c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
if (dir[0] == 0)
|
2004-12-11 00:39:27 +00:00
|
|
|
dir = ConfMan.get("path").c_str();
|
2004-11-27 00:26:11 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
assert(dir);
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-11-30 00:35:12 +00:00
|
|
|
class StdioSaveFile : public SaveFile {
|
|
|
|
private:
|
|
|
|
FILE *fh;
|
|
|
|
public:
|
2005-04-13 18:36:55 +00:00
|
|
|
StdioSaveFile(const char *filename, bool saveOrLoad) {
|
|
|
|
fh = ::fopen(filename, (saveOrLoad? "wb" : "rb"));
|
|
|
|
}
|
|
|
|
~StdioSaveFile() {
|
2005-05-08 21:49:52 +00:00
|
|
|
if (fh)
|
2005-04-13 18:36:55 +00:00
|
|
|
::fclose(fh);
|
|
|
|
}
|
2003-11-30 00:35:12 +00:00
|
|
|
|
2005-04-22 17:40:09 +00:00
|
|
|
bool eos() const { return feof(fh) != 0; }
|
|
|
|
bool ioFailed() const { return ferror(fh) != 0; }
|
|
|
|
void clearIOFailed() { clearerr(fh); }
|
|
|
|
|
2003-11-30 00:35:12 +00:00
|
|
|
bool isOpen() const { return fh != 0; }
|
|
|
|
|
2005-05-05 15:59:24 +00:00
|
|
|
uint32 read(void *dataPtr, uint32 dataSize) {
|
|
|
|
return ::fread(dataPtr, 1, dataSize, fh);
|
2005-04-13 18:36:55 +00:00
|
|
|
}
|
2005-05-05 15:59:24 +00:00
|
|
|
uint32 write(const void *dataPtr, uint32 dataSize) {
|
|
|
|
return ::fwrite(dataPtr, 1, dataSize, fh);
|
2005-04-13 18:36:55 +00:00
|
|
|
}
|
2005-05-08 23:32:31 +00:00
|
|
|
|
|
|
|
void skip(uint32 offset) {
|
2005-05-08 23:45:17 +00:00
|
|
|
::fseek(fh, offset, SEEK_CUR);
|
2005-05-08 23:32:31 +00:00
|
|
|
}
|
2003-11-30 00:35:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef USE_ZLIB
|
|
|
|
class GzipSaveFile : public SaveFile {
|
|
|
|
private:
|
|
|
|
gzFile fh;
|
2005-04-13 18:36:55 +00:00
|
|
|
bool _ioError;
|
2003-11-30 00:35:12 +00:00
|
|
|
public:
|
2005-04-13 18:36:55 +00:00
|
|
|
GzipSaveFile(const char *filename, bool saveOrLoad) {
|
|
|
|
_ioError = false;
|
|
|
|
fh = ::gzopen(filename, (saveOrLoad? "wb" : "rb"));
|
|
|
|
}
|
|
|
|
~GzipSaveFile() {
|
2005-05-08 21:49:52 +00:00
|
|
|
if (fh)
|
2005-04-13 18:36:55 +00:00
|
|
|
::gzclose(fh);
|
|
|
|
}
|
2003-11-30 00:35:12 +00:00
|
|
|
|
2005-04-22 17:40:09 +00:00
|
|
|
bool eos() const { return gzeof(fh) != 0; }
|
|
|
|
bool ioFailed() const { return _ioError; }
|
|
|
|
void clearIOFailed() { _ioError = false; }
|
|
|
|
|
2003-11-30 00:35:12 +00:00
|
|
|
bool isOpen() const { return fh != 0; }
|
|
|
|
|
2005-05-05 15:59:24 +00:00
|
|
|
uint32 read(void *dataPtr, uint32 dataSize) {
|
|
|
|
int ret = ::gzread(fh, dataPtr, dataSize);
|
2005-04-13 18:36:55 +00:00
|
|
|
if (ret <= -1)
|
|
|
|
_ioError = true;
|
|
|
|
return ret;
|
2003-11-30 00:35:12 +00:00
|
|
|
}
|
2005-05-05 15:59:24 +00:00
|
|
|
uint32 write(const void *dataPtr, uint32 dataSize) {
|
2003-11-30 00:35:12 +00:00
|
|
|
// 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
|
2005-05-05 15:59:24 +00:00
|
|
|
int ret = ::gzwrite(fh, const_cast<void *>(dataPtr), dataSize);
|
2005-04-13 18:36:55 +00:00
|
|
|
if (ret <= 0)
|
|
|
|
_ioError = true;
|
|
|
|
return ret;
|
2003-11-30 00:35:12 +00:00
|
|
|
}
|
2005-05-08 23:32:31 +00:00
|
|
|
|
|
|
|
void skip(uint32 offset) {
|
2005-05-08 23:45:17 +00:00
|
|
|
::gzseek(fh, offset, SEEK_CUR);
|
2005-05-08 23:32:31 +00:00
|
|
|
}
|
2003-11-30 00:35:12 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-06-25 22:11:48 +00:00
|
|
|
static void join_paths(const char *filename, const char *directory,
|
2003-11-08 22:43:46 +00:00
|
|
|
char *buf, int bufsize) {
|
2003-08-10 20:49:13 +00:00
|
|
|
buf[bufsize-1] = '\0';
|
|
|
|
strncpy(buf, directory, bufsize-1);
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
// Fix for Win98 issue related with game directory pointing to root drive ex. "c:\"
|
|
|
|
if ((buf[0] != 0) && (buf[1] == ':') && (buf[2] == '\\') && (buf[3] == 0)) {
|
|
|
|
buf[2] = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const int dirLen = strlen(buf);
|
|
|
|
|
|
|
|
if (dirLen > 0) {
|
2005-05-09 21:21:21 +00:00
|
|
|
#if defined(__MORPHOS__) || defined(__amigaos4__)
|
2003-08-10 20:49:13 +00:00
|
|
|
if (buf[dirLen-1] != ':' && buf[dirLen-1] != '/')
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(__GP32__) && !defined(__PALM_OS__)
|
|
|
|
strncat(buf, "/", bufsize-1); // prevent double /
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
strncat(buf, filename, bufsize-1);
|
|
|
|
}
|
2003-11-30 00:35:12 +00:00
|
|
|
|
2005-04-10 15:13:40 +00:00
|
|
|
OutSaveFile *DefaultSaveFileManager::openForSaving(const char *filename) {
|
2004-06-25 22:11:48 +00:00
|
|
|
char buf[256];
|
2004-11-27 00:26:11 +00:00
|
|
|
join_paths(filename, getSavePath(), buf, sizeof(buf));
|
2005-04-10 15:13:40 +00:00
|
|
|
return makeSaveFile(buf, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
InSaveFile *DefaultSaveFileManager::openForLoading(const char *filename) {
|
|
|
|
char buf[256];
|
|
|
|
join_paths(filename, getSavePath(), buf, sizeof(buf));
|
|
|
|
return makeSaveFile(buf, false);
|
2004-06-25 22:11:48 +00:00
|
|
|
}
|
|
|
|
|
2004-11-27 00:26:11 +00:00
|
|
|
void DefaultSaveFileManager::listSavefiles(const char * /* prefix */, bool *marks, int num) {
|
2004-06-25 22:11:48 +00:00
|
|
|
memset(marks, true, num * sizeof(bool));
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveFile *DefaultSaveFileManager::makeSaveFile(const char *filename, bool saveOrLoad) {
|
2003-11-30 00:43:01 +00:00
|
|
|
#ifdef USE_ZLIB
|
2005-04-10 15:13:40 +00:00
|
|
|
GzipSaveFile *sf = new GzipSaveFile(filename, saveOrLoad);
|
2003-11-30 00:43:01 +00:00
|
|
|
#else
|
2005-04-10 15:13:40 +00:00
|
|
|
StdioSaveFile *sf = new StdioSaveFile(filename, saveOrLoad);
|
2003-11-30 00:43:01 +00:00
|
|
|
#endif
|
2005-04-10 15:13:40 +00:00
|
|
|
if (!sf->isOpen()) {
|
|
|
|
delete sf;
|
|
|
|
sf = 0;
|
|
|
|
}
|
|
|
|
return sf;
|
2003-11-30 00:35:12 +00:00
|
|
|
}
|
2005-05-10 23:17:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // End of namespace Common
|