|
| 1 | +// |
| 2 | +// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | +// |
| 4 | +// Copyright © 2023 ANSSI. All Rights Reserved. |
| 5 | +// |
| 6 | +// Author(s): fabienfl (ANSSI) |
| 7 | +// |
| 8 | + |
| 9 | +#include "ResurrectRecordsMode.h" |
| 10 | + |
| 11 | +#include <boost/algorithm/string.hpp> |
| 12 | + |
| 13 | +#include "Log/Log.h" |
| 14 | + |
| 15 | +namespace { |
| 16 | + |
| 17 | +using namespace std::string_view_literals; |
| 18 | + |
| 19 | +constexpr auto kNo = "No"sv; |
| 20 | +constexpr auto kYes = "Yes"sv; |
| 21 | +constexpr auto kResident = "Resident"sv; |
| 22 | + |
| 23 | +constexpr auto kNoW = L"No"sv; |
| 24 | +constexpr auto kYesW = L"Yes"sv; |
| 25 | +constexpr auto kResidentW = L"Resident"sv; |
| 26 | + |
| 27 | +using ResurrectRecordsModeEntry = std::tuple<Orc::ResurrectRecordsMode, std::string_view, std::wstring_view>; |
| 28 | + |
| 29 | +constexpr std::array kModes = { |
| 30 | + ResurrectRecordsModeEntry { |
| 31 | + Orc::ResurrectRecordsMode::kNo, |
| 32 | + kNo, |
| 33 | + kNoW, |
| 34 | + }, |
| 35 | + ResurrectRecordsModeEntry { |
| 36 | + Orc::ResurrectRecordsMode::kYes, |
| 37 | + kYes, |
| 38 | + kYesW, |
| 39 | + }, |
| 40 | + ResurrectRecordsModeEntry {Orc::ResurrectRecordsMode::kResident, kResident, kResidentW}}; |
| 41 | + |
| 42 | +Orc::Result<ResurrectRecordsModeEntry> Get(Orc::ResurrectRecordsMode mode) |
| 43 | +{ |
| 44 | + using namespace Orc; |
| 45 | + |
| 46 | + if (mode >= ResurrectRecordsMode::kCount) |
| 47 | + { |
| 48 | + Log::Debug( |
| 49 | + "Failed 'ResurrectRecordsMode' conversion (value: {})", |
| 50 | + static_cast<std::underlying_type_t<ResurrectRecordsMode>>(mode)); |
| 51 | + |
| 52 | + return std::errc::no_such_file_or_directory; |
| 53 | + } |
| 54 | + |
| 55 | + const auto& entry = kModes[static_cast<size_t>(mode)]; |
| 56 | + if (std::get<0>(entry) != mode) |
| 57 | + { |
| 58 | + return std::errc::not_supported; |
| 59 | + } |
| 60 | + |
| 61 | + return entry; |
| 62 | +} |
| 63 | + |
| 64 | +} // namespace |
| 65 | + |
| 66 | +namespace Orc { |
| 67 | + |
| 68 | +Result<std::string_view> ToString(ResurrectRecordsMode mode) |
| 69 | +{ |
| 70 | + auto rv = Get(mode); |
| 71 | + if (!rv) |
| 72 | + { |
| 73 | + return rv.error(); |
| 74 | + } |
| 75 | + |
| 76 | + return std::get<1>(rv.value()); |
| 77 | +} |
| 78 | + |
| 79 | +Result<std::wstring_view> ToWString(ResurrectRecordsMode mode) |
| 80 | +{ |
| 81 | + auto rv = Get(mode); |
| 82 | + if (!rv) |
| 83 | + { |
| 84 | + return rv.error(); |
| 85 | + } |
| 86 | + |
| 87 | + return std::get<2>(rv.value()); |
| 88 | +} |
| 89 | + |
| 90 | +Result<ResurrectRecordsMode> ToResurrectRecordsMode(std::string_view mode) |
| 91 | +{ |
| 92 | + auto it = std::find_if(std::cbegin(kModes), std::cend(kModes), [&mode](const auto& item) { |
| 93 | + return boost::iequals(std::get<1>(item), mode); |
| 94 | + }); |
| 95 | + |
| 96 | + if (it != std::cend(kModes)) |
| 97 | + { |
| 98 | + return std::get<0>(*it); |
| 99 | + } |
| 100 | + |
| 101 | + return std::errc::no_such_file_or_directory; |
| 102 | +} |
| 103 | + |
| 104 | +Result<ResurrectRecordsMode> ToResurrectRecordsMode(std::wstring_view mode) |
| 105 | +{ |
| 106 | + auto it = std::find_if(std::cbegin(kModes), std::cend(kModes), [&mode](const auto& item) { |
| 107 | + return boost::iequals(std::get<2>(item), mode); |
| 108 | + }); |
| 109 | + |
| 110 | + if (it != std::cend(kModes)) |
| 111 | + { |
| 112 | + return std::get<0>(*it); |
| 113 | + } |
| 114 | + |
| 115 | + return std::errc::no_such_file_or_directory; |
| 116 | +} |
| 117 | +} // namespace Orc |
0 commit comments