[059f52]: gemrb / core / FileCache.cpp  Maximize  Restore  History

Download this file

87 lines (72 with data), 2.6 kB

 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
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2003 The GemRB Project
*
* 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.
*/
#include "FileCache.h"
#include "Compressor.h"
#include "Interface.h"
#include "PluginMgr.h"
#include "System/FileStream.h"
#include "System/VFS.h"
DataStream* CacheCompressedStream(DataStream *stream, const char* filename, int length, bool overwrite)
{
if (!core->IsAvailable(PLUGIN_COMPRESSION_ZLIB)) {
print( "No Compression Manager Available.\nCannot Load Compressed File.\n" );
return NULL;
}
char fname[_MAX_PATH];
ExtractFileFromPath(fname, filename);
char path[_MAX_PATH];
PathJoin(path, core->CachePath, fname, NULL);
if (overwrite || !file_exists(path)) {
FileStream out;
if (!out.Create(path)) {
printMessage("FileCache", "Cannot write %s.\n", RED, path);
return NULL;
}
PluginHolder<Compressor> comp(PLUGIN_COMPRESSION_ZLIB);
if (comp->Decompress(&out, stream, length) != GEM_OK)
return NULL;
} else {
stream->Seek(length, GEM_CURRENT_POS);
}
return FileStream::OpenFile(path);
}
DataStream* CacheStream(DataStream* src)
{
src->Seek(0, GEM_STREAM_START);
if (!core->SlowBIFs)
return src;
char cachedfile[_MAX_PATH];
PathJoin(cachedfile, core->CachePath, src->filename, NULL);
if (!file_exists(cachedfile)) { // File was not found in cache
FileStream dest;
if (!dest.Create(cachedfile)) {
error("Cache", "CachedFile failed to write to cached file '%s' (from '%s')\n", cachedfile, src->originalfile);
}
size_t blockSize = 1024 * 1000;
char buff[1024 * 1000];
do {
if (blockSize > src->Remains())
blockSize = src->Remains();
size_t len = src->Read(buff, blockSize);
size_t c = dest.Write(buff, len);
if (c != len) {
error("Cache", "CacheFile failed to write to cached file '%s' (from '%s')\n", cachedfile, src->originalfile);
}
} while (src->Remains());
}
delete src;
return FileStream::OpenFile(cachedfile);
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks