forked from ParadoxGameConverters/commonItems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSCommonLayer.cpp
298 lines (249 loc) · 6.65 KB
/
OSCommonLayer.cpp
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
#include "Log.h"
#include "OSCompatibilityLayer.h"
#include <algorithm>
#include <codecvt>
#include <filesystem>
using std::filesystem::copy_options;
using std::filesystem::current_path;
using std::filesystem::directory_iterator;
using std::filesystem::path;
using std::filesystem::recursive_directory_iterator;
using std::filesystem::u8path;
namespace commonItems
{
bool TryCreateFolder(const std::string& path)
{
#pragma warning(push)
#pragma warning(disable : 4996)
if (exists(u8path(path)))
return true;
if (create_directory(u8path(path)))
return true;
Log(LogLevel::Error) << "Could not create directory: " << path << " : " << GetLastErrorString();
return false;
#pragma warning(pop)
}
std::wstring GetCurrentDirectoryWString()
{
// Tried straight returning wstring, but on Linux it will break if filesystem uses characters
// outside ascii, apparently inherent conversion is broken.
try
{
const auto path = current_path().string();
return convertUTF8ToUTF16(path);
}
catch (std::exception& e)
{
Log(LogLevel::Error) << "Cannot determine current path; " << e.what();
return {};
}
}
std::set<path> GetAllFilesInFolder(const path& folder_path)
{
std::set<path> fileNames;
if (!exists(folder_path))
return fileNames;
for (const auto& p: directory_iterator(folder_path))
{
if (!p.is_directory())
{
fileNames.insert(p.path().filename());
}
}
return fileNames;
}
std::set<std::string> GetAllFilesInFolder(const std::string& path)
{
#pragma warning(push)
#pragma warning(disable : 4996)
std::set<std::string> fileNames;
if (!exists(u8path(path)))
return fileNames;
for (const auto& p: directory_iterator(u8path(path)))
{
if (!p.is_directory())
{
fileNames.insert(p.path().filename().string());
}
}
return fileNames;
#pragma warning(pop)
}
void GetAllFilesInFolder(const std::string& path, std::set<std::string>& fileNames)
{
#pragma warning(push)
#pragma warning(disable : 4996)
fileNames = GetAllFilesInFolder(path);
#pragma warning(pop)
}
std::set<path> GetAllSubfolders(const path& folder_path)
{
std::set<path> subFolders;
if (!exists(folder_path))
return subFolders;
for (const auto& p: directory_iterator(folder_path))
{
if (p.is_directory())
{
subFolders.insert(p.path().filename());
}
}
return subFolders;
}
std::set<std::string> GetAllSubfolders(const std::string& path)
{
#pragma warning(push)
#pragma warning(disable : 4996)
std::set<std::string> subFolders;
if (!exists(u8path(path)))
return subFolders;
for (const auto& p: directory_iterator(u8path(path)))
{
if (p.is_directory())
{
subFolders.insert(p.path().filename().string());
}
}
return subFolders;
#pragma warning(pop)
}
void GetAllSubfolders(const std::string& path, std::set<std::string>& subFolders)
{
#pragma warning(push)
#pragma warning(disable : 4996)
subFolders = GetAllSubfolders(path);
#pragma warning(pop)
}
std::set<path> GetAllFilesInFolderRecursive(const path& folder_path)
{
if (!exists(folder_path) || !is_directory(folder_path))
{
return {};
}
std::set<path> fileNames;
for (const auto& p: recursive_directory_iterator(folder_path))
{
if (!p.is_directory())
{
path current_path = p.path();
current_path.make_preferred();
// Get the part of current_path that's not in the requested path
auto current_itr = current_path.begin();
for (auto path_itr = folder_path.begin(); path_itr != folder_path.end(); ++current_itr, ++path_itr)
{
if (path_itr->empty())
{
break;
}
}
path requested_path;
for (; current_itr != current_path.end(); ++current_itr)
{
requested_path /= *current_itr;
}
fileNames.insert(requested_path);
}
}
return fileNames;
}
void GetAllFilesInFolderRecursive(const std::string& path, std::set<std::string>& fileNames)
{
#pragma warning(push)
#pragma warning(disable : 4996)
fileNames = GetAllFilesInFolderRecursive(path);
#pragma warning(pop)
}
bool TryCopyFile(const std::string& sourcePath, const std::string& destPath)
{
#pragma warning(push)
#pragma warning(disable : 4996)
if (copy_file(u8path(sourcePath), u8path(destPath), copy_options::overwrite_existing))
return true;
LOG(LogLevel::Warning) << "Could not copy file " << sourcePath << " to " << destPath << " - " << GetLastErrorString();
return false;
#pragma warning(pop)
}
bool CopyFolder(const std::string& sourceFolder, const std::string& destFolder)
{
#pragma warning(push)
#pragma warning(disable : 4996)
try
{
copy(u8path(sourceFolder), u8path(destFolder), copy_options::recursive);
return true;
}
catch (std::exception& e)
{
Log(LogLevel::Error) << "Could not copy folder: " << e.what() << " " << GetLastErrorString();
return false;
}
#pragma warning(pop)
}
bool RenameFolder(const std::string& sourceFolder, const std::string& destFolder)
{
#pragma warning(push)
#pragma warning(disable : 4996)
try
{
rename(u8path(sourceFolder), u8path(destFolder));
return true;
}
catch (std::exception& e)
{
Log(LogLevel::Error) << "Could not rename folder: " << e.what() << " " << GetLastErrorString();
return false;
}
#pragma warning(pop)
}
bool DoesFileExist(const path& path)
{
return exists(path) && !is_directory(path);
}
bool DoesFileExist(const std::string& path)
{
#pragma warning(push)
#pragma warning(disable : 4996)
return DoesFileExist(u8path(path));
#pragma warning(pop)
}
bool DoesFolderExist(const path& path)
{
return exists(path) && is_directory(path);
}
bool DoesFolderExist(const std::string& path)
{
#pragma warning(push)
#pragma warning(disable : 4996)
return DoesFolderExist(u8path(path));
#pragma warning(pop)
}
bool DeleteFolder(const std::string& folder)
{
#pragma warning(push)
#pragma warning(disable : 4996)
if (!exists(u8path(folder)))
return true;
if (remove_all(u8path(folder)) != static_cast<std::uintmax_t>(-1))
return true;
Log(LogLevel::Error) << "Could not delete folder: " << folder << " " << GetLastErrorString();
return false;
#pragma warning(pop)
}
std::string normalizeUTF8Path(const std::string& utf_8_path)
{
std::string asciiPath = convertUTF8ToASCII(utf_8_path);
std::ranges::replace(asciiPath, '/', '_');
std::ranges::replace(asciiPath, '\\', '_');
std::ranges::replace(asciiPath, ':', '_');
std::ranges::replace(asciiPath, '*', '_');
std::ranges::replace(asciiPath, '?', '_');
std::ranges::replace(asciiPath, '\"', '_');
std::ranges::replace(asciiPath, '<', '_');
std::ranges::replace(asciiPath, '>', '_');
std::ranges::replace(asciiPath, '|', '_');
asciiPath.erase(std::ranges::remove(asciiPath, '\t').begin(), asciiPath.end());
asciiPath.erase(std::ranges::remove(asciiPath, '\n').begin(), asciiPath.end());
asciiPath.erase(std::ranges::remove(asciiPath, '\r').begin(), asciiPath.end());
return asciiPath;
}
} // namespace commonItems