Skip to content

Commit

Permalink
Iterate in same order as hocload.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
JCGoran committed Dec 17, 2024
1 parent 6a3f8d8 commit dff57ca
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/oc/fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <locale>
#include <optional>
#include <regex>
#ifdef HAVE_UNISTD_H
Expand Down Expand Up @@ -534,18 +535,37 @@ static std::optional<std::string> search_hoc_files_regex(const std::regex& patte
const std::vector<std::string>& 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<std::string> paths_oc;
std::vector<std::string> 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<std::string> 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;
}
}
}
Expand Down

0 comments on commit dff57ca

Please sign in to comment.