Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 3 | #include <filesystem> |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 4 | #include <functional> |
William A. Kennington III | 150753f | 2022-08-05 15:20:54 -0700 | [diff] [blame] | 5 | #include <optional> |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 6 | #include <ostream> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 7 | #include <string> |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 8 | #include <string_view> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 9 | #include <unordered_map> |
| 10 | #include <vector> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace network |
| 15 | { |
| 16 | namespace config |
| 17 | { |
| 18 | |
William A. Kennington III | 150753f | 2022-08-05 15:20:54 -0700 | [diff] [blame] | 19 | /** @brief Compare in (case insensitive) vs expected (sensitive) */ |
| 20 | bool icaseeq(std::string_view in, std::string_view expected) noexcept; |
| 21 | /** @brief Turns a systemd bool string into a c++ bool */ |
| 22 | std::optional<bool> parseBool(std::string_view in) noexcept; |
| 23 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 24 | namespace fs = std::filesystem; |
| 25 | |
| 26 | fs::path pathForIntfConf(const fs::path& dir, std::string_view intf); |
| 27 | fs::path pathForIntfDev(const fs::path& dir, std::string_view intf); |
| 28 | |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 29 | template <typename T, typename Check> |
| 30 | class Checked |
| 31 | { |
| 32 | public: |
| 33 | struct unchecked |
| 34 | { |
| 35 | }; |
| 36 | |
| 37 | template <typename... Args> |
| 38 | inline constexpr Checked(Args&&... args) : |
| 39 | t(conCheck(std::forward<Args>(args)...)) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | template <typename... Args> |
| 44 | inline constexpr Checked(unchecked, Args&&... args) : |
| 45 | t(std::forward<Args>(args)...) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | inline const T& get() const noexcept |
| 50 | { |
| 51 | return t; |
| 52 | } |
| 53 | |
| 54 | inline constexpr operator const T&() const noexcept |
| 55 | { |
| 56 | return t; |
| 57 | } |
| 58 | |
| 59 | inline constexpr bool operator==(const auto& rhs) const |
| 60 | { |
| 61 | return t == rhs; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | T t; |
| 66 | |
| 67 | template <typename... Args> |
| 68 | inline static constexpr T conCheck(Args&&... args) |
| 69 | { |
| 70 | T t(std::forward<Args>(args)...); |
| 71 | Check{}(t); |
| 72 | return t; |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | template <typename T, typename Check> |
| 77 | inline constexpr bool operator==(const auto& lhs, const Checked<T, Check>& rhs) |
| 78 | { |
| 79 | return lhs == rhs.get(); |
| 80 | } |
| 81 | |
| 82 | template <typename T, typename Check> |
| 83 | inline constexpr std::ostream& operator<<(std::ostream& s, |
| 84 | const Checked<T, Check>& rhs) |
| 85 | { |
| 86 | return s << rhs.get(); |
| 87 | } |
| 88 | |
| 89 | struct KeyCheck |
| 90 | { |
| 91 | void operator()(const std::string& s); |
| 92 | }; |
| 93 | struct SectionCheck |
| 94 | { |
| 95 | void operator()(const std::string& s); |
| 96 | }; |
| 97 | struct ValueCheck |
| 98 | { |
| 99 | void operator()(const std::string& s); |
| 100 | }; |
| 101 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 102 | struct string_hash : public std::hash<std::string_view> |
| 103 | { |
| 104 | using is_transparent = void; |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 105 | |
| 106 | template <typename T> |
| 107 | inline size_t operator()(const Checked<std::string, T>& t) const |
| 108 | { |
| 109 | return static_cast<const std::hash<std::string_view>&>(*this)(t.get()); |
| 110 | } |
| 111 | template <typename T> |
| 112 | inline size_t operator()(const T& t) const |
| 113 | { |
| 114 | return static_cast<const std::hash<std::string_view>&>(*this)(t); |
| 115 | } |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 118 | using Key = Checked<std::string, KeyCheck>; |
| 119 | using Section = Checked<std::string, SectionCheck>; |
| 120 | using Value = Checked<std::string, ValueCheck>; |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 121 | using ValueList = std::vector<Value>; |
| 122 | using KeyValuesMap = |
| 123 | std::unordered_map<Key, ValueList, string_hash, std::equal_to<>>; |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 124 | using KeyValuesMapList = std::vector<KeyValuesMap>; |
| 125 | using SectionMapInt = |
| 126 | std::unordered_map<Section, KeyValuesMapList, string_hash, std::equal_to<>>; |
| 127 | |
| 128 | class SectionMap : public SectionMapInt |
| 129 | { |
| 130 | public: |
| 131 | const std::string* getLastValueString(std::string_view section, |
| 132 | std::string_view key) const noexcept; |
| 133 | inline auto getValues(std::string_view section, std::string_view key, |
| 134 | auto&& conv) const |
| 135 | { |
| 136 | std::vector<std::invoke_result_t<decltype(conv), const Value&>> values; |
| 137 | auto sit = find(section); |
| 138 | if (sit == end()) |
| 139 | { |
| 140 | return values; |
| 141 | } |
| 142 | for (const auto& secv : sit->second) |
| 143 | { |
| 144 | auto kit = secv.find(key); |
| 145 | if (kit == secv.end()) |
| 146 | { |
| 147 | continue; |
| 148 | } |
| 149 | for (auto v : kit->second) |
| 150 | { |
| 151 | values.push_back(conv(v)); |
| 152 | } |
| 153 | } |
| 154 | return values; |
| 155 | } |
| 156 | std::vector<std::string> getValueStrings(std::string_view section, |
| 157 | std::string_view key) const; |
| 158 | }; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 159 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 160 | class Parser |
| 161 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 162 | public: |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame^] | 163 | SectionMap map; |
| 164 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 165 | Parser() = default; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 166 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 167 | /** @brief Constructor |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 168 | * @param[in] filename - Absolute path of the file which will be parsed. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 169 | */ |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 170 | Parser(const fs::path& filename); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 171 | |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 172 | /** @brief Determine if there were warnings parsing the file |
| 173 | * @return The number of parsing issues in the file |
| 174 | */ |
| 175 | inline const std::vector<std::string>& getWarnings() const noexcept |
| 176 | { |
| 177 | return warnings; |
| 178 | } |
| 179 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 180 | /** @brief Get the filename last parsed successfully |
| 181 | * @return file path |
| 182 | */ |
| 183 | inline const fs::path& getFilename() const noexcept |
| 184 | { |
| 185 | return filename; |
| 186 | } |
| 187 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 188 | /** @brief Set the file name and parse it. |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 189 | * @param[in] filename - Absolute path of the file. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 190 | */ |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 191 | void setFile(const fs::path& filename); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 192 | |
| 193 | private: |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 194 | fs::path filename; |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 195 | std::vector<std::string> warnings; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 196 | }; |
| 197 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 198 | } // namespace config |
| 199 | } // namespace network |
| 200 | } // namespace phosphor |