File: FileSystem.cpp

package info (click to toggle)
caveexpress 2.4+git20160609-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 48,824 kB
  • sloc: cpp: 66,239; ansic: 1,135; sh: 471; xml: 186; python: 74; makefile: 20
file content (225 lines) | stat: -rw-r--r-- 6,187 bytes parent folder | download | duplicates (2)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "FileSystem.h"
#include "common/Log.h"
#include "common/System.h"
#include "common/Application.h"
#include "../game/GameRegistry.h"
#include <SDL.h>
#include <SDL_platform.h>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

#if defined(__EMSCRIPTEN__) || defined(__NACL__)
// use a pregenerated header with the files
#define DIRLIST_NOT_SUPPORTED 1
#endif

FileSystem::FileSystem () :
		_homeDir(System.getHomeDirectory()), _dataDir("base/" + Singleton<Application>::getInstance().getName() + "/"), _mapsDir("maps/"), _campaignsDir("campaigns/"), _texturesDir(
				"textures/"), _picsDir("pics/"), _soundsDir("sounds/"), _musicDir("music/"), _shaderDir("shaders/"), _languageDir("lang/"), _gesturesDir("gestures/")
{
#ifdef PKGDATADIR
	_dataDir = PKGDATADIR;
	if (!string::endsWith(_dataDir, "/")) {
		_dataDir += "/";
	}
	_dataDir += Singleton<Application>::getInstance().getName() + "/";
#endif
	registerURL("maps", getMapsDir());
	registerURL("textures", getTexturesDir());
	registerURL("pics", getPicsDir());
	registerURL("music", getMusicDir());
	registerURL("shaders", getShaderDir());
	registerURL("languages", getLanguageDir());
	registerURL("sounds", getSoundsDir());
	registerURL("campaigns", getCampaignsDir());
	registerURL("gestures", getGesturesDir());
	registerURL("home", _homeDir);
}

FileSystem::~FileSystem ()
{
}

FileSystem& FileSystem::get ()
{
	static FileSystem instance;
	return instance;
}

void FileSystem::shutdown ()
{
}

bool FileSystem::copy (const std::string& src, const std::string& target) const {
	FILE *f = fopen(src.c_str(), "rb");
	if (!f) {
		Log::error(LOG_COMMON, "Opening source file '%s' failed", src.c_str());
		return false;
	}

	FILE* ft = fopen(target.c_str(), "wb");
	if (!ft) {
		fclose(f);
		return false;
	}
	fseek(f, 0, SEEK_END);
	const size_t len = static_cast<size_t>(ftell(f));
	fseek(f, 0, SEEK_SET);

	unsigned char *const buf = (unsigned char *) malloc(len);
	if (fread(buf, 1, len, f) != len) {
		fclose(f);
		fclose(ft);
		free(buf);
		return false;
	}
	fclose(f);

	if (fwrite(buf, 1, len, ft) != len) {
		Log::error(LOG_COMMON, "Opening dest file '%s' failed", target.c_str());
		free(buf);
		fclose(ft);
		return false;
	}

	free(buf);
	fclose(ft);
	return true;
}

bool FileSystem::deleteFile (const std::string& filename) const
{
	const std::string fullPath = _homeDir + filename;
	return remove(fullPath.c_str()) == 0;
}

long FileSystem::writeFile (const std::string& filename, const unsigned char *buf, size_t length, bool overwrite) const
{
	if (filename.empty())
		return -1L;

	const std::string path = _homeDir + filename;
	SDL_RWops *rwops = createRWops(path, "wb");
	File file(rwops, filename);
	if (!overwrite && file.exists()) {
		Log::info(LOG_COMMON, "file already exists: %s", path.c_str());
		return -1L;
	}
	if (!System.mkdir(_homeDir)) {
		Log::error(LOG_COMMON, "could not create directory: %s",  _homeDir.c_str());
		return -1L;
	}
	createDir(file);
	Log::info(LOG_COMMON, "writing file %s", path.c_str());
	return file.write(buf, length);
}

long FileSystem::writeSysFile (const std::string& filename, const unsigned char *buf, size_t length, bool overwrite) const
{
	if (filename.empty())
		return -1L;

	const std::string path = getAbsoluteWritePath() + filename;
	SDL_RWops *rwops = createRWops(path, "wb");
	FilePtr file(new File(rwops, path));
	if (!overwrite && file->exists())
		return -1L;
	const long ret = file->write(buf, length);
	if (ret < 0)
		Log::error(LOG_COMMON, "failed to write file %s", path.c_str());
	else
		Log::info(LOG_COMMON, "wrote file %s of size %li", path.c_str(), ret);
	return ret;
}

bool FileSystem::createDir (const File& file) const
{
	const std::string path = file.getPath();
	return System.mkdir(path);
}

bool FileSystem::exists (const std::string& filename) const
{
	const FilePtr& file(getFile(filename));
	return file->exists();
}

SDL_RWops* FileSystem::createRWops (const std::string& path, const std::string& mode) const
{
	SDL_RWops *rwops = SDL_RWFromFile(path.c_str(), mode.c_str());
	SDL_ClearError();
	return rwops;
}

FilePtr FileSystem::getFile (const std::string& filename) const
{
	if (!getAbsoluteWritePath().empty()) {
		const std::string path = getAbsoluteWritePath() + getDataDir() + filename;
		SDL_RWops *rwopsLocal = createRWops(path);
		if (rwopsLocal != nullptr) {
			FilePtr ptr(new File(rwopsLocal, path));
			return ptr;
		}
	}
	const std::string path = getDataDir() + filename;
	SDL_RWops *rwops = createRWops(path);
	FilePtr ptr(new File(rwops, path));
	return ptr;
}

std::string FileSystem::getDirForURLType (const std::string& type) const
{
	auto i = _schemes.find(type);
	if (i == _schemes.end())
		return "";
	return i->second;
}

void FileSystem::registerURL(const std::string& type, const std::string& dir)
{
	_schemes[type] = dir;
}

FilePtr FileSystem::getFileFromURL (const std::string& filename) const
{
	std::string::size_type pos = filename.find_first_of("://", 0);
	if (pos == std::string::npos) {
		System.exit("Not a valid url: " + filename, 1);
	}

	const std::string& type = filename.substr(0, pos);
	const std::string& dir = getDirForURLType(type);
	if (dir.empty()) {
		System.exit("No type registered for " + type, 1);
	}
	const std::string& name = filename.substr(pos + 3, filename.length());
	return getFile(dir + name);
}

const std::string FileSystem::getAbsoluteWritePath () const
{
	return _homeDir;
}

DirectoryEntries FileSystem::listDirectory (const std::string& basedir, const std::string& subdir) const
{
#if DIRLIST_NOT_SUPPORTED
	return Singleton<GameRegistry>::getInstance().getGame()->listDirectory(basedir, subdir);
#else
	DirectoryEntries entriesAll;
	const std::string sysWritePath = getAbsoluteWritePath() + basedir;
	if (!sysWritePath.empty())
		entriesAll = System.listDirectory(sysWritePath, subdir);

	const std::string dataDir = getDataDir() + basedir;
	const DirectoryEntries entries = System.listDirectory(dataDir, subdir);
	for (const std::string& entry : entries) {
		entriesAll.push_back(entry);
	}
	std::sort(entriesAll.begin(), entriesAll.end());
	entriesAll.erase(std::unique(entriesAll.begin(), entriesAll.end()), entriesAll.end());
	return entriesAll;
#endif
}