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 | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame^] | 22 | namespace fs = std::filesystem; |
| 23 | |
| 24 | fs::path pathForIntfConf(const fs::path& dir, std::string_view intf); |
| 25 | fs::path pathForIntfDev(const fs::path& dir, std::string_view intf); |
| 26 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 27 | struct string_hash : public std::hash<std::string_view> |
| 28 | { |
| 29 | using is_transparent = void; |
| 30 | }; |
| 31 | |
| 32 | using Key = std::string; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 33 | using Section = std::string; |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 34 | using Value = std::string; |
| 35 | using ValueList = std::vector<Value>; |
| 36 | using KeyValuesMap = |
| 37 | std::unordered_map<Key, ValueList, string_hash, std::equal_to<>>; |
| 38 | using SectionMap = |
| 39 | std::unordered_map<Section, KeyValuesMap, string_hash, std::equal_to<>>; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 40 | |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 41 | class Parser |
| 42 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 43 | public: |
| 44 | Parser() = default; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 45 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 46 | /** @brief Constructor |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 47 | * @param[in] filename - Absolute path of the file which will be parsed. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 48 | */ |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 49 | Parser(const fs::path& filename); |
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 Get the values of the given key and section. |
| 52 | * @param[in] section - section name. |
| 53 | * @param[in] key - key to look for. |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 54 | * @returns The ValueList or nullptr if no key + section exists. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 55 | */ |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 56 | const ValueList& getValues(std::string_view section, |
| 57 | std::string_view key) const noexcept; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 58 | |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 59 | /** @brief Determine if there were warnings parsing the file |
| 60 | * @return The number of parsing issues in the file |
| 61 | */ |
| 62 | inline const std::vector<std::string>& getWarnings() const noexcept |
| 63 | { |
| 64 | return warnings; |
| 65 | } |
| 66 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame^] | 67 | /** @brief Get the filename last parsed successfully |
| 68 | * @return file path |
| 69 | */ |
| 70 | inline const fs::path& getFilename() const noexcept |
| 71 | { |
| 72 | return filename; |
| 73 | } |
| 74 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 75 | /** @brief Set the file name and parse it. |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 76 | * @param[in] filename - Absolute path of the file. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 77 | */ |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 78 | void setFile(const fs::path& filename); |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 79 | |
| 80 | private: |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame^] | 81 | fs::path filename; |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 82 | SectionMap sections; |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 83 | std::vector<std::string> warnings; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 84 | }; |
| 85 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 86 | } // namespace config |
| 87 | } // namespace network |
| 88 | } // namespace phosphor |