forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.cpp
323 lines (276 loc) · 10.2 KB
/
filesystem.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
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
#include "filesystem.h"
#include <algorithm>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iterator>
#include <string>
#include <type_traits>
#include <vector>
#include "cata_utility.h"
#include "debug.h"
#if defined(_WIN32)
# include "platform_win.h"
#endif
bool assure_dir_exist( const std::string &path )
{
std::error_code ec;
fs::path p( path );
bool ret = fs::is_directory( p, ec ) || ( !fs::exists( p, ec ) && fs::create_directories( p, ec ) );
return !ec && ret;
}
bool dir_exist( const std::string &path )
{
return fs::is_directory( path );
}
bool file_exist( const std::string &path )
{
return fs::exists( path ) && !fs::is_directory( path );
}
bool remove_file( const std::string &path )
{
std::error_code ec;
return fs::remove( path, ec );
}
bool rename_file( const std::string &old_path, const std::string &new_path )
{
std::error_code ec;
fs::rename( old_path, new_path, ec );
return !ec;
}
bool remove_directory( const std::string &path )
{
std::error_code ec;
return fs::remove( path, ec );
}
const char *cata_files::eol()
{
#if defined(_WIN32)
// NOLINTNEXTLINE(cata-text-style): carriage return is necessary here
static const char local_eol[] = "\r\n";
#else
static const char local_eol[] = "\n";
#endif
return local_eol;
}
std::string read_entire_file( const std::string &path )
{
cata::ifstream infile( fs::u8path( path ), std::ifstream::in | std::ifstream::binary );
return std::string( std::istreambuf_iterator<char>( infile ),
std::istreambuf_iterator<char>() );
}
namespace
{
//--------------------------------------------------------------------------------------------------
// For non-empty path, call function for each file at path.
//--------------------------------------------------------------------------------------------------
template <typename Function>
void for_each_dir_entry( const std::string &path, Function function )
{
if( path.empty() ) {
return;
}
std::error_code ec;
auto dir_iter = fs::directory_iterator( path, ec );
if( ec ) {
std::string e_str = ec.message();
DebugLog( D_WARNING, D_MAIN ) << "opendir [" << path << "] failed with \"" << e_str << "\".";
return;
}
for( auto &dir_entry : dir_iter ) {
function( dir_entry );
}
}
//--------------------------------------------------------------------------------------------------
// Returns true if entry is a directory, false otherwise.
//--------------------------------------------------------------------------------------------------
bool is_directory( const fs::directory_entry &entry, const std::string &full_path )
{
// We do an extra dance here because directory_entry might not have a cached valid stat result.
std::error_code ec;
bool result = entry.is_directory( ec );
if( ec ) {
std::string e_str = ec.message();
DebugLog( D_WARNING, D_MAIN ) << "stat [" << full_path << "] failed with \"" << e_str << "\".";
return false;
}
return result;
}
//--------------------------------------------------------------------------------------------------
// If at_end is true, returns whether entry's name ends in match.
// Otherwise, returns whether entry's name contains match.
//--------------------------------------------------------------------------------------------------
bool name_contains( const fs::directory_entry &entry, const std::string &match, const bool at_end )
{
std::string entry_name = entry.path().filename().u8string();
const size_t len_fname = entry_name.length();
const size_t len_match = match.length();
if( len_match > len_fname ) {
return false;
}
const size_t offset = at_end ? ( len_fname - len_match ) : 0;
return strstr( entry_name.c_str() + offset, match.c_str() ) != nullptr;
}
//--------------------------------------------------------------------------------------------------
// Return every file at root_path matching predicate.
//
// If root_path is empty, search the current working directory.
// If recursive_search is true, search breadth-first into the directory hierarchy.
//
// Results are ordered depth-first with directories searched in lexically order. Furthermore,
// regular files at each level are also ordered lexically by file name.
//
// Files ending in ~ are excluded.
//--------------------------------------------------------------------------------------------------
template <typename Predicate>
std::vector<std::string> find_file_if_bfs( const std::string &root_path,
const bool recursive_search,
Predicate predicate )
{
std::deque<std::string> directories {!root_path.empty() ? root_path : "."};
std::vector<std::string> results;
while( !directories.empty() ) {
const auto path = std::move( directories.front() );
directories.pop_front();
const std::ptrdiff_t n_dirs = static_cast<std::ptrdiff_t>( directories.size() );
const std::ptrdiff_t n_results = static_cast<std::ptrdiff_t>( results.size() );
// We could use fs::recursive_directory_iterator maybe
for_each_dir_entry( path, [&]( const fs::directory_entry & entry ) {
const auto full_path = entry.path().generic_u8string();
// don't add files ending in '~'.
if( full_path.back() == '~' ) {
return;
}
// add sub directories to recursive_search if requested
const bool is_dir = is_directory( entry, full_path );
if( recursive_search && is_dir ) {
directories.emplace_back( full_path );
}
// check the file
if( !predicate( entry, is_dir ) ) {
return;
}
results.emplace_back( full_path );
} );
// Keep files and directories to recurse ordered consistently
// by sorting from the old end to the new end.
// NOLINTNEXTLINE(cata-use-localized-sorting)
std::sort( std::begin( directories ) + n_dirs, std::end( directories ) );
// NOLINTNEXTLINE(cata-use-localized-sorting)
std::sort( std::begin( results ) + n_results, std::end( results ) );
}
return results;
}
} //anonymous namespace
//--------------------------------------------------------------------------------------------------
std::vector<std::string> get_files_from_path( const std::string &pattern,
const std::string &root_path, const bool recursive_search, const bool match_extension )
{
return find_file_if_bfs( root_path, recursive_search, [&]( const fs::directory_entry & entry,
bool ) {
return name_contains( entry, pattern, match_extension );
} );
}
/**
* Find directories which containing pattern.
* @param pattern Search pattern.
* @param root_path Search root.
* @param recursive_search Be recurse or not.
* @return vector or directories without pattern filename at end.
*/
std::vector<std::string> get_directories_with( const std::string &pattern,
const std::string &root_path, const bool recursive_search )
{
if( pattern.empty() ) {
return std::vector<std::string>();
}
auto files = find_file_if_bfs( root_path, recursive_search, [&]( const fs::directory_entry & entry,
bool ) {
return name_contains( entry, pattern, true );
} );
// Chop off the file names. Dir path MUST be splitted by '/'
for( auto &file : files ) {
file.erase( file.rfind( '/' ), std::string::npos );
}
files.erase( std::unique( std::begin( files ), std::end( files ) ), std::end( files ) );
return files;
}
/**
* Find directories which containing pattern.
* @param patterns Search patterns.
* @param root_path Search root.
* @param recursive_search Be recurse or not.
* @return vector or directories without pattern filename at end.
*/
std::vector<std::string> get_directories_with( const std::vector<std::string> &patterns,
const std::string &root_path, const bool recursive_search )
{
if( patterns.empty() ) {
return std::vector<std::string>();
}
const auto ext_beg = std::begin( patterns );
const auto ext_end = std::end( patterns );
auto files = find_file_if_bfs( root_path, recursive_search, [&]( const fs::directory_entry & entry,
bool ) {
return std::any_of( ext_beg, ext_end, [&]( const std::string & ext ) {
return name_contains( entry, ext, true );
} );
} );
//chop off the file names
for( auto &file : files ) {
file.erase( file.rfind( '/' ), std::string::npos );
}
//remove resulting duplicates
files.erase( std::unique( std::begin( files ), std::end( files ) ), std::end( files ) );
return files;
}
bool copy_file( const std::string &source_path, const std::string &dest_path )
{
cata::ifstream source_stream( fs::u8path( source_path ),
std::ifstream::in | std::ifstream::binary );
if( !source_stream ) {
return false;
}
return write_to_file( dest_path, [&]( std::ostream & dest_stream ) {
dest_stream << source_stream.rdbuf();
}, nullptr ) &&source_stream;
}
std::string ensure_valid_file_name( const std::string &file_name )
{
const char replacement_char = ' ';
const std::string invalid_chars = "\\/:?\"<>|";
// do any replacement in the file name, if needed.
std::string new_file_name = file_name;
std::transform( new_file_name.begin(), new_file_name.end(),
new_file_name.begin(), [&]( const char c ) {
if( invalid_chars.find( c ) != std::string::npos ) {
return replacement_char;
}
return c;
} );
return new_file_name;
}
#if defined(_WIN32) && defined(__GLIBCXX__)
// GLIBCXX does not offer the wchar_t extension for fstream paths
std::string cata::_details::path_to_native( const fs::path &p )
{
if( GetACP() == 65001 ) { // utf-8 code page
return p.u8string();
} else {
return wstr_to_native( p.wstring() );
}
}
#elif defined(_WIN32)
std::wstring cata::_details::path_to_native( const fs::path &p )
{
return p.wstring();
}
#else
std::string cata::_details::path_to_native( const fs::path &p )
{
return p.u8string();
}
#endif