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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/*
* The ManaPlus Client
* Copyright (C) 2013-2017 The ManaPlus Developers
*
* This file is part of The ManaPlus Client.
*
* 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
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "utils/files.h"
#include "logger.h"
#if defined(ANDROID) || defined(__native_client__)
#include "resources/resourcemanager/resourcemanager.h"
#include "utils/mkdir.h"
#endif // defined(ANDROID) || defined(__native_client__)
#include "utils/mkdir.h"
#include "utils/paths.h"
#include "utils/virtfs.h"
#include "utils/stringutils.h"
#include <algorithm>
#include <dirent.h>
#include <sstream>
#include "debug.h"
#ifdef ANDROID
void Files::extractLocale()
{
// in future need also remove all locales in local dir
const std::string fileName2 = std::string(getenv(
"APPDIR")).append("/locale.zip");
resourceManager->addToSearchPath(fileName2, Append_false);
const std::string localDir = std::string(getenv("APPDIR")).append("/");
char **rootDirs = VirtFs::enumerateFiles("locale");
for (char **i = rootDirs; *i; i++)
{
const std::string dir = std::string("locale/").append(*i);
if (VirtFs::isDirectory(dir.c_str()))
{
const std::string moFile = dir + "/LC_MESSAGES/manaplus.mo";
if (VirtFs::exists((moFile).c_str()))
{
const std::string localFile = localDir + moFile;
const std::string localDir2 = localDir + dir + "/LC_MESSAGES";
mkdir_r(localDir2.c_str());
copyPhysFsFile(moFile, localFile);
}
}
}
VirtFs::freeList(rootDirs);
resourceManager->removeFromSearchPath(fileName2);
remove(fileName2.c_str());
}
#endif // ANDROID
#if defined(ANDROID) || defined(__native_client__)
namespace
{
#ifdef ANDROID
int mFilesCount = 0;
#endif // ANDROID
Files::CopyFileCallbackPtr mCallbackPtr = nullptr;
} // namespace
void Files::setCopyCallBack(Files::CopyFileCallbackPtr callback)
{
mCallbackPtr = callback;
}
void Files::copyPhysFsFile(const std::string &restrict inFile,
const std::string &restrict outFile)
{
int size = 0;
void *const buf = VirtFs::loadFile(inFile, size);
FILE *const file = fopen(outFile.c_str(), "w");
fwrite(buf, 1, size, file);
fclose(file);
free(buf);
#ifdef ANDROID
if (mCallbackPtr)
{
mCallbackPtr(mFilesCount);
mFilesCount ++;
}
#endif // ANDROID
}
void Files::copyPhysFsDir(const std::string &restrict inDir,
const std::string &restrict outDir)
{
mkdir_r(outDir.c_str());
char **files = VirtFs::enumerateFiles(inDir.c_str());
for (char **i = files; *i; i++)
{
const std::string file = std::string(inDir).append("/").append(*i);
const std::string outDir2 = std::string(outDir).append("/").append(*i);
if (VirtFs::isDirectory(file.c_str()))
copyPhysFsDir(file, outDir2);
else
copyPhysFsFile(file, outDir2);
}
VirtFs::freeList(files);
}
void Files::extractZip(const std::string &restrict zipName,
const std::string &restrict inDir,
const std::string &restrict outDir)
{
resourceManager->addToSearchPath(zipName, Append_false);
copyPhysFsDir(inDir, outDir);
resourceManager->removeFromSearchPath(zipName);
remove(zipName.c_str());
}
#endif // ANDROID __native_client__
int Files::renameFile(const std::string &restrict srcName,
const std::string &restrict dstName)
{
#if defined __native_client__
FILE *srcFile = fopen(srcName.c_str(), "rb");
if (srcFile == nullptr)
return -1;
FILE *dstFile = fopen(dstName.c_str(), "w+b");
if (dstFile == nullptr)
{
fclose(srcFile);
return -1;
}
const int chunkSize = 500000;
char *buf = new char[chunkSize];
size_t sz = 0;
while ((sz = fread(buf, 1, chunkSize, srcFile)))
{
if (fwrite(buf, 1, sz, dstFile) != sz)
{
delete [] buf;
fclose(srcFile);
fclose(dstFile);
::remove(dstName.c_str());
return -1;
}
}
delete [] buf;
fclose(srcFile);
fclose(dstFile);
if (!::remove(srcName.c_str()))
return 0;
return -1;
#else // defined __native_client__
return ::rename(srcName.c_str(), dstName.c_str());
#endif // defined __native_client__
}
int Files::copyFile(const std::string &restrict srcName,
const std::string &restrict dstName)
{
FILE *srcFile = fopen(srcName.c_str(), "rb");
if (srcFile == nullptr)
return -1;
FILE *dstFile = fopen(dstName.c_str(), "w+b");
if (dstFile == nullptr)
{
fclose(srcFile);
return -1;
}
const int chunkSize = 500000;
char *buf = new char[chunkSize];
size_t sz = 0;
while ((sz = fread(buf, 1, chunkSize, srcFile)))
{
if (fwrite(buf, 1, sz, dstFile) != sz)
{
delete [] buf;
fclose(srcFile);
fclose(dstFile);
return -1;
}
}
delete [] buf;
fclose(srcFile);
fclose(dstFile);
return 0;
}
void Files::getFiles(const std::string &path, StringVect &list)
{
char **const fonts = VirtFs::enumerateFiles(path.c_str());
for (char *const *i = fonts; *i; i++)
{
if (!VirtFs::isDirectory((path + *i).c_str()))
list.push_back(*i);
}
VirtFs::freeList(fonts);
}
void Files::getDirs(const std::string &path, StringVect &list)
{
char **const fonts = VirtFs::enumerateFiles(path.c_str());
for (char *const *i = fonts; *i; i++)
{
if (VirtFs::isDirectory((path + *i).c_str()))
list.push_back(*i);
}
VirtFs::freeList(fonts);
}
void Files::getFilesWithDir(const std::string &path, StringVect &list)
{
char **const fonts = VirtFs::enumerateFiles(path.c_str());
for (char *const *i = fonts; *i; i++)
{
if (!VirtFs::isDirectory((path + *i).c_str()))
list.push_back(path + *i);
}
VirtFs::freeList(fonts);
}
bool Files::existsLocal(const std::string &path)
{
bool flg(false);
std::fstream file;
file.open(path.c_str(), std::ios::in);
if (file.is_open())
flg = true;
file.close();
return flg;
}
std::string Files::getPath(const std::string &file)
{
// get the real path to the file
const char *const tmp = VirtFs::getRealDir(file.c_str());
std::string path;
// if the file is not in the search path, then its nullptr
if (tmp)
{
path = std::string(tmp).append(dirSeparator).append(file);
#if defined __native_client__
std::string dataZip = "/http/data.zip/";
if (path.substr(0, dataZip.length()) == dataZip)
path = path.replace(0, dataZip.length(), "/http/data/");
#endif // defined __native_client__
}
else
{
// if not found in search path return the default path
path = getPackageDir().append(dirSeparator).append(file);
}
return path;
}
std::string Files::loadTextFileString(const std::string &fileName)
{
int contentsLength;
char *fileContents = static_cast<char*>(
VirtFs::loadFile(fileName, contentsLength));
if (!fileContents)
{
logger->log("Couldn't load text file: %s", fileName.c_str());
return std::string();
}
const std::string str = std::string(fileContents, contentsLength);
free(fileContents);
return str;
}
bool Files::loadTextFile(const std::string &fileName,
StringVect &lines)
{
int contentsLength;
char *fileContents = static_cast<char*>(
VirtFs::loadFile(fileName, contentsLength));
if (!fileContents)
{
logger->log("Couldn't load text file: %s", fileName.c_str());
return false;
}
std::istringstream iss(std::string(fileContents, contentsLength));
std::string line;
while (getline(iss, line))
lines.push_back(line);
free(fileContents);
return true;
}
bool Files::loadTextFileLocal(const std::string &fileName,
StringVect &lines)
{
std::ifstream file;
char line[501];
file.open(fileName.c_str(), std::ios::in);
if (!file.is_open())
{
logger->log("Couldn't load text file: %s", fileName.c_str());
return false;
}
while (file.getline(line, 500))
lines.push_back(line);
return true;
}
void Files::saveTextFile(std::string path,
const std::string &restrict name,
const std::string &restrict text)
{
if (!mkdir_r(path.c_str()))
{
std::ofstream file;
file.open((path.append("/").append(name)).c_str(), std::ios::out);
if (file.is_open())
file << text << std::endl;
file.close();
}
}
void Files::deleteFilesInDirectory(std::string path)
{
path += "/";
const struct dirent *next_file = nullptr;
DIR *const dir = opendir(path.c_str());
if (dir)
{
while ((next_file = readdir(dir)))
{
const std::string file = next_file->d_name;
if (file != "." && file != "..")
remove((path + file).c_str());
}
closedir(dir);
}
}
void Files::getFilesInDir(const std::string &dir,
StringVect &list,
const std::string &ext)
{
const std::string path = dir + "/";
StringVect tempList;
Files::getFilesWithDir(path, tempList);
FOR_EACH (StringVectCIter, it, tempList)
{
const std::string &str = *it;
if (findLast(str, ext))
list.push_back(str);
}
std::sort(list.begin(), list.end());
}
|