Skip to content

Commit cef6698

Browse files
authored
Merge pull request #3105 from Digitalone1/master
Optimize search file util
2 parents 0d41123 + 34060ca commit cef6698

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

include/presets_manager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class PresetsManager {
7979

8080
auto load_local_preset_file(const PresetType& preset_type, const std::string& name) -> bool;
8181

82-
auto load_community_preset_file(const PresetType& preset_type, const std::string& full_path) -> bool;
82+
auto load_community_preset_file(const PresetType& preset_type, const std::string& full_path_stem) -> bool;
8383

8484
auto read_effects_pipeline_from_preset(const PresetType& preset_type,
8585
const std::filesystem::path& input_file,

src/equalizer_ui.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ auto import_apo_preset(EqualizerBox* self, const std::string& file_path) -> bool
369369
double preamp = 0.0;
370370

371371
if (const auto re = std::regex(R"(^[ \t]*#)"); eq_file.is_open()) {
372-
for (std::string line; getline(eq_file, line);) {
372+
for (std::string line; std::getline(eq_file, line);) {
373373
if (std::regex_search(line, re)) { // Avoid commented lines
374374
continue;
375375
}
@@ -666,7 +666,7 @@ auto import_graphiceq_preset(EqualizerBox* self, const std::string& file_path) -
666666
std::vector<struct GraphicEQ_Band> bands;
667667

668668
if (const auto re = std::regex(R"(^[ \t]*#)"); eq_file.is_open()) {
669-
for (std::string line; getline(eq_file, line);) {
669+
for (std::string line; std::getline(eq_file, line);) {
670670
if (std::regex_search(line, re)) { // Avoid commented lines
671671
continue;
672672
}

src/presets_manager.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,12 +620,13 @@ auto PresetsManager::load_local_preset_file(const PresetType& preset_type, const
620620
return false;
621621
}
622622

623-
auto PresetsManager::load_community_preset_file(const PresetType& preset_type, const std::string& full_path) -> bool {
624-
const auto input_file = std::filesystem::path{full_path + json_ext};
623+
auto PresetsManager::load_community_preset_file(const PresetType& preset_type, const std::string& full_path_stem)
624+
-> bool {
625+
const auto input_file = std::filesystem::path{full_path_stem + json_ext};
625626

626627
// Check preset existence
627628
if (!std::filesystem::exists(input_file)) {
628-
util::warning("the community preset \"" + full_path + "\" does not exist on the filesystem");
629+
util::warning("the community preset \"" + input_file.string() + "\" does not exist on the filesystem");
629630

630631
return false;
631632
}

src/util.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -424,31 +424,39 @@ auto search_filename(const std::filesystem::path& path,
424424
const std::string& filename,
425425
std::string& full_path_result,
426426
const uint& top_scan_level) -> bool {
427-
// Recursive util to search a filename from an origin full path.
427+
// Recursive util to search a filename from an origin full path directory.
428428
// The search is performed in subdirectories and it's stopped at a specified
429429
// sublevel (top_scan_level = 1 searches only in the path).
430430

431-
if (!std::filesystem::exists(path)) {
431+
if (!std::filesystem::exists(path) || !std::filesystem::is_directory(path)) {
432432
return false;
433433
}
434434

435+
const auto fn = path / filename;
436+
437+
// Get the file in this directory, if exists.
438+
if (std::filesystem::exists(fn) && std::filesystem::is_regular_file(fn)) {
439+
// File found, abort the search.
440+
full_path_result = fn.c_str();
441+
442+
return true;
443+
}
444+
445+
// The file is not in this directory, search in subdirectories.
435446
const auto scan_level = top_scan_level - 1U;
436447

448+
if (scan_level == 0U) {
449+
return false;
450+
}
451+
437452
auto it = std::filesystem::directory_iterator{path};
438453

439454
try {
440455
while (it != std::filesystem::directory_iterator{}) {
441-
if (std::filesystem::is_regular_file(it->status())) {
442-
if (const auto path = it->path(); path.filename().c_str() == filename) {
443-
// File found, abort the search.
444-
full_path_result = path.c_str();
445-
446-
return true;
447-
}
448-
} else if (scan_level > 0U && std::filesystem::is_directory(it->status())) {
449-
if (const auto path = it->path(); !path.empty()) {
456+
if (std::filesystem::is_directory(it->status())) {
457+
if (const auto p = it->path(); !p.empty()) {
450458
// Continue the search in the subfolder.
451-
const auto found = search_filename(path, filename, full_path_result, scan_level);
459+
const auto found = search_filename(p, filename, full_path_result, scan_level);
452460

453461
if (found) {
454462
return true;

0 commit comments

Comments
 (0)