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