aboutsummaryrefslogtreecommitdiff
path: root/backends/saves/default/default-saves.cpp
blob: a6adb19112f15a442872bf44a9cf12a9fcf4d794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#if !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)

#include "backends/saves/default/default-saves.h"

#include "common/savefile.h"
#include "common/util.h"
#include "common/fs.h"
#include "common/archive.h"
#include "common/config-manager.h"
#include "common/zlib.h"

#include <errno.h>	// for removeSavefile()


DefaultSaveFileManager::DefaultSaveFileManager() {
}

DefaultSaveFileManager::DefaultSaveFileManager(const Common::String &defaultSavepath) {
	ConfMan.registerDefault("savepath", defaultSavepath);
}


void DefaultSaveFileManager::checkPath(const Common::FSNode &dir) {
	clearError();
	if (!dir.exists()) {
		setError(Common::kPathDoesNotExist, "The savepath '"+dir.getPath()+"' does not exist");
	} else if (!dir.isDirectory()) {
		setError(Common::kPathNotDirectory, "The savepath '"+dir.getPath()+"' is not a directory");
	}
}

Common::StringList DefaultSaveFileManager::listSavefiles(const char *pattern) {
	Common::FSNode savePath(getSavePath());
	checkPath(savePath);
	if (getError() != Common::kNoError)
		return Common::StringList();

	Common::FSDirectory dir(savePath);
	Common::ArchiveMemberList savefiles;
	Common::StringList results;
	Common::String search(pattern);

	if (dir.listMatchingMembers(savefiles, search) > 0) {
		for (Common::ArchiveMemberList::const_iterator file = savefiles.begin(); file != savefiles.end(); ++file) {
			results.push_back((*file)->getName());
		}
	}

	return results;
}

Common::InSaveFile *DefaultSaveFileManager::openForLoading(const char *filename) {
	// Ensure that the savepath is valid. If not, generate an appropriate error.
	Common::FSNode savePath(getSavePath());
	checkPath(savePath);
	if (getError() != Common::kNoError)
		return 0;

	Common::FSNode file = savePath.getChild(filename);
	if (!file.exists())
		return 0;

	// Open the file for reading
	Common::SeekableReadStream *sf = file.createReadStream();

	return Common::wrapCompressedReadStream(sf);
}

Common::OutSaveFile *DefaultSaveFileManager::openForSaving(const char *filename) {
	// Ensure that the savepath is valid. If not, generate an appropriate error.
	Common::FSNode savePath(getSavePath());
	checkPath(savePath);
	if (getError() != Common::kNoError)
		return 0;

	Common::FSNode file = savePath.getChild(filename);

	// Open the file for saving
	Common::WriteStream *sf = file.createWriteStream();

	return Common::wrapCompressedWriteStream(sf);
}

bool DefaultSaveFileManager::removeSavefile(const char *filename) {
	clearError();

	Common::FSNode savePath(getSavePath());
	checkPath(savePath);
	if (getError() != Common::kNoError)
		return false;

	Common::FSNode file = savePath.getChild(filename);

	// FIXME: remove does not exist on all systems. If your port fails to
	// compile because of this, please let us know (scummvm-devel or Fingolfin).
	// There is a nicely portable workaround, too: Make this method overloadable.
	if (remove(file.getPath().c_str()) != 0) {
#ifndef _WIN32_WCE
		if (errno == EACCES)
			setError(Common::kWritePermissionDenied, "Search or write permission denied: "+file.getName());

		if (errno == ENOENT)
			setError(Common::kPathDoesNotExist, "removeSavefile: '"+file.getName()+"' does not exist or path is invalid");
#endif
		return false;
	} else {
		return true;
	}
}

Common::String DefaultSaveFileManager::getSavePath() const {

	Common::String dir;

	// Try to use game specific savepath from config
	dir = ConfMan.get("savepath");

	// 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 (dir == "None") {
		ConfMan.removeKey("savepath", ConfMan.getActiveDomainName());
		ConfMan.flushToDisk();
		dir = ConfMan.get("savepath");
	}

#ifdef _WIN32_WCE
	if (dir.empty())
		dir = ConfMan.get("path");
#endif

	return dir;
}

#endif // !defined(DISABLE_DEFAULT_SAVEFILEMANAGER)