From dff57ca35b3be8ad8034fa6d1ee2c3a2a00c9064 Mon Sep 17 00:00:00 2001 From: Goran Jelic-Cizmek Date: Tue, 17 Dec 2024 14:50:33 +0100 Subject: [PATCH] Iterate in same order as `hocload.sh` --- src/oc/fileio.cpp | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/src/oc/fileio.cpp b/src/oc/fileio.cpp index d9cd0a5651..6068bc68da 100644 --- a/src/oc/fileio.cpp +++ b/src/oc/fileio.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #ifdef HAVE_UNISTD_H @@ -534,18 +535,37 @@ static std::optional search_hoc_files_regex(const std::regex& patte const std::vector& paths) { namespace fs = std::filesystem; for (const auto& path: paths) { + // construct a list containing names of, in this order: + // - `.oc` files (sorted according to locale) + // - `.hoc` files (sorted according to locale) + std::vector paths_oc; + std::vector paths_hoc; for (const auto& entry: fs::directory_iterator(path)) { - if (entry.is_regular_file() && - (entry.path().extension() == ".oc" || entry.path().extension() == ".hoc")) { - auto file = std::ifstream(entry.path()); - if (!file.is_open()) - continue; - - std::string line; - while (std::getline(file, line)) { - if (std::regex_search(line, pattern)) { - return entry.path().string(); - } + if (entry.is_regular_file() && entry.path().extension() == ".oc") { + paths_oc.push_back(entry.path()); + } else if (entry.is_regular_file() && entry.path().extension() == ".hoc") { + paths_hoc.push_back(entry.path()); + } + } + std::sort(paths_oc.begin(), paths_oc.end(), std::locale()); + std::sort(paths_hoc.begin(), paths_hoc.end(), std::locale()); + + std::vector result; + std::merge(paths_oc.begin(), + paths_oc.end(), + paths_hoc.begin(), + paths_hoc.end(), + back_inserter(result)); + + for (const auto& entry: result) { + auto file = std::ifstream(entry); + if (!file.is_open()) + continue; + + std::string line; + while (std::getline(file, line)) { + if (std::regex_search(line, pattern)) { + return entry; } } }