1 |
|
|
/* |
2 |
|
|
* The ManaPlus Client |
3 |
|
|
* Copyright (C) 2013-2019 The ManaPlus Developers |
4 |
|
|
* Copyright (C) 2019-2021 Andrei Karas |
5 |
|
|
* |
6 |
|
|
* This file is part of The ManaPlus Client. |
7 |
|
|
* |
8 |
|
|
* This program is free software; you can redistribute it and/or modify |
9 |
|
|
* it under the terms of the GNU General Public License as published by |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
11 |
|
|
* any later version. |
12 |
|
|
* |
13 |
|
|
* This program is distributed in the hope that it will be useful, |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
|
|
* GNU General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU General Public License |
19 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#include "fs/virtfs/tools.h" |
23 |
|
|
|
24 |
|
|
#include "logger.h" |
25 |
|
|
|
26 |
|
|
#include "fs/paths.h" |
27 |
|
|
|
28 |
|
|
#include "fs/virtfs/fs.h" |
29 |
|
|
#include "fs/virtfs/list.h" |
30 |
|
|
|
31 |
|
|
#include "utils/foreach.h" |
32 |
|
|
#include "utils/stringutils.h" |
33 |
|
|
|
34 |
|
|
#include <algorithm> |
35 |
|
|
#include <sstream> |
36 |
|
|
|
37 |
|
|
#include "debug.h" |
38 |
|
|
|
39 |
|
|
namespace VirtFs |
40 |
|
|
{ |
41 |
|
|
void searchAndAddArchives(const std::string &restrict path, |
42 |
|
|
const std::string &restrict ext, |
43 |
|
|
const Append append) |
44 |
|
|
{ |
45 |
|
|
List *const list = VirtFs::enumerateFiles(path); |
46 |
|
|
FOR_EACH (StringVectCIter, i, list->names) |
47 |
|
|
{ |
48 |
|
|
const std::string str = *i; |
49 |
|
|
const size_t len = str.size(); |
50 |
|
|
|
51 |
|
|
if (len > ext.length() && |
52 |
|
|
ext == str.substr(len - ext.length())) |
53 |
|
|
{ |
54 |
|
|
const std::string file = path + str; |
55 |
|
|
const std::string realPath = VirtFs::getRealDir(file); |
56 |
|
|
VirtFs::mountZip(pathJoin(realPath, file), append); |
57 |
|
|
} |
58 |
|
|
} |
59 |
|
|
VirtFs::freeList(list); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
void searchAndRemoveArchives(const std::string &restrict path, |
63 |
|
|
const std::string &restrict ext) |
64 |
|
|
{ |
65 |
|
|
List *const list = VirtFs::enumerateFiles(path); |
66 |
|
|
FOR_EACH (StringVectCIter, i, list->names) |
67 |
|
|
{ |
68 |
|
|
const std::string str = *i; |
69 |
|
|
const size_t len = str.size(); |
70 |
|
|
if (len > ext.length() && |
71 |
|
|
ext == str.substr(len - ext.length())) |
72 |
|
|
{ |
73 |
|
|
const std::string file = path + str; |
74 |
|
|
const std::string realPath = VirtFs::getRealDir(file); |
75 |
|
|
VirtFs::unmountZip(pathJoin(realPath, file)); |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
VirtFs::freeList(list); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
85 |
void getFilesInDir(const std::string &dir, |
82 |
|
|
StringVect &list, |
83 |
|
|
const std::string &ext) |
84 |
|
|
{ |
85 |
|
85 |
const std::string &path = dir; |
86 |
|
170 |
StringVect tempList; |
87 |
✓✗ |
170 |
VirtFs::getFilesWithDir(path, tempList); |
88 |
✓✓ |
494 |
FOR_EACH (StringVectCIter, it, tempList) |
89 |
|
|
{ |
90 |
|
69 |
const std::string &str = *it; |
91 |
✓✗✓✓
|
69 |
if (findLast(str, ext)) |
92 |
✓✗ |
29 |
list.push_back(str); |
93 |
|
|
} |
94 |
|
170 |
std::sort(list.begin(), list.end()); |
95 |
|
85 |
} |
96 |
|
|
|
97 |
|
771 |
std::string getPath(const std::string &file) |
98 |
|
|
{ |
99 |
|
|
// get the real path to the file |
100 |
✓✗ |
2313 |
const std::string tmp = VirtFs::getRealDir(file); |
101 |
|
771 |
std::string path; |
102 |
|
|
|
103 |
|
|
// if the file is not in the search path, then its empty |
104 |
✓✓ |
771 |
if (!tmp.empty()) |
105 |
|
|
{ |
106 |
✓✗ |
2307 |
path = pathJoin(tmp, file); |
107 |
|
|
#if defined __native_client__ |
108 |
|
|
std::string dataZip = "/http/data.zip/"; |
109 |
|
|
if (path.substr(0, dataZip.length()) == dataZip) |
110 |
|
|
path = path.replace(0, dataZip.length(), "/http/data/"); |
111 |
|
|
#endif // defined __native_client__ |
112 |
|
|
} |
113 |
|
|
else |
114 |
|
|
{ |
115 |
|
|
// if not found in search path return the default path |
116 |
✓✗✓✗
|
6 |
path = pathJoin(getPackageDir(), file); |
117 |
|
|
} |
118 |
|
|
|
119 |
|
771 |
return path; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
3 |
std::string loadTextFileString(const std::string &fileName) |
123 |
|
|
{ |
124 |
|
|
int contentsLength; |
125 |
✓✗ |
6 |
const char *fileContents = VirtFs::loadFile(fileName, contentsLength); |
126 |
|
|
|
127 |
✗✓ |
3 |
if (fileContents == nullptr) |
128 |
|
|
{ |
129 |
|
|
logger->log("Couldn't load text file: %s", fileName.c_str()); |
130 |
|
|
return std::string(); |
131 |
|
|
} |
132 |
|
12 |
const std::string str = std::string(fileContents, contentsLength); |
133 |
|
3 |
delete [] fileContents; |
134 |
|
3 |
return str; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
49 |
bool loadTextFile(const std::string &fileName, |
138 |
|
|
StringVect &lines) |
139 |
|
|
{ |
140 |
|
|
int contentsLength; |
141 |
✓✗ |
98 |
const char *fileContents = VirtFs::loadFile(fileName, contentsLength); |
142 |
|
|
|
143 |
✓✓ |
49 |
if (fileContents == nullptr) |
144 |
|
|
{ |
145 |
|
2 |
logger->log("Couldn't load text file: %s", fileName.c_str()); |
146 |
|
|
return false; |
147 |
|
|
} |
148 |
|
|
|
149 |
✓✗ |
282 |
std::istringstream iss(std::string(fileContents, contentsLength)); |
150 |
|
47 |
std::string line; |
151 |
|
|
|
152 |
✓✗✓✓
|
758 |
while (getline(iss, line)) |
153 |
✓✗ |
332 |
lines.push_back(line); |
154 |
|
|
|
155 |
|
47 |
delete [] fileContents; |
156 |
|
47 |
return true; |
157 |
|
|
} |
158 |
|
|
} // namespace VirtFs |