Skip to content

Commit abcc146

Browse files
authored
Merge pull request #82465 from sparr/dedupe_filesystem_path_types
Dedupe filesystem use of cata_path and string
2 parents b0e5957 + f7bc33d commit abcc146

File tree

2 files changed

+69
-169
lines changed

2 files changed

+69
-169
lines changed

src/filesystem.cpp

Lines changed: 52 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "filesystem.h"
2+
#include "cata_path.h"
23

34
#include <algorithm>
45
#include <cstddef>
@@ -258,31 +259,17 @@ void for_each_dir_entry( const std::filesystem::path &path, Function &&function
258259
//--------------------------------------------------------------------------------------------------
259260
// Returns true if entry is a directory, false otherwise.
260261
//--------------------------------------------------------------------------------------------------
261-
bool is_directory( const std::filesystem::directory_entry &entry, const std::string &full_path )
262+
bool is_directory( const std::filesystem::directory_entry &entry,
263+
const std::string &full_path = "" )
262264
{
263265
// We do an extra dance here because directory_entry might not have a cached valid stat result.
264266
std::error_code ec;
265267
bool result = entry.is_directory( ec );
266268

267269
if( ec ) {
268270
std::string e_str = ec.message();
269-
DebugLog( D_WARNING, D_MAIN ) << "stat [" << full_path << "] failed with \"" << e_str << "\".";
270-
return false;
271-
}
272-
273-
return result;
274-
}
275-
276-
bool is_directory( const std::filesystem::directory_entry &entry )
277-
{
278-
// We do an extra dance here because directory_entry might not have a cached valid stat result.
279-
std::error_code ec;
280-
bool result = entry.is_directory( ec );
281-
282-
if( ec ) {
283-
std::string e_str = ec.message();
284-
DebugLog( D_WARNING, D_MAIN ) << "stat [" << entry.path().generic_u8string() << "] failed with \""
285-
<< e_str << "\".";
271+
DebugLog( D_WARNING, D_MAIN ) << "stat [" << ( full_path.empty() ? entry.path().generic_u8string() :
272+
full_path ) << "] failed with \"" << e_str << "\".";
286273
return false;
287274
}
288275

@@ -435,39 +422,24 @@ std::vector<cata_path> find_file_if_bfs( const cata_path &root_path,
435422
} //anonymous namespace
436423

437424
//--------------------------------------------------------------------------------------------------
438-
std::vector<std::string> get_files_from_path( const std::string &pattern,
439-
const std::string &root_path, const bool recursive_search, const bool match_extension )
425+
template<typename P>
426+
std::vector<P> get_files_from_path( const std::string &pattern, const P &root_path,
427+
const bool recursive_search, const bool match_extension )
440428
{
441-
return find_file_if_bfs( root_path,
442-
recursive_search, [&]( const std::filesystem::directory_entry & entry,
443-
bool ) {
444-
return name_contains( entry, pattern, match_extension, false );
445-
} );
446-
}
447-
std::vector<cata_path> get_files_from_path( const std::string &pattern,
448-
const cata_path &root_path, const bool recursive_search, const bool match_extension )
449-
{
450-
return find_file_if_bfs( root_path,
451-
recursive_search, [&]( const std::filesystem::directory_entry & entry,
452-
bool ) {
429+
return find_file_if_bfs( root_path, recursive_search,
430+
[&]( const std::filesystem::directory_entry & entry, bool ) {
453431
return name_contains( entry, pattern, match_extension, false );
454432
} );
455433
}
434+
template std::vector<std::string> get_files_from_path( const std::string &pattern,
435+
const std::string &root_path, const bool recursive_search, const bool match_extension );
436+
template std::vector<cata_path> get_files_from_path( const std::string &pattern,
437+
const cata_path &root_path, const bool recursive_search, const bool match_extension );
456438

457-
std::vector<std::string> get_files_from_path_with_path_exclusion( const std::string &pattern,
439+
template<typename P>
440+
std::vector<P> get_files_from_path_with_path_exclusion( const std::string &pattern,
458441
const std::string &pattern_clash,
459-
const std::string &root_path, const bool recursive_search, const bool match_extension )
460-
{
461-
return find_file_if_bfs( root_path,
462-
recursive_search, [&]( const std::filesystem::directory_entry & entry,
463-
bool ) {
464-
return name_contains( entry, pattern, match_extension, false ) &&
465-
!name_contains( entry, pattern_clash, false, true );
466-
} );
467-
}
468-
std::vector<cata_path> get_files_from_path_with_path_exclusion( const std::string &pattern,
469-
const std::string &pattern_clash,
470-
const cata_path &root_path, const bool recursive_search, const bool match_extension )
442+
const P &root_path, const bool recursive_search, const bool match_extension )
471443
{
472444
return find_file_if_bfs( root_path,
473445
recursive_search, [&]( const std::filesystem::directory_entry & entry,
@@ -476,6 +448,12 @@ std::vector<cata_path> get_files_from_path_with_path_exclusion( const std::strin
476448
!name_contains( entry, pattern_clash, false, true );
477449
} );
478450
}
451+
template std::vector<std::string> get_files_from_path_with_path_exclusion(
452+
const std::string &pattern, const std::string &pattern_clash, const std::string &root_path,
453+
bool recursive_search, bool match_extension );
454+
template std::vector<cata_path> get_files_from_path_with_path_exclusion(
455+
const std::string &pattern, const std::string &pattern_clash, const cata_path &root_path,
456+
bool recursive_search, bool match_extension );
479457

480458
/**
481459
* Find directories which containing pattern.
@@ -484,51 +462,16 @@ std::vector<cata_path> get_files_from_path_with_path_exclusion( const std::strin
484462
* @param recursive_search Be recurse or not.
485463
* @return vector or directories without pattern filename at end.
486464
*/
487-
std::vector<std::string> get_directories_with( const std::string &pattern,
488-
const std::string &root_path, const bool recursive_search )
465+
template<typename P>
466+
std::vector<P> get_directories_with( const std::string &pattern, const P &root_path,
467+
const bool recursive_search )
489468
{
490-
if( pattern.empty() ) {
491-
return std::vector<std::string>();
492-
}
493-
494-
auto files = find_file_if_bfs( root_path,
495-
recursive_search, [&]( const std::filesystem::directory_entry & entry,
496-
bool ) {
497-
return name_contains( entry, pattern, true, false );
498-
} );
499-
500-
// Chop off the file names. Dir path MUST be splitted by '/'
501-
for( auto &file : files ) {
502-
file.erase( file.rfind( '/' ), std::string::npos );
503-
}
504-
505-
files.erase( std::unique( std::begin( files ), std::end( files ) ), std::end( files ) );
506-
507-
return files;
508-
}
509-
510-
std::vector<cata_path> get_directories_with( const std::string &pattern,
511-
const cata_path &root_path, const bool recursive_search )
512-
{
513-
if( pattern.empty() ) {
514-
return {};
515-
}
516-
517-
auto files = find_file_if_bfs( root_path,
518-
recursive_search, [&]( const std::filesystem::directory_entry & entry,
519-
bool ) {
520-
return name_contains( entry, pattern, true, false );
521-
} );
522-
523-
// Chop off the file names. Dir path MUST be splitted by '/'
524-
for( cata_path &file : files ) {
525-
file = file.parent_path();
526-
}
527-
528-
files.erase( std::unique( std::begin( files ), std::end( files ) ), std::end( files ) );
529-
530-
return files;
469+
return get_directories_with( std::vector<std::string>( { pattern } ), root_path, recursive_search );
531470
}
471+
template std::vector<std::string> get_directories_with( const std::string &pattern,
472+
const std::string &root_path, const bool recursive_search );
473+
template std::vector<cata_path> get_directories_with( const std::string &pattern,
474+
const cata_path &root_path, const bool recursive_search );
532475

533476
/**
534477
* Find directories which containing pattern.
@@ -537,37 +480,9 @@ std::vector<cata_path> get_directories_with( const std::string &pattern,
537480
* @param recursive_search Be recurse or not.
538481
* @return vector or directories without pattern filename at end.
539482
*/
540-
std::vector<std::string> get_directories_with( const std::vector<std::string> &patterns,
541-
const std::string &root_path, const bool recursive_search )
542-
{
543-
if( patterns.empty() ) {
544-
return std::vector<std::string>();
545-
}
546-
547-
const auto ext_beg = std::begin( patterns );
548-
const auto ext_end = std::end( patterns );
549-
550-
auto files = find_file_if_bfs( root_path,
551-
recursive_search, [&]( const std::filesystem::directory_entry & entry,
552-
bool ) {
553-
return std::any_of( ext_beg, ext_end, [&]( const std::string & ext ) {
554-
return name_contains( entry, ext, true, false );
555-
} );
556-
} );
557-
558-
//chop off the file names
559-
for( auto &file : files ) {
560-
file.erase( file.rfind( '/' ), std::string::npos );
561-
}
562-
563-
//remove resulting duplicates
564-
files.erase( std::unique( std::begin( files ), std::end( files ) ), std::end( files ) );
565-
566-
return files;
567-
}
568-
569-
std::vector<cata_path> get_directories_with( const std::vector<std::string> &patterns,
570-
const cata_path &root_path, const bool recursive_search )
483+
template<typename P>
484+
std::vector<P> get_directories_with( const std::vector<std::string> &patterns, const P &root_path,
485+
const bool recursive_search )
571486
{
572487
if( patterns.empty() ) {
573488
return {};
@@ -585,43 +500,34 @@ std::vector<cata_path> get_directories_with( const std::vector<std::string> &pat
585500
} );
586501

587502
//chop off the file names
588-
for( cata_path &file : files ) {
589-
file = cata_path{ file.get_logical_root(), file.get_relative_path().parent_path() };
503+
if constexpr( std::is_same_v<P, cata_path> ) {
504+
for( cata_path &file : files ) {
505+
file = cata_path{ file.get_logical_root(), file.get_relative_path().parent_path() };
506+
}
507+
} else {
508+
for( auto &file : files ) {
509+
file.erase( file.rfind( '/' ), std::string::npos );
510+
}
590511
}
591512

592513
//remove resulting duplicates
593514
files.erase( std::unique( std::begin( files ), std::end( files ) ), std::end( files ) );
594515

595516
return files;
596517
}
518+
template std::vector<std::string> get_directories_with( const std::vector<std::string> &patterns,
519+
const std::string &root_path, const bool recursive_search );
520+
template std::vector<cata_path> get_directories_with( const std::vector<std::string> &patterns,
521+
const cata_path &root_path, const bool recursive_search );
597522

598523
/**
599524
* Finds all directories within given path
600525
* @param root_path Search root.
601526
* @param recursive_search Be recurse or not.
602527
* @return vector or directories without pattern filename at end.
603528
*/
604-
std::vector<std::string> get_directories( const std::string &root_path,
605-
const bool recursive_search )
606-
{
607-
auto files = find_file_if_bfs( root_path,
608-
recursive_search, [&]( const std::filesystem::directory_entry & entry,
609-
bool ) {
610-
return dir_exist( entry.path() );
611-
} );
612-
613-
files.erase( std::unique( std::begin( files ), std::end( files ) ), std::end( files ) );
614-
615-
return files;
616-
}
617-
618-
/**
619-
* Finds all directories within given path
620-
* @param root_path Search root.
621-
* @param recursive_search Be recurse or not.
622-
* @return vector or directories without pattern filename at end.
623-
*/
624-
std::vector<cata_path> get_directories( const cata_path &root_path, const bool recursive_search )
529+
template <typename P>
530+
std::vector<P> get_directories( const P &root_path, const bool recursive_search )
625531
{
626532
auto files = find_file_if_bfs( root_path,
627533
recursive_search, [&]( const std::filesystem::directory_entry & entry,
@@ -633,6 +539,10 @@ std::vector<cata_path> get_directories( const cata_path &root_path, const bool r
633539

634540
return files;
635541
}
542+
template std::vector<std::string> get_directories( const std::string &root_path,
543+
const bool recursive_search );
544+
template std::vector<cata_path> get_directories( const cata_path &root_path,
545+
const bool recursive_search );
636546

637547
bool copy_file( const std::string &source_path, const std::string &dest_path )
638548
{

src/filesystem.h

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,11 @@ const char *eol();
6868
* @param match_extension If true, match pattern at the end of file names. Otherwise, match anywhere
6969
* in the file name.
7070
*/
71-
std::vector<std::string> get_files_from_path( const std::string &pattern,
72-
const std::string &root_path = "", bool recursive_search = false,
73-
bool match_extension = false );
74-
std::vector<cata_path> get_files_from_path( const std::string &pattern,
75-
const cata_path &root_path, bool recursive_search = false,
76-
bool match_extension = false );
71+
template<typename P>
72+
std::vector<P> get_files_from_path( const std::string &pattern,
73+
const P &root_path = P(), bool recursive_search = false,
74+
bool match_extension = false );
75+
7776
/**
7877
* Returns a vector of files or directories matching pattern at @p root_path excluding those who's path matches @p pattern_clash.
7978
*
@@ -87,13 +86,10 @@ std::vector<cata_path> get_files_from_path( const std::string &pattern,
8786
* @param match_extension If true, match pattern at the end of file names. Otherwise, match anywhere
8887
* in the file name.
8988
*/
90-
std::vector<std::string> get_files_from_path_with_path_exclusion( const std::string &pattern,
91-
const std::string &pattern_clash,
92-
const std::string &root_path = "", bool recursive_search = false,
93-
bool match_extension = false );
94-
std::vector<cata_path> get_files_from_path_with_path_exclusion( const std::string &pattern,
89+
template<typename P>
90+
std::vector<P> get_files_from_path_with_path_exclusion( const std::string &pattern,
9591
const std::string &pattern_clash,
96-
const cata_path &root_path, bool recursive_search = false,
92+
const P &root_path = P(), bool recursive_search = false,
9793
bool match_extension = false );
9894

9995
//--------------------------------------------------------------------------------------------------
@@ -103,23 +99,17 @@ std::vector<cata_path> get_files_from_path_with_path_exclusion( const std::strin
10399
* @param patterns A vector or patterns to match.
104100
* @see get_files_from_path
105101
*/
106-
std::vector<std::string> get_directories_with( const std::vector<std::string> &patterns,
107-
const std::string &root_path = "", bool recursive_search = false );
108-
109-
std::vector<std::string> get_directories_with( const std::string &pattern,
110-
const std::string &root_path = "", bool recursive_search = false );
111-
112-
std::vector<cata_path> get_directories_with( const std::vector<std::string> &patterns,
113-
const cata_path &root_path = {}, bool recursive_search = false );
114-
115-
std::vector<cata_path> get_directories_with( const std::string &pattern,
116-
const cata_path &root_path = {}, bool recursive_search = false );
102+
template<typename P>
103+
std::vector<P> get_directories_with( const std::vector<std::string> &patterns,
104+
const P &root_path = P(), bool recursive_search = false );
117105

118-
std::vector<std::string> get_directories( const std::string &root_path = "",
119-
bool recursive_search = false );
106+
template<typename P>
107+
std::vector<P> get_directories_with( const std::string &pattern,
108+
const P &root_path = P(), bool recursive_search = false );
120109

121-
std::vector<cata_path> get_directories( const cata_path &root_path = {}, bool recursive_search =
122-
false );
110+
template<typename P>
111+
std::vector<P> get_directories( const P &root_path = P(),
112+
bool recursive_search = false );
123113

124114
bool copy_file( const std::string &source_path, const std::string &dest_path );
125115
bool copy_file( const cata_path &source_path, const cata_path &dest_path );

0 commit comments

Comments
 (0)