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> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 4 | #include <string> |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 5 | #include <string_view> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 6 | #include <unordered_map> |
| 7 | #include <vector> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace network |
| 12 | { |
| 13 | namespace config |
| 14 | { |
| 15 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 16 | struct string_hash : public std::hash<std::string_view> |
| 17 | { |
| 18 | using is_transparent = void; |
| 19 | }; |
| 20 | |
| 21 | using Key = std::string; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 22 | using Section = std::string; |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 23 | using Value = std::string; |
| 24 | using ValueList = std::vector<Value>; |
| 25 | using KeyValuesMap = |
| 26 | std::unordered_map<Key, ValueList, string_hash, std::equal_to<>>; |
| 27 | using SectionMap = |
| 28 | std::unordered_map<Section, KeyValuesMap, string_hash, std::equal_to<>>; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 29 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 30 | namespace fs = std::filesystem; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 31 | |
| 32 | class Parser |
| 33 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 34 | public: |
| 35 | Parser() = default; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 36 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 37 | /** @brief Constructor |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 38 | * @param[in] filename - Absolute path of the file which will be parsed. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 39 | */ |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 40 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 41 | Parser(const fs::path& filename); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 42 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 43 | /** @brief Get the values of the given key and section. |
| 44 | * @param[in] section - section name. |
| 45 | * @param[in] key - key to look for. |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 46 | * @returns The ValueList or nullptr if no key + section exists. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 47 | */ |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 48 | const ValueList& getValues(std::string_view section, |
| 49 | std::string_view key) const noexcept; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 50 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 51 | /** @brief Set the value of the given key and section. |
| 52 | * @param[in] section - section name. |
| 53 | * @param[in] key - key name. |
| 54 | * @param[in] value - value. |
| 55 | */ |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 56 | void setValue(const std::string& section, const std::string& key, |
| 57 | const std::string& value); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 58 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 59 | /** @brief Set the file name and parse it. |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 60 | * @param[in] filename - Absolute path of the file. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 61 | */ |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 62 | void setFile(const fs::path& filename); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 63 | |
| 64 | private: |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 65 | SectionMap sections; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 66 | }; |
| 67 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 68 | } // namespace config |
| 69 | } // namespace network |
| 70 | } // namespace phosphor |