1
1
#include " filesystem.h"
2
+ #include " cata_path.h"
2
3
3
4
#include < algorithm>
4
5
#include < cstddef>
@@ -258,31 +259,17 @@ void for_each_dir_entry( const std::filesystem::path &path, Function &&function
258
259
// --------------------------------------------------------------------------------------------------
259
260
// Returns true if entry is a directory, false otherwise.
260
261
// --------------------------------------------------------------------------------------------------
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 = " " )
262
264
{
263
265
// We do an extra dance here because directory_entry might not have a cached valid stat result.
264
266
std::error_code ec;
265
267
bool result = entry.is_directory ( ec );
266
268
267
269
if ( ec ) {
268
270
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 << " \" ." ;
286
273
return false ;
287
274
}
288
275
@@ -435,39 +422,24 @@ std::vector<cata_path> find_file_if_bfs( const cata_path &root_path,
435
422
} // anonymous namespace
436
423
437
424
// --------------------------------------------------------------------------------------------------
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 )
440
428
{
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 ) {
453
431
return name_contains ( entry, pattern, match_extension, false );
454
432
} );
455
433
}
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 );
456
438
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,
458
441
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 )
471
443
{
472
444
return find_file_if_bfs ( root_path,
473
445
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
476
448
!name_contains ( entry, pattern_clash, false , true );
477
449
} );
478
450
}
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 );
479
457
480
458
/* *
481
459
* Find directories which containing pattern.
@@ -484,51 +462,16 @@ std::vector<cata_path> get_files_from_path_with_path_exclusion( const std::strin
484
462
* @param recursive_search Be recurse or not.
485
463
* @return vector or directories without pattern filename at end.
486
464
*/
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 )
489
468
{
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 );
531
470
}
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 );
532
475
533
476
/* *
534
477
* Find directories which containing pattern.
@@ -537,37 +480,9 @@ std::vector<cata_path> get_directories_with( const std::string &pattern,
537
480
* @param recursive_search Be recurse or not.
538
481
* @return vector or directories without pattern filename at end.
539
482
*/
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 )
571
486
{
572
487
if ( patterns.empty () ) {
573
488
return {};
@@ -585,43 +500,34 @@ std::vector<cata_path> get_directories_with( const std::vector<std::string> &pat
585
500
} );
586
501
587
502
// 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
+ }
590
511
}
591
512
592
513
// remove resulting duplicates
593
514
files.erase ( std::unique ( std::begin ( files ), std::end ( files ) ), std::end ( files ) );
594
515
595
516
return files;
596
517
}
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 );
597
522
598
523
/* *
599
524
* Finds all directories within given path
600
525
* @param root_path Search root.
601
526
* @param recursive_search Be recurse or not.
602
527
* @return vector or directories without pattern filename at end.
603
528
*/
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 )
625
531
{
626
532
auto files = find_file_if_bfs ( root_path,
627
533
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
633
539
634
540
return files;
635
541
}
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 );
636
546
637
547
bool copy_file ( const std::string &source_path, const std::string &dest_path )
638
548
{
0 commit comments