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