Skip to content

Commit

Permalink
implement open preference (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand authored Jan 8, 2025
1 parent 79b2bae commit d770216
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/odr/internal/open_strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@

namespace odr::internal {

namespace {

template <typename T> auto priority_comparator(const std::vector<T> &priority) {
return [&priority](const T &a, const T &b) {
auto a_it = std::find(std::begin(priority), std::end(priority), a);
auto b_it = std::find(std::begin(priority), std::end(priority), b);

if (a_it == std::end(priority) && b_it == std::end(priority)) {
return false;
}
if (a_it == std::end(priority)) {
return false;
}
if (b_it == std::end(priority)) {
return true;
}

return std::distance(std::begin(priority), a_it) <
std::distance(std::begin(priority), b_it);
};
}

} // namespace

std::vector<FileType>
open_strategy::types(const std::shared_ptr<abstract::File> &file) {
std::vector<FileType> result;
Expand Down Expand Up @@ -386,6 +410,9 @@ open_strategy::open_file(std::shared_ptr<abstract::File> file,
probe_types.erase(probe_types_end, probe_types.end());
}

std::ranges::sort(probe_types,
priority_comparator(preference.file_type_priority));

for (FileType as : probe_types) {
std::vector<DecoderEngine> probe_engines;
if (preference.with_engine.has_value()) {
Expand All @@ -399,6 +426,9 @@ open_strategy::open_file(std::shared_ptr<abstract::File> file,
probe_engines.erase(probe_engines_end, probe_engines.end());
}

std::ranges::sort(probe_engines,
priority_comparator(preference.engine_priority));

for (DecoderEngine with : probe_engines) {
auto decoded_file = open_file(file, as, with);
if (decoded_file != nullptr) {
Expand Down

0 comments on commit d770216

Please sign in to comment.