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> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 6 | #include <string> |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 7 | #include <string_view> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 8 | #include <unordered_map> |
| 9 | #include <vector> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 10 | |
| 11 | namespace phosphor |
| 12 | { |
| 13 | namespace network |
| 14 | { |
| 15 | namespace config |
| 16 | { |
| 17 | |
William A. Kennington III | 150753f | 2022-08-05 15:20:54 -0700 | [diff] [blame] | 18 | /** @brief Compare in (case insensitive) vs expected (sensitive) */ |
| 19 | bool icaseeq(std::string_view in, std::string_view expected) noexcept; |
| 20 | /** @brief Turns a systemd bool string into a c++ bool */ |
| 21 | std::optional<bool> parseBool(std::string_view in) noexcept; |
| 22 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 23 | namespace fs = std::filesystem; |
| 24 | |
| 25 | fs::path pathForIntfConf(const fs::path& dir, std::string_view intf); |
| 26 | fs::path pathForIntfDev(const fs::path& dir, std::string_view intf); |
| 27 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 28 | struct string_hash : public std::hash<std::string_view> |
| 29 | { |
| 30 | using is_transparent = void; |
| 31 | }; |
| 32 | |
| 33 | using Key = std::string; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 34 | using Section = std::string; |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 35 | using Value = std::string; |
| 36 | using ValueList = std::vector<Value>; |
| 37 | using KeyValuesMap = |
| 38 | std::unordered_map<Key, ValueList, string_hash, std::equal_to<>>; |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame^] | 39 | using KeyValuesMapList = std::vector<KeyValuesMap>; |
| 40 | using SectionMapInt = |
| 41 | std::unordered_map<Section, KeyValuesMapList, string_hash, std::equal_to<>>; |
| 42 | |
| 43 | class SectionMap : public SectionMapInt |
| 44 | { |
| 45 | public: |
| 46 | const std::string* getLastValueString(std::string_view section, |
| 47 | std::string_view key) const noexcept; |
| 48 | inline auto getValues(std::string_view section, std::string_view key, |
| 49 | auto&& conv) const |
| 50 | { |
| 51 | std::vector<std::invoke_result_t<decltype(conv), const Value&>> values; |
| 52 | auto sit = find(section); |
| 53 | if (sit == end()) |
| 54 | { |
| 55 | return values; |
| 56 | } |
| 57 | for (const auto& secv : sit->second) |
| 58 | { |
| 59 | auto kit = secv.find(key); |
| 60 | if (kit == secv.end()) |
| 61 | { |
| 62 | continue; |
| 63 | } |
| 64 | for (auto v : kit->second) |
| 65 | { |
| 66 | values.push_back(conv(v)); |
| 67 | } |
| 68 | } |
| 69 | return values; |
| 70 | } |
| 71 | std::vector<std::string> getValueStrings(std::string_view section, |
| 72 | std::string_view key) const; |
| 73 | }; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 74 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 75 | class Parser |
| 76 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 77 | public: |
| 78 | Parser() = default; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 79 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 80 | /** @brief Constructor |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 81 | * @param[in] filename - Absolute path of the file which will be parsed. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 82 | */ |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 83 | Parser(const fs::path& filename); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 84 | |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame^] | 85 | /** @brief Retrieve the map of all values in the file */ |
| 86 | inline const SectionMap& getMap() const noexcept |
| 87 | { |
| 88 | return sections; |
| 89 | } |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 90 | |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 91 | /** @brief Determine if there were warnings parsing the file |
| 92 | * @return The number of parsing issues in the file |
| 93 | */ |
| 94 | inline const std::vector<std::string>& getWarnings() const noexcept |
| 95 | { |
| 96 | return warnings; |
| 97 | } |
| 98 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 99 | /** @brief Get the filename last parsed successfully |
| 100 | * @return file path |
| 101 | */ |
| 102 | inline const fs::path& getFilename() const noexcept |
| 103 | { |
| 104 | return filename; |
| 105 | } |
| 106 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 107 | /** @brief Set the file name and parse it. |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 108 | * @param[in] filename - Absolute path of the file. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 109 | */ |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 110 | void setFile(const fs::path& filename); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 111 | |
| 112 | private: |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 113 | fs::path filename; |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 114 | SectionMap sections; |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 115 | std::vector<std::string> warnings; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 116 | }; |
| 117 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 118 | } // namespace config |
| 119 | } // namespace network |
| 120 | } // namespace phosphor |